newtypes icon indicating copy to clipboard operation
newtypes copied to clipboard

Generalize HasBuilder to enable modelling infallible constructors (for NewtypeWrapped in contrast to NewValidated)

Open sideeffffect opened this issue 1 year ago • 6 comments

/cc @lgmyrek

sideeffffect avatar Oct 02 '24 11:10 sideeffffect

Hey, thanks for the PR.

Can you explain where having infallible constructors would help?

alexandru avatar Oct 03 '24 07:10 alexandru

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]]
  }

sideeffffect avatar Oct 03 '24 20:10 sideeffffect

What do you think @alexandru , is this a sensible approach?

sideeffffect avatar Oct 06 '24 10:10 sideeffffect

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.

sideeffffect avatar Oct 07 '24 09:10 sideeffffect

I'm thinking yes, but let me think about it. I'm taking a look tonight, to play around with the code.

alexandru avatar Oct 09 '24 07:10 alexandru

Hello @alexandru , have you had a chance to consider this PR for merging? 😺

sideeffffect avatar Jun 14 '25 18:06 sideeffffect