Skip to content

Instantly share code, notes, and snippets.

@gclaramunt
Created June 6, 2012 18:08
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gclaramunt/2883664 to your computer and use it in GitHub Desktop.
Save gclaramunt/2883664 to your computer and use it in GitHub Desktop.
Very simple phantom types example
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)
^
*/
@pedrofurla
Copy link

I still don't see the phantoms. Oh, maybe they are invisible...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment