Skip to content

Instantly share code, notes, and snippets.

@milessabin
Created May 19, 2016 13:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milessabin/29aaa36a1587c5a8f2fe2ec075715a76 to your computer and use it in GitHub Desktop.
Save milessabin/29aaa36a1587c5a8f2fe2ec075715a76 to your computer and use it in GitHub Desktop.
Doing something useful with bare singleton types and no macros ...
scala> trait Label[T] { val value: Boolean }
defined trait Label
scala> def mkInstance(s: String, v: Boolean): Label[s.type] = new Label[s.type] { val value = v }
mkInstance: (s: String, v: Boolean)Label[s.type]
scala> implicit def lTrue = mkInstance("True", true)
lTrue: Label[String("True")]
scala> implicit def lFalse = mkInstance("False", false)
lFalse: Label[String("False")]
scala> def label(s: String)(implicit ls: Label[s.type]): Boolean = ls.value
label: (s: String)(implicit ls: Label[s.type])Boolean
scala> label("True")
res0: Boolean = true
scala> label("False")
res1: Boolean = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment