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

Support aliasing RefineM #107

Closed
fthomas opened this issue Dec 15, 2015 · 1 comment · Fixed by #108
Closed

Support aliasing RefineM #107

fthomas opened this issue Dec 15, 2015 · 1 comment · Fixed by #108
Assignees

Comments

@fthomas
Copy link
Owner

fthomas commented Dec 15, 2015

The RefineM macro can currently be invoked via the refineM functions (e.g. refineMV[Positive](1)) or via an implicit conversion which needs to be imported with import auto._ (it then can be invoked with 1: (Int Refined Positive)). It would be nice if users of refined could write their own aliases for RefineM with fixed types and without importing an implicit conversion. See https://github.com/quasar-analytics/quasar/pull/1025 for more context.

We can support this with a variation of the RefineMPartiallyApplied class:

final class RefineMFullyApplied[F[_, _], T, P] {
  def apply(t: T)(implicit v: Validate[T, P], rt: RefType[F]): F[T, P] = macro RefineM.macroImpl[F, T, P]
}

val Natural = new RefineMFullyApplied[Refined, Long, NonNegative]
scala> Natural(1L)
res3: eu.timepit.refined.api.Refined[Long,eu.timepit.refined.numeric.NonNegative] = Refined(1)

scala> Natural(-1)
<console>:47: error: Predicate (-1 < 0) did not fail.
       Natural(-1)
              ^

scala> Natural(1.3)
<console>:47: error: type mismatch;
 found   : Double(1.3)
 required: Long
       Natural(1.3)
               ^

/cc @jedesah

@fthomas fthomas self-assigned this Dec 15, 2015
@wemrysi
Copy link

wemrysi commented Dec 15, 2015

Nice! 👍

fthomas added a commit that referenced this issue Dec 15, 2015
`refineMF` is a variation of `refineM` but which requires that the base
type is specified before `apply` can be called. This allows to create
aliases like `val Natural = RefType[Refined].refineMF[Long, NonNegative]`
which only can be called with `Long` arguments.

closes #107
fthomas added a commit that referenced this issue Dec 15, 2015
`refineMF` is a variation of `refineM` but which requires that the base
type is specified before `apply` can be called. This allows to create
aliases like `val Natural = RefType[Refined].refineMF[Long, NonNegative]`
which only can be called with `Long` arguments.

closes #107
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants