Skip to content

Commit

Permalink
Verify that the examples from the README type-check
Browse files Browse the repository at this point in the history
  • Loading branch information
fthomas committed Jun 6, 2015
1 parent f1812d8 commit 9f6d7d9
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 138 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ an excellent motivation why this kind of library is useful).
A quick example:

```scala
import eu.timepit.refined._
import eu.timepit.refined.numeric._

// refineLit decorates the type of its parameter if it satisfies the
// given type-level predicate:
scala> refineLit[Positive](5)
Expand Down Expand Up @@ -46,6 +49,9 @@ The type conversion of refined types is a compile-time operation that is
provided by the library:

```scala
import shapeless.nat._
import shapeless.tag.@@

scala> val a: Int @@ Greater[_5] = refineLit(10)
a: Int @@ Greater[_5] = 10

Expand All @@ -68,6 +74,13 @@ intervention.
## More examples

```scala
import shapeless.{ ::, HNil }
import eu.timepit.refined.boolean._
import eu.timepit.refined.char._
import eu.timepit.refined.collection._
import eu.timepit.refined.generic._
import eu.timepit.refined.string._

scala> refineLit[NonEmpty]("Hello")
res2: String @@ NonEmpty = Hello

Expand Down
49 changes: 0 additions & 49 deletions docs/example.md

This file was deleted.

97 changes: 97 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
The examples from the [README.md](https://github.com/fthomas/refined/blob/master/README.md):

```scala
scala> import eu.timepit.refined._
import eu.timepit.refined._

scala> import eu.timepit.refined.numeric._
import eu.timepit.refined.numeric._

scala> refineLit[Positive](5)
res0: shapeless.tag.@@[Int,eu.timepit.refined.numeric.Positive] = 5

scala> refineLit[Positive](-5)
<console>:15: error: Predicate failed: (-5 > 0).
refineLit[Positive](-5)
^

scala> refine[Positive](5)
res2: Either[String,shapeless.tag.@@[Int,eu.timepit.refined.numeric.Positive]] = Right(5)

scala> refine[Positive](-5)
res3: Either[String,shapeless.tag.@@[Int,eu.timepit.refined.numeric.Positive]] = Left(Predicate failed: (-5 > 0).)
```

```scala
scala> import shapeless.nat._
import shapeless.nat._

scala> import shapeless.tag.@@
import shapeless.tag.$at$at

scala> val a: Int @@ Greater[_5] = refineLit(10)
a: shapeless.tag.@@[Int,eu.timepit.refined.numeric.Greater[shapeless.nat._5]] = 10

scala> val b: Int @@ Greater[_4] = a
b: shapeless.tag.@@[Int,eu.timepit.refined.numeric.Greater[shapeless.nat._4]] = 10

scala> val c: Int @@ Greater[_6] = a
<console>:19: error: invalid inference: eu.timepit.refined.numeric.Greater[shapeless.nat._5] ==> eu.timepit.refined.numeric.Greater[shapeless.nat._6]
val c: Int @@ Greater[_6] = a
^
```

```scala
scala> import shapeless.{ ::, HNil }
import shapeless.{$colon$colon, HNil}

scala> import eu.timepit.refined.boolean._
import eu.timepit.refined.boolean._

scala> import eu.timepit.refined.char._
import eu.timepit.refined.char._

scala> import eu.timepit.refined.collection._
import eu.timepit.refined.collection._

scala> import eu.timepit.refined.generic._
import eu.timepit.refined.generic._

scala> import eu.timepit.refined.string._
import eu.timepit.refined.string._

scala> refineLit[NonEmpty]("Hello")
res4: shapeless.tag.@@[String,eu.timepit.refined.collection.NonEmpty] = Hello

scala> refineLit[NonEmpty]("")
<console>:35: error: Predicate isEmpty() did not fail.
refineLit[NonEmpty]("")
^

scala> type ZeroToOne = Not[Less[_0]] And Not[Greater[_1]]
defined type alias ZeroToOne

scala> refineLit[ZeroToOne](1.8)
<console>:36: error: Right predicate of (!(1.8 < 0) && !(1.8 > 1)) failed: Predicate (1.8 > 1) did not fail.
refineLit[ZeroToOne](1.8)
^

scala> refineLit[AnyOf[Digit :: Letter :: Whitespace :: HNil]]('F')
res7: shapeless.tag.@@[Char,eu.timepit.refined.boolean.AnyOf[shapeless.::[eu.timepit.refined.char.Digit,shapeless.::[eu.timepit.refined.char.Letter,shapeless.::[eu.timepit.refined.char.Whitespace,shapeless.HNil]]]]] = F

scala> refineLit[MatchesRegex[W.`"[0-9]+"`.T]]("123.")
<console>:35: error: Predicate failed: "123.".matches("[0-9]+").
refineLit[MatchesRegex[W.`"[0-9]+"`.T]]("123.")
^

scala> val d1: Char @@ Equal[W.`'3'`.T] = refineLit('3')
d1: shapeless.tag.@@[Char,eu.timepit.refined.generic.Equal[Char('3')]] = 3

scala> val d2: Char @@ Digit = d1
d2: shapeless.tag.@@[Char,eu.timepit.refined.char.Digit] = 3

