Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Last active January 14, 2020 08:51
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 xuwei-k/01766ecb0a9d37e8149e1db7ecd6126b to your computer and use it in GitHub Desktop.
Save xuwei-k/01766ecb0a9d37e8149e1db7ecd6126b to your computer and use it in GitHub Desktop.
Welcome to Scala 2.12.10 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_201).
Type in expressions for evaluation. Or try :help.
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Foo
trait NotFoo[-A]
object NotFoo {
implicit def instance[A]: NotFoo[A] = new NotFoo[A]{}
implicit val ambiguousFooInstance1: NotFoo[Foo] = new NotFoo[Foo]{}
implicit val ambiguousFooInstance2: NotFoo[Foo] = new NotFoo[Foo]{}
}
// Exiting paste mode, now interpreting.
defined trait Foo
defined trait NotFoo
defined object NotFoo
scala> def aaa[A: NotFoo](a: A) = a
aaa: [A](a: A)(implicit evidence$1: NotFoo[A])A
scala> aaa(42)
res0: Int = 42
scala> aaa("z")
res1: String = z
scala> aaa(new Foo{})
<console>:14: error: ambiguous implicit values:
both value ambiguousFooInstance1 in object NotFoo of type => NotFoo[Foo]
and value ambiguousFooInstance2 in object NotFoo of type => NotFoo[Foo]
match expected type NotFoo[Foo]
aaa(new Foo{})
^
scala> class FooSubClass extends Foo
defined class FooSubClass
scala> aaa(new FooSubClass)
<console>:14: error: ambiguous implicit values:
both value ambiguousFooInstance1 in object NotFoo of type => NotFoo[Foo]
and value ambiguousFooInstance2 in object NotFoo of type => NotFoo[Foo]
match expected type NotFoo[FooSubClass]
aaa(new FooSubClass)
^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment