Last active
April 27, 2017 13:10
-
-
Save folone/4945168 to your computer and use it in GitHub Desktop.
A taste of dependent types in scala with shapeless.
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
> 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
Hi! Is it still alive in 2017? What part of shapeless is it?