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 RefinedTypeOps for companion objects of refined types #369

Merged
merged 12 commits into from
Dec 17, 2017

Conversation

fthomas
Copy link
Owner

@fthomas fthomas commented Dec 15, 2017

This is a solution for #342 but unlike #346 it does not add a new macro. Compared to a macro solution, companion objects need be written manually and require a little bit boilerplate. E.g. for a refined type type PosInt = Int Refined Positive one would need to write a companion that extends from RefinedTypeOps like this:

object PosInt extends RefinedTypeOps[PosInt, Int]

Usage then looks like:

scala> PosInt(5)
res4: PosInt = 5

scala> PosInt(-5)
<console>:51: error: Predicate failed: (-5 > 0).
       PosInt(-5)
             ^

scala> PosInt.from(5)
res6: Either[String,PosInt] = Right(5)

scala> val PosInt(x) = 5
x: PosInt = 5

So this PR adds the RefinedTypeOps class to simplify the definition of companion objects of refined types, the RefinedType type class that combines RefType and Validate instances of a refined type (thus making #193 obsolete), and companion objects for refined types in the eu.timepit.refined.types package.

@codecov
Copy link

codecov bot commented Dec 15, 2017

Codecov Report

Merging #369 into master will decrease coverage by 2.37%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #369      +/-   ##
==========================================
- Coverage   98.94%   96.57%   -2.38%     
==========================================
  Files          39       41       +2     
  Lines         476      497      +21     
  Branches        8       10       +2     
==========================================
+ Hits          471      480       +9     
- Misses          5       17      +12
Impacted Files Coverage Δ
...ain/scala/eu/timepit/refined/api/RefinedType.scala 100% <100%> (ø)
.../scala/eu/timepit/refined/api/RefinedTypeOps.scala 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7c8df7b...20a7b10. Read the comment docs.

@fthomas fthomas changed the title WIP: Add RefinedTypeOps for companion objects of refined types Add RefinedTypeOps for companion objects of refined types Dec 16, 2017
@fthomas fthomas merged commit 390f685 into master Dec 17, 2017
@fthomas fthomas deleted the topic/RefinedTypeOps branch December 17, 2017 09:12
@fthomas
Copy link
Owner Author

fthomas commented Dec 21, 2017

Another advantage of this approach over a macro that just adds some functions to an object is that we can easily add extension methods to any refined type companion. See #382 for details.

@fthomas fthomas mentioned this pull request Dec 23, 2017
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.

1 participant