Skip to content

Commit ce8d979

Browse files
authored
add code tabs to num30 (#2644)
* add code tabs to num30 * correct for errors. * finally corrected mistakes. * correct sentence according to review.
1 parent aaa9a2d commit ce8d979

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

_overviews/scala3-book/fun-eta-expansion.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,36 @@ next-page: fun-hofs
1111

1212
When you look at the Scaladoc for the `map` method on Scala collections classes, you see that it’s defined to accept a _function_:
1313

14+
{% tabs fun_1 %}
15+
{% tab 'Scala 2 and 3' for=fun_1 %}
16+
1417
```scala
1518
def map[B](f: (A) => B): List[B]
1619
-----------
1720
```
1821

22+
{% endtab %}
23+
{% endtabs %}
24+
1925
Indeed, the Scaladoc clearly states, “`f` is the _function_ to apply to each element.”
2026
But despite that, somehow you can pass a _method_ into `map`, and it still works:
2127

28+
{% tabs fun_2 %}
29+
{% tab 'Scala 2 and 3' for=fun_2 %}
30+
2231
```scala
2332
def times10(i: Int) = i * 10 // a method
2433
List(1, 2, 3).map(times10) // List(10,20,30)
2534
```
2635

36+
{% endtab %}
37+
{% endtabs %}
38+
2739
Have you ever wondered how this works---how you can pass a _method_ into `map`, which expects a _function_?
2840

2941
The technology behind this is known as _Eta Expansion_.
3042
It converts an expression of _method type_ to an equivalent expression of _function type_, and it does so seamlessly and quietly.
3143

32-
33-
3444
## The differences between methods and functions
3545

3646
{% comment %}
@@ -45,18 +55,31 @@ Unlike methods, _functions_ are complete objects themselves, making them first-c
4555
Their syntax is also different.
4656
This example shows how to define a method and a function that perform the same task, determining if the given integer is even:
4757

58+
{% tabs fun_3 %}
59+
{% tab 'Scala 2 and 3' for=fun_3 %}
60+
4861
```scala
4962
def isEvenMethod(i: Int) = i % 2 == 0 // a method
5063
val isEvenFunction = (i: Int) => i % 2 == 0 // a function
5164
```
5265

66+
{% endtab %}
67+
{% endtabs %}
68+
5369
The function truly is an object, so you can use it just like any other variable, such as putting it in a list:
5470

71+
{% tabs fun_4 %}
72+
{% tab 'Scala 2 and 3' for=fun_4 %}
73+
5574
```scala
5675
val functions = List(isEvenFunction)
5776
```
5877

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 %}
6083

6184
```scala
6285
// 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
6790
You can make this conversion explicit by writing `isEvenMethod _` or `isEvenMethod(_)` instead of `isEvenMethod`.
6891
```
6992

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 %}
7198

7299
```scala
73100
val functions = List(isEvenFunction) // works
74101
val methods = List(isEvenMethod) // works
75102
```
76103

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+
77109
For the purpose of this introductory book, the important things to know are:
78110

79111
- Eta Expansion is the Scala technology that lets you use methods just like functions
80112
- The technology has been improved in Scala 3 to be almost completely seamless
81113

82114
For more details on how this works, see the [Eta Expansion page][eta_expansion] in the Reference documentation.
83115

84-
85-
86116
[eta_expansion]: {{ site.scala3ref }}/changed-features/eta-expansion.html
87117
[extension]: {% link _overviews/scala3-book/ca-extension-methods.md %}
88118
[toplevel]: {% link _overviews/scala3-book/taste-toplevel-definitions.md %}

0 commit comments

Comments
 (0)