question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Eta-expansion doesn't insert default arguments

See original GitHub issue

This fails in Scalac and Dotty, but seems a bit silly, especially because f _ doesn’t get defaults but f(_) does:

scala> def f(a: Int, b: Boolean = true): Int = a
f: (a: Int, b: Boolean)Int

scala> f: (Int => Int)
<console>:13: error: type mismatch;
 found   : (Int, Boolean) => Int
 required: Int => Int
       f: (Int => Int)
       ^
cala> (f _): (Int => Int)
1 |(f _): (Int => Int)
  |  ^
  |  found:    (Int, Boolean) => Int
  |  required: Int => Int
  |
scala> (f(_)): (Int => Int)
val res0: Int => Int = Lambda$1550/925936450@537c8c7e

This came up in https://github.com/lampepfl/dotty/pull/4276/commits/23464fd64e3a4d1e3c2c2635c289543690d70a5e#diff-b59d25461c7203c18a90d6373be182cdL541

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
LPTKcommented, Oct 7, 2018

Following TypeScript, the type of expression f (or f _ for that matter) should be (a: Int, b?: Boolean) => Int where the ? in b? means the parameter is optional; and this would be a subtype of Int => Int.

In TypeScript:

function f(a: number, b: boolean = true): number { return a }
let g: (x: number, y?: boolean) => number = f
let h: (_:number) => number = true ? f : g

IMHO this is yet another example of how full-featured function types – which can properly reflect all method types – would make everything easier and more regular in Scala, the same way they do in TypeScript.

0reactions
OlivierBlanvillaincommented, Mar 19, 2019

I will close this as it’s non actionable in it’s current state. It sounds like it would require non trivial changes (the addition of default-parameterized function types?) and would likely be a breaking change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scala - Currying and default arguments - Stack Overflow
The result of the eta expansion is a Function object, which does not carry any argument names with it (as it is possible...
Read more >
Automatic Eta Expansion - More Details - Scala 3
If m has an argument list with one or more parameters, we always eta-expand · If m is has an empty argument list...
Read more >
Insert to a table using function with default arguments
The idea is to make only one argument mandatory and other two optional. The function listed below works well for all three inputs...
Read more >
Record Types — Agda 2.6.2.2 documentation
Inductive records have eta-equality on by default, while no-eta-equality is the default for coinductive records. In fact, the eta-equality and coinductive ...
Read more >
Expressions | Scala 2.13
If the application uses named or default arguments the following transformation is applied to convert it into an application without named or default ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found