bug icon indicating copy to clipboard operation
bug copied to clipboard

overloading curried function in Scala 2?

Open He-Pin opened this issue 3 years ago • 3 comments

class Test:
 def foo(x: String)(y: Int): Int = ???
 def foo(x: String)(y: Double): Int = ???

This is allowed in Scala 3 , should this be ported to Scala 2?

class Test {
  def foo(x: String)(y: Int): Int = ???
  def foo(x: String)(y: Double): Int = ???
}

Is not allowed in Scala 2.

He-Pin avatar Apr 25 '22 03:04 He-Pin

The failure comes at the use site:

scala 2.13.8> (new Test).foo("foo")(3)
                         ^
              error: ambiguous reference to overloaded definition,
              both method foo in class Test of type (x: String)(y: Double): Int
              and  method foo in class Test of type (x: String)(y: Int): Int
              match argument types (String)

I haven't studied this, but offhand, it doesn't seem like the kind of change we're making anymore in Scala 2. Has anyone looked into what it would take to change it...?

SethTisue avatar Apr 25 '22 15:04 SethTisue

I commented that the motivation according to the ref was extension methods (where the first param list is just the receiver). I am curious whether there is interaction with type interference. Also this would surely be a -Xsource:3.

som-snytt avatar Apr 25 '22 15:04 som-snytt

I don't know how Scala 3 does it - would be interesting if someone can summarize :) As far as I can tell, type checking (and overload resolution as part of it) in Scala 2 works one param list at the time, so it might be quite a deep change.

lrytz avatar Apr 26 '22 06:04 lrytz