Skip to content

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

Open
sideeffffect wants to merge 2 commits intomonix:mainfrom
sideeffffect:infallible-HasBuilder
Open

Generalize HasBuilder to enable modelling infallible constructors (for NewtypeWrapped in contrast to NewValidated)#174
sideeffffect wants to merge 2 commits intomonix:mainfrom
sideeffffect:infallible-HasBuilder

Conversation

@sideeffffect
Copy link

/cc @lgmyrek

…r NewtypeWrapped in contrast to NewValidated)
@alexandru
Copy link
Member

Hey, thanks for the PR.

Can you explain where having infallible constructors would help?

@sideeffffect
Copy link
Author

sideeffffect commented Oct 3, 2024

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
Copy link
Author

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

@sideeffffect
Copy link
Author

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.

@alexandru
Copy link
Member

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

@sideeffffect
Copy link
Author

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

@alexandru alexandru force-pushed the main branch 3 times, most recently from cc3289c to b28e2fa Compare March 1, 2026 08:17
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

Successfully merging this pull request may close these issues.

2 participants