-
-
Save fedesilva/2883723 to your computer and use it in GitHub Desktop.
Very simple phantom types example
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
trait Flying | |
trait Landed | |
case class Plane[Status]() | |
def land(p:Plane[Flying])=Plane[Landed]() | |
def takeOff(p:Plane[Landed])= Plane[Flying]() | |
val plane = new Plane[Landed]() | |
/* | |
scala> val flying=takeOff(plane) | |
flying: Plane[Flying] = Plane() | |
scala> val landed=land(flying) | |
landed: Plane[Landed] = Plane() | |
scala> takeOff(flying) | |
<console>:15: error: type mismatch; | |
found : Plane[Flying] | |
required: Plane[Landed] | |
takeOff(flying) | |
^ | |
scala> land(landed) | |
<console>:17: error: type mismatch; | |
found : Plane[Landed] | |
required: Plane[Flying] | |
land(landed) | |
^ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment