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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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.

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