Generate utility method to construct refined-aware case classes automatically
See original GitHub issueHi,
It’s convenience to have a utility method to construct case classes automatically. Those case classes will have some field types are refined- ones. Returning value will be an Option
which resolves if validation passes.
In detail, it would be something like:
case class Foo(a: String @@ NonEmpty, b: Int, c: Int @@ Greater[_0] = 1)
object Foo {
// It would be great to have the following method generated automatically
def refine(a: String, b: Int, c: Int = 1): Option[Foo] {
val fooEither = for {
refinedA <- refineT[NonEmpty](a)
refinedC <- refineT[Greater[_0]](c)
} yield Foo(refinedA, b, refinedC)
fooEither match {
case Right(r) => Some(r)
case Left(_) => None
}
}
}
I wonder if it aligns with this project’s roadmap. If yes, I’m happy to submit a pull request because I have a working version using annotation macro right now.
Thanks.
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
4.14. Generating Boilerplate Code with Case Classes - O'Reilly
An apply method is generated, so you don't need to use the new keyword to create a new instance of the class. Accessor...
Read more >scala - automatically generate case object for case class
You can create macro annotation (generating companion object, failing if it already exists) import scala.annotation.
Read more >Case Classes | Scala Book
A great thing about a case class is that it automatically generates an unapply method for your class, so you don't have to...
Read more >Using Case Classes to Structure Your Data - Daniela Sfregola
A case class provides a convenient and efficient way to represent your data in an immutable way to make sure your program can...
Read more >Java 15 through the eyes of a Scala programmer
What are the similarities between records and case classes? equals , hashCode and toString methods are automatically generated (unless ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Since the future of macros in Scala is currently uncertain, I feel uncomfortable adding more to the code base. Closing for now.
OK. I’ll work on the pull request first and we can sort it out later. Take care.