Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

position of using clause prevents overloaded extension method being picked #11713

Closed
rjolly opened this issue Mar 12, 2021 · 6 comments · Fixed by #15636
Closed

position of using clause prevents overloaded extension method being picked #11713

rjolly opened this issue Mar 12, 2021 · 6 comments · Fixed by #15636
Assignees
Milestone

Comments

@rjolly
Copy link
Contributor

rjolly commented Mar 12, 2021

Compiler version

3.0.0-RC1

Minimized code and output

extension [T1](x: T1)(using Numeric[T1])
  def combine[T2](y: T2)(using Numeric[T2]) = ???
  def combine(y: String) = ???

val res = 100.combine(200)
//        ^^^^^^^^^^^
//    value combine is not a member of Int.
//    An extension method was tried, but could not be fully constructed:
//
//        combine()    failed with
//
//            value combine: <overloaded combine> does not take parameters

Expectation : should work, as when we move using to the end of the parameter list:

extension [T1](x: T1)
  def combine[T2](y: T2)(using Numeric[T2], Numeric[T1]) = ???
  def combine(y: String) = ???

val res = 100.combine(200)
@rjolly rjolly changed the title Extension method with overload not picked depending of using clause placement Overloaded extension not picked depending of using clause placement Mar 12, 2021
@bishabosha bishabosha changed the title Overloaded extension not picked depending of using clause placement position of using clause prevents overloaded extension method being picked Mar 12, 2021
@odersky
Copy link
Contributor

odersky commented Mar 14, 2021

That will be quite hard to do. Overloading resolution is super hard as it is. This would make it worse.

@rjolly
Copy link
Contributor Author

rjolly commented Mar 14, 2021

Is its interaction with implicit resolution documented somewhere?

@rjolly
Copy link
Contributor Author

rjolly commented Mar 15, 2021

What's disturbing is that with non-extensions it succeeds:

def combine[T1,T2](x: T1)(using Numeric[T1])(y: T2)(using Numeric[T2]) = ???
def combine[T1](x: T1)(using Numeric[T1])(y: String) = ???

val res = combine(100)(200)

So it seems we're not far from the goal.

@rjolly
Copy link
Contributor Author

rjolly commented Mar 16, 2021

Answering to myself : https://dotty.epfl.ch/docs/reference/changed-features/implicit-resolution.html

  1. The rule for picking a most specific alternative among a set of overloaded or implicit alternatives is refined to take context parameters into account. All else being equal, an alternative that takes some context parameters is taken to be less specific than an alternative that takes none. If both alternatives take context parameters, we try to choose between them as if they were methods with regular parameters. The following paragraph in the SLS §6.26.3 is affected by this change:

Original version:

An alternative A is more specific than an alternative B if the relative weight of A over B is greater than the relative weight of B over A.

Modified version:

An alternative A is more specific than an alternative B if

the relative weight of A over B is greater than the relative weight of B over A, or
the relative weights are the same, and A takes no implicit parameters but B does, or
the relative weights are the same, both A and B take implicit parameters, and A is more specific than B if all implicit parameters in either alternative are replaced by regular parameters.

It says nothing about the position of argument lists, probably because it predates multiple implicit argument lists, and the fact that these had to be at the end(?)

@YourPsychiatrist
Copy link
Contributor

I think I found a similar occurrence:

extension [T1] (x: T1)
  def f(fn: T1 ?=> Unit) =
    fn(using x)
  def f(s: String) = 
    s

// just yields "Hello"
val a = 100.f("Hello")

// error, 100.f() was considered yada yada
val res = 100.f { 
  println("wat")
}

I am guessing that is because A ?=> B compiles to A => B with using clause attached, but that's really just a wild guess. Would love to look into it, though. Of course, only in case the consensus is that the current state is not intended behaviour and overload resolution should pick the correct (the intended, that is) function.

@som-snytt
Copy link
Contributor

Also extension with overload, using: #13668

@griggt griggt self-assigned this Jul 8, 2022
griggt added a commit to griggt/dotty that referenced this issue Jul 9, 2022
In some cases while going deeper into alternatives during overload resolution,
we may end up with a nested polytype after dropping contextual parameters.

In particular this can happen for an extension with a `using` clause, as seen
in tests/pos/i11713.scala and tests/pos/i13668.scala.

The overload applicability test fails here unless the type parameter lists
are merged.

Co-authored-by: Gagandeep Kalra <[email protected]>
Co-authored-by: Mark T. Kennedy <[email protected]>

Fixes scala#11713
Fixes scala#13668
@Kordyjan Kordyjan added this to the 3.2.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants