Skip to content

Instantly share code, notes, and snippets.

@lyricallogical
Created January 17, 2012 14:03
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 lyricallogical/1626723 to your computer and use it in GitHub Desktop.
Save lyricallogical/1626723 to your computer and use it in GitHub Desktop.
imihu
$ scala
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_29).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait A[T] { def t : T }
defined trait A
scala> trait B extends A[Int]
defined trait B
scala> def f[C[_] <: A[_]](a : C[_]) = a
f: [C[_] <: A[_]](a: C[_])C[_]
scala> f(new B { def t = 1 } : B)
res0: A[_] = $anon$1@b1cb25
scala> f(new B { def t = 1 })
res1: A[_] = $anon$1@10010ec
scala> def g[T, C[T] <: A[T]](a : C[T]) = a
g: [T, C[T] <: A[T]](a: C[T])C[T]
scala> g(new B { def t = 1 } : B)
res2: A[Int] = $anon$1@1ac7fbb
scala> g(new B { def t = 1 })
<console>:11: error: type mismatch;
found : java.lang.Object with B
required: ?C[?T]
Note that implicit conversions are not applicable because they are ambiguous:
both method any2Ensuring in object Predef of type [A](x: A)Ensuring[A]
and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
are possible conversion functions from java.lang.Object with B to ?C[?T]
g(new B { def t = 1 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment