0

I have a code like the following:

implicit val _ = new MyClass()

And I am getting the following error:

Implicit pattern definition binds no variables

Why?

I am using Scala -> 2.13.3, SBT -> 1.3.13, and Java -> OpenJDK v14.0.2

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
himanshuIIITian
  • 5,985
  • 6
  • 50
  • 70
  • 3
    Related [Create unnamed implicit class or function](https://stackoverflow.com/questions/35758131/create-unnamed-implicit-class-or-function) – Mario Galic Aug 21 '20 at 11:37

2 Answers2

5

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]
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
4

In Scala 3 (Dotty) we could provide unnamed implicit value like so

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
Mario Galic
  • 47,285
  • 6
  • 56
  • 98