lampepfl / dotty-feature-requests

Historical feature requests. Please create new feature requests at https://github.com/lampepfl/dotty/discussions/new?category=feature-requests
31 stars 2 forks source link

Allow eta-expansion for creator applications #232

Closed neko-kai closed 1 year ago

neko-kai commented 3 years ago

Compiler version

3.0.1

Minimized code

class Pair(a: Int, b: Int)
case class CasePair(a: Int, b: Int)

def a[A](f: (Int, Int) => A) = f(1, 2)

def xa = println(a(Pair.apply)) // boom
def xb = println(a(Pair.apply _)) // boom
def xc = println(a(Pair)) // boom

def ya = println(a(CasePair.apply)) // fine
def yb = println(a(CasePair.apply _)) // fine
def yc = println(a(CasePair)) // fine

@main def main = { xa; xb; xc; ya; yb; yc }

Output

def xa = println(a(Pair.apply)) // boom
// missing arguments for method apply in object Pair

def xb = println(a(Pair.apply _)) // boom
// Only function types can be followed by _ but the current expression has type Pair

def xc = println(a(Pair)) // boom
// Found:    Pair.type
// Required: (Int, Int) => Any

Expectation

Expected implicit apply to eta-expand, which would allow using a class's constructor as a function without having to supply (and later change) a placeholder for each constructor parameter, as in Pair(_, _) (prior discussion/motivation in https://contributors.scala-lang.org/t/let-constructors-to-be-considered-as-functions/3798)

odersky commented 3 years ago

That would be a language enhancement and a quite complicated one. I think this should go to feature suggestions, and ideally be accompanied with a PR.

neko-kai commented 3 years ago

Could anyone please transfer this issue to https://github.com/lampepfl/dotty-feature-requests/ in that case? If I open a new issue there, it would be identical to this one or refer to it, so it would probably better to avoid duplication.

bishabosha commented 3 years ago

now a feature request