newtypes
newtypes copied to clipboard
Generalize HasBuilder to enable modelling infallible constructors (for NewtypeWrapped in contrast to NewValidated)
/cc @lgmyrek
Hey, thanks for the PR.
Can you explain where having infallible constructors would help?
For usage with NewtypeWrapped. That's just a wrapper, so there's no validation that could fail.
Than you can coerce all typeclass implementations for A into NewtypeWrapped[A]. This is how we do it
implicit class CoerceFOps[F[_], A](private val self: F[A]) extends AnyVal {
def coerce[B](
implicit
extractor: HasExtractor.Aux[B, A],
builder: HasBuilder.Aux[B, Nothing, A],
): F[B] = self.asInstanceOf[F[B]]
}
What do you think @alexandru , is this a sensible approach?
Before we did it like this
implicit class CoerceFOps[F[_], A](private val self: F[A]) extends AnyVal {
def coerce[B <: NewtypeWrapped[A]#Type]: F[B] = self.asInstanceOf[F[B]]
}
but we felt that it's cleaner to use the HasExtractor/HasBuilder type classes rather than <: NewtypeWrapped[A]#Type.
I'm thinking yes, but let me think about it. I'm taking a look tonight, to play around with the code.
Hello @alexandru , have you had a chance to consider this PR for merging? 😺