Create a gist now

Instantly share code, notes, and snippets.

@folone /gist:4945168
Last active Dec 13, 2015

A taste of dependent types in scala with shapeless.
> shapeless-core/console
[warn] Credentials file /home/folone/.ivy2/.credentials does not exist
[info] Compiling 24 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes...
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.0-20130205-141957-132e09fc2e (OpenJDK 64-Bit Server VM, Java 1.7.0_09).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import shapeless._
import shapeless._
scala> import SingletonTypes._
import SingletonTypes._
scala> val x: ^(1 > 2) = false
x: Boolean(false) = false
scala> val x: ^(1 > 2) = true
<console>:13: error: type mismatch;
found : Boolean(true)
required: Boolean(false)
val x: ^(1 > 2) = true
^
scala> val x: ^(1 + 3 * 20 / 10) = 7
x: Int(7) = 7
scala> val x: ^(1 + 3 * 20 / 10) = 6
<console>:13: error: type mismatch;
found : Int(6)
required: Int(7)
val x: ^(1 + 3 * 20 / 10) = 6
^
scala> def factorial(n: Int):Int = if(n <= 1) 1 else n * factorial(n-1)
factorial: (n: Int)Int
scala> val test: ^(factorial(5)) = 102
<console>:14: error: type mismatch;
found : Int(102)
required: Int(120)
val test: ^(factorial(5)) = 102
^
scala> val test: ^(factorial(5)) = 120
test: Int(120) = 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment