Skip to content

Instantly share code, notes, and snippets.

@odersky
Created February 22, 2017 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odersky/040b88501bac8cfd6cbbd856b0836ae3 to your computer and use it in GitHub Desktop.
Save odersky/040b88501bac8cfd6cbbd856b0836ae3 to your computer and use it in GitHub Desktop.
Phantom Types
object Phantom {
type Nothing <: Any
type Any
def assume[T <: Any]: T
class Function1[-T <: Any, +R <: Any] {
def apply(x: T): R
}
class ImplicitFunction1[-T <: Any, +R <: Any] extends Function1[T, R] {
def apply(implicit x: T): R
}
... same for other arities ...
}
object X {
type CanScare[Int] <: Phantom.Any
def canScare: CanScare[Int] = Phantom.assume[CanScare[Int]]
}
Rules:
No mixing of phantom and non-phantom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment