question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate utility method to construct refined-aware case classes automatically

See original GitHub issue

Hi,

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:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fthomascommented, Feb 3, 2019

Since the future of macros in Scala is currently uncertain, I feel uncomfortable adding more to the code base. Closing for now.

0reactions
trungfinitycommented, Nov 27, 2015

OK. I’ll work on the pull request first and we can sort it out later. Take care.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found