Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prelude Newtypes lack implicit coercion #1464

Open
googley42 opened this issue Jan 25, 2025 · 0 comments
Open

Prelude Newtypes lack implicit coercion #1464

googley42 opened this issue Jan 25, 2025 · 0 comments

Comments

@googley42
Copy link
Collaborator

googley42 commented Jan 25, 2025

Prelude Newtypes lack of implicit coercion causes issues with interop with Newtypes from other libraries.

So in some of our services at work we have > 60 Newtypes which have to span libraries eg Smithy4s Newtypes and Prelude Newtypes, and for the transformations we use Chimney.

The smith4s newtypes have a Wrap/Unwrap typeclass called Bijection which is shown below

trait Bijection[A, B] extends Function[A, B] { outer =>
  def to(a: A): B
  def from(b: B): A
}

This type class is used to define implicit conversions via Chimney transformations.

Just about all the other Newtype libraries have some kind of coercion, however Prelude Newtypes do not - the team had provide a cake style hack to achieve this coercion ie:

  trait BijectionSupport[A] { self: zio.prelude.Newtype[A] =>
    
    implicit final val zioNewTypeSmithyBijection: Bijection[A, Type] =
      new Bijection[A, Type] {
        def to(a: A): Type = self.apply(a) 
        def from(b: Type): A = self.unwrap(b)
      }
  }

// usage of BijectionSupport
type AccountId = AccountId.Type
object AccountId extends Newtype[String] with BijectionSupport[String]

cc @jdegoes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant