Created
June 6, 2012 18:08
-
-
Save gclaramunt/2883664 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 FlightStatus | |
trait Flying extends FlightStatus | |
trait Landed extends FlightStatus | |
case class Plane[Status <: FlightStatus]() | |
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
I still don't see the phantoms. Oh, maybe they are invisible...