Questions tagged [eta-expansion]

Use this tag for questions addressing eta expansion in Scala. Also add the Scala-tag to your question

Eta Expansion is part of the language . There is a well-answered question on SO explaining eta expansion. It refers also to the docs which explain:

The expression e _ is well-formed if e is of method type or if e is a call-by-name parameter. If e is a method with parameters, e _ represents e converted to a function type by eta expansion. If e is a parameterless method or call-by-name parameter of type =>T, e _ represents the function of type () => T, which evaluates e

11 questions
40
votes
2 answers

What is the eta expansion in Scala?

I am new to Scala. I just heard the term "eta expansion" and roughly know that it means to expand a method to a function object. But I find few resources in SO that systematically introduce it. I am curious about how eta expansion works in Scala.…
Lifu Huang
  • 11,930
  • 14
  • 55
  • 77
9
votes
2 answers

Partial function application prematurely runs codeblock when used with underscore

Given: def save(f: => Any)(run:Boolean) { if (run) { println("running f"); f } else println("not running f") } I can call it with: save("test")(true) -> running f save("test")(false) -> not running f save(throw new…
ssanj
  • 2,169
  • 3
  • 19
  • 28
4
votes
0 answers

Why eta-expansion of polymorphic method does not result in polymorphic function value?

Scala 2 does not have polymorphic function values so eta-expanding polymorphic methods gives only scala> def f[A](a: A): A = ??? def f[A](a: A): A scala> f _ val res0: Nothing => Nothing = $Lambda$7757/1502613782@45af2c1 However Scala 3 does have…
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
3
votes
1 answer

In Scala, what are the rules for making closures with underscores?

At first I had believed that using underscores to make closures (e.g. println _) were just shorthand for using an arrow (e.g. x => println x), but I just recently learned that you can also do the following: def f(a: Int, b: Int) = a + 2 * b List(1,…
math4tots
  • 8,540
  • 14
  • 58
  • 95
2
votes
0 answers

How to eta-convert tuple of refinement-typed coordinates?

In the following Liquid Haskell program, the definition of c' and z' are separate. This program is accepted by the LH typechecker. {-@ type Digit = { v : _ | 0 <= v && v <= 9 } @-} type Digit = Int {-@ addDigit :: c : Bool -> x : Digit -> y :…
Cactus
  • 27,075
  • 9
  • 69
  • 149
2
votes
2 answers

What is the difference between foreach(println) and foreach(println())?

I have a string array: val str:Array[String] = Array("aa","bb") scala> str.foreach(println) // works aa bb scala> str.foreach(println()) // println() also returns a Unit, doesn't it? ^ error: type mismatch; found :…
Julia Chang
  • 181
  • 1
  • 8
1
vote
1 answer

Haskell - Eta reduction and Eta expansion

I've been studying functional program optimization, and have been digging into the GHC source. I (mostly) understand what eta reduction and eta expansion are. Eta reduction only removes redundant lambdas: \x -> abs x => abs Eta expansion is the…
1
vote
1 answer

Specify the method signature of the method to apply the eta expansion

Is there a way to specify the signature of the method to which I want to apply the eta expansion? For example: val tupleNum = (1L,2L) case class CaseClass(a:String, b:String) object CaseClass { def apply(a: Long, b: Long): CaseClass = new…
angelcervera
  • 3,699
  • 1
  • 40
  • 68
0
votes
0 answers

In Scala 3.2, what's the most efficient method for eta-expanding a function or a class constructor with names and default argument(s)?

During my involvement of an project that heavily relies on type-checked binding with Schematic data. I found many of the existing code share the following pattern: case class Datum1( col1: String = "default", col2: Double = 0 ... (over 40…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
0
votes
1 answer

Why can a scala function name be used where a function value is expected, when it is not a function value itself?

def f(x: Int): Boolean = (x >= 0 && x < 4) List(1, 3, 5).map(f) // List(true, true, false) f // does not compile Why can f be used where a function value is expected, even if it is not a function value itself?
0
votes
3 answers

Calling an anonymous function inside a map method

I was making a constructor with multiple possible arguments, when I realized my IDE was pointing out a type discrepancy: case class PathAndColumns(path: String, column: Array[Column]) { def this(path: String, column: Column) { this(path,…
Juliosor
  • 131
  • 1
  • 8