Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@SethTisue
Last active August 29, 2015 13:59
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 SethTisue/10738540 to your computer and use it in GitHub Desktop.
Save SethTisue/10738540 to your computer and use it in GitHub Desktop.
// method bodies in Foo are just dummies
scala> class Foo[T] { def parse(s: String): T = null.asInstanceOf[T] ; def format(t: T): String = ""}
defined class Foo
scala> class C; class D
defined class C
defined class D
scala> val foos = List[Foo[_]](new Foo[C], new Foo[D])
foos: List[Foo[_]] = List(Foo@6f36c2f0, Foo@f58853c)
// try #1: doesn't work, existential type fail
scala> val foo = foos(0)
foo: Foo[_] = Foo@6f36c2f0
scala> foo.format(foo.parse(""))
<console>:13: error: type mismatch;
found : (some other)_1(in value foo)
required: _1(in value foo)
foo.format(foo.parse(""))
^
// try #2: works (but why?)
scala> foos(0) match { case foo => foo.format(foo.parse("")) }
res1: String = ""
// try #3: also works (path-dependent type win?)
scala> abstract class Foo { type T; def parse(s: String): T = null.asInstanceOf[T] ; def format(t: T): String = ""}
defined class Foo
scala> val foos = List[Foo](new Foo { type T = C }, new Foo { type T = D })
foos: List[Foo] = List($anon$1@15d49048, $anon$2@7098b907)
scala> val foo = foos(0)
foo: Foo = $anon$1@15d49048
scala> foo.format(foo.parse(""))
res4: String = ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment