使用下划线作为标识符时,隐式模式定义不绑定任何变量

Implicit pattern definition binds no variables when using underscore as identifier

我有如下代码:

implicit val _ = new MyClass()

我收到以下错误:

Implicit pattern definition binds no variables

为什么?

我正在使用 Scala -> 2.13.3、SBT -> 1.3.13 和 Java -> OpenJDK v14.0.2

https://github.com/scala/scala/pull/8699

https://github.com/scala/bug/issues/11618

If a pattern definition binds no variables, it is probably a mistake if it is marked implicit, because it introduces no implicit values, or if it is a template statement, because it accidentally introduces a template member.

Please warn on:

implicit val _ = 42
implicitly[Int]

在 Scala 3 (Dotty) 中,我们可以像这样提供未命名的隐式值

scala> class MyClass(val x: Int)
// defined class MyClass

scala> given MyClass(41)
// defined object given_MyClass

scala> summon[MyClass].x + 1
val res0: Int = 42