scala / bug

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

No bound inferred in method invocation, therefore no conversions are applied #12750

Open som-snytt opened 1 year ago

som-snytt commented 1 year ago

Reproduction steps

Scala version: 2.13.10

scala> def b[A <: Byte](a: A): a.type = a
def b[A <: Byte](a: A): a.type

scala> b(52)
       ^
       error: inferred type arguments [Int] do not conform to method b's type parameter bounds [A <: Byte]
         ^
       error: type mismatch;
        found   : Int(52)
        required: A

Problem

Explain how the above behavior isn't what you expected.

It's not about me and my expectations. b expects a Byte, so 52 ought to oblige.

SethTisue commented 1 year ago

are you claiming this isn't behaving as spec'ed.... or you're suggesting a spec change?

som-snytt commented 1 year ago

The spec is that when the expected type is Byte, the lexeme with digits is a Byte, not Int.

Actually, Scala 2 does not inject any conversion in this case. b works in Scala 3 only. This change would probably go in the doc page that is still under construction, the one that invites you to watch the old smarter video. Also it's a wonder more people don't complain about Scala 3 ignoring wildcard import of language features. Dotty is like that quirky roommate who wears thin.

class C {
  def b[A <: Byte](a: A) = "hi"
  def f(x: Byte) = "hi"
}

class D {
  val c = new C
  val x = "123"
  import language._
  import language.implicitConversions
  implicit def cv(s: String): Byte = java.lang.Byte.parseByte(s)
  def test() = {
    println {(
      c.b(x),
      c.f(x),
    )}
  }
}

object Test extends App {
  new D().test()
}
som-snytt commented 1 year ago

Also the issue template needs to add:

Are you sure this is a bug or did you just have a question?