Created
March 8, 2019 16:27
-
-
Save bkyrlach/e2e798d3db7c3865c3ed4ab79440436f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| object Proof1 { | |
| def main(args: Array[String]): Unit = { | |
| trait IsRaining | |
| trait IsCloudy | |
| def proveIt(p: IsRaining): IsCloudy = new IsCloudy {} | |
| proveIt(new IsRaining{}) match { | |
| case _: IsCloudy => println("It's cloudy!") | |
| } | |
| } | |
| } | |
| object Proof1_2 { | |
| sealed class IsRaining private () {} | |
| object IsRaining { | |
| private val instance = new IsRaining() | |
| def getInstance: IsRaining = instance | |
| } | |
| sealed class IsCloudy private () {} | |
| object IsCloudy { | |
| private val instance = new IsCloudy() | |
| def getInstance: IsCloudy = instance | |
| } | |
| def main(args: Array[String]): Unit = { | |
| def proveIt(p: IsRaining): IsCloudy = IsCloudy.getInstance | |
| proveIt(IsRaining.getInstance) match { | |
| case _: IsCloudy => println("It's cloudy!") | |
| } | |
| } | |
| } |
This file contains hidden or 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
| object Proof2 { | |
| def main(args: Array[String]): Unit = { | |
| def getOrDefault[A](m: Option[A], fallback: A): A = m match { | |
| case Some(a) => a | |
| case None => fallback | |
| } | |
| val a: Option[Int] = Some(3) | |
| val b: Option[Int] = None | |
| val c: Option[String] = Some("Hello, Tangram!") | |
| val d: Option[String] = None | |
| println(getOrDefault(a, -1)) | |
| println(getOrDefault(b, -1)) | |
| println(getOrDefault(c, "Hello, world.")) | |
| println(getOrDefault(d, "Hello, world.")) | |
| def get[A](m: Option[A]): A = m match { | |
| case Some(a) => a | |
| case None => throw new Exception("Nothing else we can do here!") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment