-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #483 from fthomas/topic/AsValueOf
Unify numeric type class instances
- Loading branch information
Showing
15 changed files
with
183 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
modules/core/shared/src/main/scala/eu/timepit/refined/internal/WitnessAs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package eu.timepit.refined.internal | ||
|
||
import shapeless.{Nat, Witness} | ||
import shapeless.ops.nat.ToInt | ||
|
||
/** | ||
* `WitnessAs[A, B]` provides the singleton value of type `A` in `fst` | ||
* and `fst` converted to type `B` in `snd`. | ||
* | ||
* The purpose of this type class is to write numeric type class | ||
* instances that work with both literal singleton types and | ||
* `shapeless.Nat`. | ||
* | ||
* Example: {{{ | ||
* scala> import eu.timepit.refined.W | ||
* | import shapeless.nat._5 | ||
* | ||
* scala> WitnessAs[W.`5`.T, Int] | ||
* res1: WitnessAs[W.`5`.T, Int] = WitnessAs(5,5) | ||
* | ||
* scala> WitnessAs[_5, Int] | ||
* res2: WitnessAs[_5, Int] = WitnessAs(Succ(),5) | ||
* }}} | ||
*/ | ||
final case class WitnessAs[A, B](fst: A, snd: B) | ||
|
||
object WitnessAs { | ||
def apply[A, B](implicit ev: WitnessAs[A, B]): WitnessAs[A, B] = ev | ||
|
||
implicit def natWitnessAs[B, A <: Nat]( | ||
implicit | ||
wa: Witness.Aux[A], | ||
ta: ToInt[A], | ||
nb: Numeric[B] | ||
): WitnessAs[A, B] = | ||
WitnessAs(wa.value, nb.fromInt(ta.apply())) | ||
|
||
implicit def singletonWitnessAs[B, A <: B]( | ||
implicit wa: Witness.Aux[A] | ||
): WitnessAs[A, B] = | ||
WitnessAs(wa.value, wa.value) | ||
} |
Oops, something went wrong.