Implicit conversion for libraries dependent on refined (seperating `auto` to trait and object)
See original GitHub issueProblem:
Libraries which depend on refined types currently force the user to call import eu.timepit.refined.auto._
to allow standard type variables (literals) to be implicitly converted into their refined types.
Example:
import eu.timepit.refined.api.Refined
import eu.timepit.refined.api.Validate
case class ZeroOrOne()
implicit val zeroOrOneValidate: Validate.Plain[Int, ZeroOrOne] =
Validate.fromPredicate(p => p == 0 || p == 1, p => s"($p is in not binary)", ZeroOrOne())
def libFunc(x : Int Refined ZeroOrOne) : Int = x.get
libFunc(1)
fails without the import
Solution (as discussed in gitter):
Split refined’s auto
into trait and object (see code). I recommend that the trait’s name will be more significant than auto
.
This will allow the user to extend a companion object to the refined type from that trait, thus allowing the implicit conversion. So the example above changes into:
import eu.timepit.refined.api.Refined
import eu.timepit.refined.api.Validate
case class ZeroOrOne()
object ZeroOrOne extends eu.timepit.refined.api.auto //Or other name if chosen
implicit val zeroOrOneValidate: Validate.Plain[Int, ZeroOrOne] =
Validate.fromPredicate(p => p == 0 || p == 1, p => s"($p is in not binary)", ZeroOrOne())
def libFunc(x : Int Refined ZeroOrOne) : Int = x.get
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:13 (6 by maintainers)
Top Results From Across the Web
estatico/scala-newtype - Gitter
Hello, I have a question: does it make a difference to use map vs coerce ? I have a refined type which I...
Read more >Proposal: Changes to Implicit Conversions - Scala Contributors
Conversion to the standard library, as a subtype of Function1 . Implicit conversions are changed to be given instances of the new type....
Read more >Implicit Implications (part 3): The Future is Functional (implicitly)
These objects contain implicit conversions between Scala Iterables ... ability of the compiler to resolve dependant object types correctly.
Read more >Implicit class vs Implicit conversion to trait - Stack Overflow
I'm trying to add new functions to existing types (so I can have the IDE auto suggest relevant functions ...
Read more >5 Property Refinement / Resolution
During refinement the set of properties that apply to a formatting object is transformed into a set of traits that define constraints on...
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
Ok, applying this diff
allows this usage with only one import that introduces the type alias
type PosInt = Int Refined Positive
:So for
Refined
types we can basically get rid off the extraauto._
import at the expense that users can’t opt out of the implicit conversionsT => Refined[T, P]
andRefined[T, P] => T
. I need to think about the pros and cons of this approach to decide if we want to do this.Thanks @fthomas. Nice update! You may now close this issue. I’m using singleton-ops which better fits my use-case.