scala> val d3: Char @@ Letter = d1
<console>:35: error: invalid inference: eu.timepit.refined.generic.Equal[Char('3')] ==> eu.timepit.refined.char.Letter
val d3: Char @@ Letter = d1
^
```
21 changes: 8 additions & 13 deletions docs/point.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,19 @@ that provides a string representation for the predicate that is used for error
messages:

```scala
scala> import eu.timepit.refined.Predicate
import eu.timepit.refined.Predicate

scala> implicit val quadrant1Predicate: Predicate[Quadrant1, Point] =
| Predicate.instance(p => p.x >= 0 && p.y >= 0, p => s"($p is in quadrant 1)")
quadrant1Predicate: eu.timepit.refined.Predicate[Quadrant1,Point] = eu.timepit.refined.Predicate$$anon$2@31757faf
implicit val quadrant1Predicate: Predicate[Quadrant1, Point] =
Predicate.instance(p => p.x >= 0 && p.y >= 0, p => s"($p is in quadrant 1)")

scala> implicit val quadrant2Predicate: Predicate[Quadrant2, Point] =
| Predicate.instance(p => p.x < 0 && p.y >= 0, p => s"($p is in quadrant 2)")
quadrant2Predicate: eu.timepit.refined.Predicate[Quadrant2,Point] = eu.timepit.refined.Predicate$$anon$2@2d92f894
implicit val quadrant2Predicate: Predicate[Quadrant2, Point] =
Predicate.instance(p => p.x < 0 && p.y >= 0, p => s"($p is in quadrant 2)")

scala> implicit val quadrant3Predicate: Predicate[Quadrant3, Point] =
| Predicate.instance(p => p.x < 0 && p.y < 0, p => s"($p is in quadrant 3)")
quadrant3Predicate: eu.timepit.refined.Predicate[Quadrant3,Point] = eu.timepit.refined.Predicate$$anon$2@fdab625
implicit val quadrant3Predicate: Predicate[Quadrant3, Point] =
Predicate.instance(p => p.x < 0 && p.y < 0, p => s"($p is in quadrant 3)")

scala> implicit val quadrant4Predicate: Predicate[Quadrant4, Point] =
| Predicate.instance(p => p.x >= 0 && p.y < 0, p => s"($p is in quadrant 4)")
quadrant4Predicate: eu.timepit.refined.Predicate[Quadrant4,Point] = eu.timepit.refined.Predicate$$anon$2@16ce3417
implicit val quadrant4Predicate: Predicate[Quadrant4, Point] =
Predicate.instance(p => p.x >= 0 && p.y < 0, p => s"($p is in quadrant 4)")
```

We have now everything in place to refine `Point` values with the `refine`
Expand Down
28 changes: 0 additions & 28 deletions docs/safeBigInt.md

This file was deleted.

26 changes: 0 additions & 26 deletions docs/src/example.md

This file was deleted.

52 changes: 52 additions & 0 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
The examples from the [README.md](https://github.com/fthomas/refined/blob/master/README.md):

```tut:nofail
import eu.timepit.refined._
import eu.timepit.refined.numeric._
refineLit[Positive](5)
refineLit[Positive](-5)
refine[Positive](5)
refine[Positive](-5)
```

```tut:nofail
import shapeless.nat._
import shapeless.tag.@@
val a: Int @@ Greater[_5] = refineLit(10)
val b: Int @@ Greater[_4] = a
val c: Int @@ Greater[_6] = a
```

```tut:nofail
import shapeless.{ ::, HNil }
import eu.timepit.refined.boolean._
import eu.timepit.refined.char._
import eu.timepit.refined.collection._
import eu.timepit.refined.generic._
import eu.timepit.refined.string._
refineLit[NonEmpty]("Hello")
refineLit[NonEmpty]("")
type ZeroToOne = Not[Less[_0]] And Not[Greater[_1]]
refineLit[ZeroToOne](1.8)
refineLit[AnyOf[Digit :: Letter :: Whitespace :: HNil]]('F')
refineLit[MatchesRegex[W.`"[0-9]+"`.T]]("123.")
val d1: Char @@ Equal[W.`'3'`.T] = refineLit('3')
val d2: Char @@ Digit = d1
val d3: Char @@ Letter = d1
```
2 changes: 1 addition & 1 deletion docs/src/point.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ one that checks if a given `Point` lies in the corresponding quadrant and one
that provides a string representation for the predicate that is used for error
messages:

```tut
```tut:silent
import eu.timepit.refined.Predicate
implicit val quadrant1Predicate: Predicate[Quadrant1, Point] =
Expand Down
16 changes: 0 additions & 16 deletions docs/src/safeBigInt.md

This file was deleted.

6 changes: 1 addition & 5 deletions src/main/scala/eu/timepit/refined/internal/RefineLit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ object RefineLit {
t.tree match {
case Literal(Constant(value)) =>
predicate.validated(value.asInstanceOf[T]) match {
case None =>
val pTpe = weakTypeOf[P]
val tTpe = weakTypeOf[T]
c.Expr(q"$t.asInstanceOf[$tTpe @@ $pTpe]")

case None => c.Expr(q"$t.asInstanceOf[${weakTypeOf[T @@ P]}]")
case Some(msg) => c.abort(c.enclosingPosition, msg)
}

Expand Down

0 comments on commit 9f6d7d9

Please sign in to comment.