You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/scala3-book/fun-eta-expansion.md
+36-6Lines changed: 36 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -11,26 +11,36 @@ next-page: fun-hofs
11
11
12
12
When you look at the Scaladoc for the `map` method on Scala collections classes, you see that it’s defined to accept a _function_:
13
13
14
+
{% tabs fun_1 %}
15
+
{% tab 'Scala 2 and 3' for=fun_1 %}
16
+
14
17
```scala
15
18
defmap[B](f: (A) =>B):List[B]
16
19
-----------
17
20
```
18
21
22
+
{% endtab %}
23
+
{% endtabs %}
24
+
19
25
Indeed, the Scaladoc clearly states, “`f` is the _function_ to apply to each element.”
20
26
But despite that, somehow you can pass a _method_ into `map`, and it still works:
21
27
28
+
{% tabs fun_2 %}
29
+
{% tab 'Scala 2 and 3' for=fun_2 %}
30
+
22
31
```scala
23
32
deftimes10(i: Int) = i *10// a method
24
33
List(1, 2, 3).map(times10) // List(10,20,30)
25
34
```
26
35
36
+
{% endtab %}
37
+
{% endtabs %}
38
+
27
39
Have you ever wondered how this works---how you can pass a _method_ into `map`, which expects a _function_?
28
40
29
41
The technology behind this is known as _Eta Expansion_.
30
42
It converts an expression of _method type_ to an equivalent expression of _function type_, and it does so seamlessly and quietly.
31
43
32
-
33
-
34
44
## The differences between methods and functions
35
45
36
46
{% comment %}
@@ -45,18 +55,31 @@ Unlike methods, _functions_ are complete objects themselves, making them first-c
45
55
Their syntax is also different.
46
56
This example shows how to define a method and a function that perform the same task, determining if the given integer is even:
47
57
58
+
{% tabs fun_3 %}
59
+
{% tab 'Scala 2 and 3' for=fun_3 %}
60
+
48
61
```scala
49
62
defisEvenMethod(i: Int) = i %2==0// a method
50
63
valisEvenFunction= (i: Int) => i %2==0// a function
51
64
```
52
65
66
+
{% endtab %}
67
+
{% endtabs %}
68
+
53
69
The function truly is an object, so you can use it just like any other variable, such as putting it in a list:
54
70
71
+
{% tabs fun_4 %}
72
+
{% tab 'Scala 2 and 3' for=fun_4 %}
73
+
55
74
```scala
56
75
valfunctions=List(isEvenFunction)
57
76
```
58
77
59
-
Conversely, a method technically isn’t an object, so in Scala 2 you couldn’t put a method in a `List`, at least not directly, as shown in this example:
78
+
{% endtab %}
79
+
{% endtabs %}
80
+
81
+
{% tabs fun_5 class=tabs-scala-version %}
82
+
{% tab 'Scala 2' for=fun_5 %}
60
83
61
84
```scala
62
85
// this example shows the Scala 2 error message
@@ -67,22 +90,29 @@ Unapplied methods are only converted to functions when a function type is expect
67
90
You can make this conversion explicit by writing `isEvenMethod _` or `isEvenMethod(_)` instead of `isEvenMethod`.
68
91
```
69
92
70
-
As shown in that error message, there is a manual way to convert a method into a function in Scala 2, but the important part for Scala 3 is that the Eta Expansion technology is improved, so now when you attempt to use a method as a variable, it just works---you don’t have to handle the manual conversion yourself:
93
+
Conversely, a method technically isn’t an object, so in Scala 2 you couldn’t put a method in a `List`, at least not directly, as shown in this example:
94
+
95
+
{% endtab %}
96
+
97
+
{% tab 'Scala 3' for=fun_5 %}
71
98
72
99
```scala
73
100
valfunctions=List(isEvenFunction) // works
74
101
valmethods=List(isEvenMethod) // works
75
102
```
76
103
104
+
The important part for Scala 3 is that the Eta Expansion technology is improved, so now when you attempt to use a method as a variable, it just works---you don’t have to handle the manual conversion yourself.
105
+
106
+
{% endtab %}
107
+
{% endtabs %}
108
+
77
109
For the purpose of this introductory book, the important things to know are:
78
110
79
111
- Eta Expansion is the Scala technology that lets you use methods just like functions
80
112
- The technology has been improved in Scala 3 to be almost completely seamless
81
113
82
114
For more details on how this works, see the [Eta Expansion page][eta_expansion] in the Reference documentation.
0 commit comments