Skip to content

Method overload with context function single argument vs varargs #16597

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

Open
prolativ opened this issue Dec 29, 2022 · 1 comment
Open

Method overload with context function single argument vs varargs #16597

prolativ opened this issue Dec 29, 2022 · 1 comment

Comments

@prolativ
Copy link
Contributor

Compiler version

3.3.0-RC1-bin-20221224-6f5bb34-NIGHTLY

Minimized code

//> using scala "3.3.0-RC1-bin-20221224-6f5bb34-NIGHTLY"

trait Ctx

def bar(f1: Ctx => Int) = ???
def bar(f1: Ctx => Int, f2: Ctx => Int) = ???

def baz(f1: Ctx ?=> Int) = ???
def baz(f1: Ctx ?=> Int, f2: Ctx ?=> Int) = ???

def qux(f1: Ctx => Int) = ???
def qux(fs: (Ctx => Int)*) = ???

def quz(f1: Ctx ?=> Int) = ???
def quz(fs: (Ctx ?=> Int)*) = ???

val a1 = bar(_ => 1)
val a2 = bar(_ => 1, _ => 2)

val b1 = baz(_ ?=> 1)
val b2 = baz(_ ?=> 1, _ ?=> 2)

val c1 = baz(1)
val c2 = baz(1, 2)

val d1 = qux(_ => 1)
val d2 = qux(_ => 1, _ => 2)

val e1 = quz(_ ?=> 1)
val e2 = quz(_ ?=> 1, _ ?=> 2)

val f1 = quz(1)
val f2 = quz(1, 2)

Output

[error] ./Overload.scala:29:10: None of the overloaded alternatives of method quz with types
[error]  (fs: ((Ctx) ?=> Int)*): Nothing
[error]  (f1: (Ctx) ?=> Int): Nothing
[error] match arguments (<?> => <?>)
[error] val e1 = quz(_ ?=> 1)
[error]          ^^^
[error] ./Overload.scala:32:10: None of the overloaded alternatives of method quz with types
[error]  (fs: ((Ctx) ?=> Int)*): Nothing
[error]  (f1: (Ctx) ?=> Int): Nothing
[error] match arguments ((1 : Int))
[error] val f1 = quz(1)
[error]          ^^^

Expectation

The snippet should compile.

Following the discussion in #16511 I understand that in case of method overloading we cannot rely on the actual type of method parameters to distinguish between two variants of the same method when context functions are involved. However we should be able to use the number of parameters instead and normally the compiler has no problem with that unless it has to deal with context functions and varargs vs a single argument at the same time.

I would also expect the PR linked above to have fixed case e1 as an explicit context lambda is used there but unfortunately it didn't solve the problem.

@prolativ prolativ added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 29, 2022
@Kordyjan Kordyjan added this to the Future versions milestone Jan 13, 2023
@prolativ
Copy link
Contributor Author

prolativ commented Mar 8, 2023

Workaround: overload separately for 0, 1 and >=2 arguments

def quz() = ???
def quz(f1: Ctx ?=> Int) = ???
def quz(f1: Ctx ?=> Int, f2: Ctx ?=> Int, fs: (Ctx ?=> Int)*) = ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants