scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Implicit val conversion precedence and absence of warning #10392

Closed Dveim closed 1 year ago

Dveim commented 7 years ago

Given implicit val val_int2str = (i: Int) => {println("val");i.toString} and implicit def def_int2str(i: Int) = {println("def");i.toString}, 1) there is no warning about "please enable implicits explicitly" for val 2) val always has precedence over def, no collision happens

SethTisue commented 1 year ago

As I understand it, point 1 is intentional (though arguably unfortunate). Scala 3 clamps down, as per https://docs.scala-lang.org/scala3/guides/migration/incompat-contextual-abstractions.html :

Scala 3 does not support implicit conversion from an implicit function value, of the form implicit val ev: A => B

Not sure about point 2, but at this point, it seems unlikely we'd mess with it.

som-snytt commented 1 year ago

The Scaladoc is wrong when it says that the warning "governs" vals, implying vals typed as Function1 but not other vals.

The confusing part is that other types may be function types:

class C {
  implicit val cv0: List[String] = List("zero", "one", "two", "three")
  //implicit val cv1: Int => String = _.toString
  implicit def cv2(i: Int): String = i.toString * 2

  def f(i: Int): String = i
}

object Test extends App {
  val c = new C
  println {
    c.f(3)
  }
}