Skip to content

Instantly share code, notes, and snippets.

@milessabin
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save milessabin/88a21b1f2c6ec37ab6a7 to your computer and use it in GitHub Desktop.

Select an option

Save milessabin/88a21b1f2c6ec37ab6a7 to your computer and use it in GitHub Desktop.
Deriving path-dependent type equality from value identity (in turn proved by singleton type inhabitation). For context see: https://gist.github.com/copumpkin/cb26bb921bbb81183299
scala> trait Foo { type T; val t: T }
defined trait Foo
scala> val x = new Foo { type T = Int ; val t = 23 }
x: Foo{type T = Int} = $anon$1@5aeea69b
scala> val y = new Foo { type T = String ; val t = "foo" }
y: Foo{type T = String} = $anon$1@7d7902b8
scala> def compare(a: Foo, b: Foo): Option[b.T] = a match { case a2: b.type => Some(a2.t) ; case _ => None }
compare: (a: Foo, b: Foo)Option[b.T]
scala> compare(x, y)
res0: Option[y.T] = None
scala> compare(y, x)
res1: Option[x.T] = None
scala> compare(y, y)
res2: Option[y.T] = Some(foo)
scala> compare(x, x)
res3: Option[x.T] = Some(23)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment