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

Add apply method to Schema #2023

Merged
merged 2 commits into from
Nov 29, 2023
Merged

Add apply method to Schema #2023

merged 2 commits into from
Nov 29, 2023

Conversation

kyri-petrou
Copy link
Collaborator

This PR adds helper methods to "summon" a schema of type A. This is particularly useful in Scala 3 when schemas are derived via the derives keyword. e.g.,:

case class Foo(x: Int) derives Schema.SemiAuto
case class Bar(x: Int)

// Before
given Schema[Any, Bar] = summon[Schema[Any, Foo]].contramap(???)

// Now
given Schema[Any, Bar] = Schema[Foo].contramap(???)

Also works with custom schemas:

trait Env
object MySchema extends SchemaDerivation[Env]
case class Foo(x: Int) derives MySchema.SemiAuto
case class Bar(x: Int)

given Schema[Env, Bar] = MySchema[Foo].contramap(???)

Another option would be to have the apply method defined on object Schema directly as below, but I thought it might be a bit annoying having to add the environment parameter each time

def apply[R, A](implicit ev: Schema[R, A]): Schema[R, A] = ev

Copy link
Collaborator

@frekw frekw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement! 👍

@kyri-petrou
Copy link
Collaborator Author

Agh, should probably add one for ArgBuilder while at it. I'll add it now

@kyri-petrou kyri-petrou merged commit eaed53c into series/2.x Nov 29, 2023
10 checks passed
@kyri-petrou kyri-petrou deleted the schema-apply-methods branch November 29, 2023 06:49
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.

3 participants