@@ -46,7 +46,7 @@ If you want them to be immutable---read only---create them as `val` fields inste
46
46
47
47
Prior to Scala 3, you used the ` new ` keyword to create a new instance of a class:
48
48
49
- {% tabs class_2 class=tabs-scala-version %}
49
+ {% tabs class_2 %}
50
50
{% tab 'Scala 2 and 3' for=class_2 %}
51
51
52
52
``` scala
@@ -59,7 +59,7 @@ val p = new Person("Robert Allen Zimmerman", "Harmonica Player")
59
59
60
60
However, with [ creator applications] [ creator ] this isn’t required in Scala 3:
61
61
62
- {% tabs class_3 class=tabs-scala-version %}
62
+ {% tabs class_3 %}
63
63
{% tab 'Scala 2 and 3' for=class_3 %}
64
64
65
65
``` scala
@@ -71,7 +71,7 @@ val p = Person("Robert Allen Zimmerman", "Harmonica Player")
71
71
72
72
Once you have an instance of a class such as ` p ` , you can access its fields, which in this example are all constructor parameters:
73
73
74
- {% tabs class_4 class=tabs-scala-version %}
74
+ {% tabs class_4 %}
75
75
{% tab 'Scala 2 and 3' for=class_4 %}
76
76
77
77
``` scala
@@ -84,7 +84,7 @@ p.vocation // "Harmonica Player"
84
84
85
85
As mentioned, all of these parameters were created as ` var ` fields, so you can also mutate them:
86
86
87
- {% tabs class_5 class=tabs-scala-version %}
87
+ {% tabs class_5 %}
88
88
{% tab 'Scala 2 and 3' for=class_5 %}
89
89
90
90
``` scala
@@ -184,7 +184,7 @@ class Socket(val timeout: Int = 5_000, val linger: Int = 5_000):
184
184
185
185
A great thing about this feature is that it lets consumers of your code create classes in a variety of different ways, as though the class had alternate constructors:
186
186
187
- {% tabs default-values_2 class=tabs-scala-version %}
187
+ {% tabs default-values_2 %}
188
188
{% tab 'Scala 2 and 3' for=default-values_2 %}
189
189
190
190
``` scala
@@ -201,7 +201,7 @@ val s = Socket(linger = 10_000) // timeout: 5000, linger: 10000
201
201
When creating a new instance of a class, you can also use named parameters.
202
202
This is particularly helpful when many of the parameters have the same type, as shown in this comparison:
203
203
204
- {% tabs default-values_3 class=tabs-scala-version %}
204
+ {% tabs default-values_3 %}
205
205
{% tab 'Scala 2 and 3' for=default-values_3 %}
206
206
207
207
``` scala
@@ -325,7 +325,7 @@ The class has three constructors, given by the numbered comments in the code:
325
325
326
326
Those constructors can be called like this:
327
327
328
- {% tabs structor_2 class=tabs-scala-version %}
328
+ {% tabs structor_2 %}
329
329
{% tab 'Scala 2 and 3' for=structor_2 %}
330
330
331
331
``` scala
@@ -376,7 +376,7 @@ object StringUtils:
376
376
377
377
We can use the object as follows:
378
378
379
- {% tabs object_2 class=tabs-scala-version %}
379
+ {% tabs object_2 %}
380
380
{% tab 'Scala 2 and 3' for=object_2 %}
381
381
382
382
``` scala
@@ -414,7 +414,7 @@ isNullOrEmpty("John Casey") // false
414
414
415
415
or just _ some_ members:
416
416
417
- {% tabs object_4 class=tabs-scala-version %}
417
+ {% tabs object_4 %}
418
418
{% tab 'Scala 2 and 3' for=object_4 %}
419
419
420
420
``` scala
@@ -716,7 +716,7 @@ class IrishSetter(name: String) extends HasLegs, HasTail:
716
716
Notice that the ` IrishSetter ` class implements the abstract members that are defined in ` HasLegs ` and ` HasTail ` .
717
717
Now you can create new ` IrishSetter ` instances:
718
718
719
- {% tabs traits_5 class=tabs-scala-version %}
719
+ {% tabs traits_5 %}
720
720
{% tab 'Scala 2 and 3' for=traits_5 %}
721
721
722
722
``` scala
@@ -835,7 +835,7 @@ Basic enumerations are used to define sets of constants, like the months in a ye
835
835
836
836
As an example, these enumerations define sets of attributes related to pizzas:
837
837
838
- {% tabs enum_1 class=tabs-scala-version %}
838
+ {% tabs enum_1 %}
839
839
{% tab 'Scala 3 only' for=enum_1 %}
840
840
841
841
```scala
@@ -854,7 +854,7 @@ enum Topping:
854
854
855
855
To use them in other code, first import them, and then use them:
856
856
857
- {% tabs enum_2 class=tabs-scala-version %}
857
+ {% tabs enum_2 %}
858
858
{% tab 'Scala 3 only' for=enum_2 %}
859
859
860
860
```scala
@@ -867,7 +867,7 @@ val currentCrustSize = Small
867
867
868
868
Enum values can be compared using equals (`==`), and also enum_1ed on:
869
869
870
- {% tabs enum_3 class=tabs-scala-version %}
870
+ {% tabs enum_3 %}
871
871
{% tab 'Scala 3 only' for=enum_3 %}
872
872
873
873
```scala
@@ -889,7 +889,7 @@ currentCrustSize enum_1
889
889
890
890
Enumerations can also be parameterized:
891
891
892
- {% tabs enum_4 class=tabs-scala-version %}
892
+ {% tabs enum_4 %}
893
893
{% tab 'Scala 3 only' for=enum_4 %}
894
894
895
895
```scala
@@ -904,7 +904,7 @@ enum Color(val rgb: Int):
904
904
905
905
And they can also have members (like fields and methods):
906
906
907
- {% tabs enum_5 class=tabs-scala-version %}
907
+ {% tabs enum_5 %}
908
908
{% tab 'Scala 3 only' for=enum_5 %}
909
909
910
910
```scala
@@ -926,7 +926,7 @@ enum Planet(mass: Double, radius: Double):
926
926
927
927
If you want to use Scala-defined enums as Java enums, you can do so by extending the class `java.lang.Enum` (which is imported by default) as follows:
928
928
929
- {% tabs enum_6 class=tabs-scala-version %}
929
+ {% tabs enum_6 %}
930
930
{% tab 'Scala 3 only' for=enum_6 %}
931
931
932
932
```scala
@@ -953,7 +953,7 @@ The section on [algebraic datatypes][adts] and the [reference documentation][ref
953
953
Case classes are used to model immutable data structures.
954
954
Take the following example:
955
955
956
- {% tabs case-classes_1 class=tabs-scala-version %}
956
+ {% tabs case-classes_1 %}
957
957
{% tab 'Scala 2 and 3' for=case-classes_1 %}
958
958
959
959
```scala:
@@ -966,7 +966,7 @@ case class Person(name: String, relation: String)
966
966
Since we declare `Person` as a case class, the fields `name` and `relation` are public and immutable by default.
967
967
We can create instances of case classes as follows:
968
968
969
- {% tabs case-classes_2 class=tabs-scala-version %}
969
+ {% tabs case-classes_2 %}
970
970
{% tab 'Scala 2 and 3' for=case-classes_2 %}
971
971
972
972
```scala
@@ -978,7 +978,7 @@ val christina = Person("Christina", "niece")
978
978
979
979
Note that the fields can’t be mutated:
980
980
981
- {% tabs case-classes_3 class=tabs-scala-version %}
981
+ {% tabs case-classes_3 %}
982
982
{% tab 'Scala 2 and 3' for=case-classes_3 %}
983
983
984
984
```scala
@@ -1092,7 +1092,7 @@ trait Person:
1092
1092
1093
1093
Then, create these case classes to extend that trait:
1094
1094
1095
- {% tabs case-classes_6 class=tabs-scala-version %}
1095
+ {% tabs case-classes_6 %}
1096
1096
{% tab 'Scala 2 and 3' for=case-classes_6 %}
1097
1097
1098
1098
```scala
@@ -1134,7 +1134,7 @@ def getPrintableString(p: Person): String = p match
1134
1134
1135
1135
Notice these two patterns in the `case` statements:
1136
1136
1137
- {% tabs case-classes_8 class=tabs-scala-version %}
1137
+ {% tabs case-classes_8 %}
1138
1138
{% tab 'Scala 2 and 3' for=case-classes_8 %}
1139
1139
1140
1140
```scala
@@ -1153,7 +1153,7 @@ Technically, the specific type of pattern case-classes_1ing shown in these examp
1153
1153
1154
1154
To show how that code works, create an instance of `Student` and `Teacher`:
1155
1155
1156
- {% tabs case-classes_9 class=tabs-scala-version %}
1156
+ {% tabs case-classes_9 %}
1157
1157
{% tab 'Scala 2 and 3' for=case-classes_9 %}
1158
1158
1159
1159
```scala
@@ -1166,7 +1166,7 @@ val t = Teacher("Bob Donnan", "Mathematics")
1166
1166
1167
1167
Next, this is what the output looks like in the REPL when you call `getPrintableString` with those two instances:
1168
1168
1169
- {% tabs case-classes_10 class=tabs-scala-version %}
1169
+ {% tabs case-classes_10 %}
1170
1170
{% tab 'Scala 2 and 3' for=case-classes_10 %}
1171
1171
1172
1172
```scala
@@ -1253,7 +1253,7 @@ They’re particularly useful whenever you need a singleton object that needs a
1253
1253
Case objects are useful when you need to pass immutable messages around.
1254
1254
For instance, if you’re working on a music player project, you’ll create a set of commands or messages like this:
1255
1255
1256
- {% tabs case-objects_1 class=tabs-scala-version %}
1256
+ {% tabs case-objects_1 %}
1257
1257
{% tab 'Scala 2 and 3' for=case-objects_1 %}
1258
1258
1259
1259
```scala
@@ -1301,4 +1301,4 @@ def handleMessages(message: Message): Unit = message match
1301
1301
[fp-modeling]: {% link _overviews/scala3-book/domain-modeling-fp.md %}
1302
1302
[creator]: {{ site.scala3ref }}/other-new-features/creator-applications.html
1303
1303
[unapply]: {{ site.scala3ref }}/changed-features/pattern-matching.html
1304
- [trait-params]: {{ site.scala3ref }}/other-new-features/trait-parameters.html
1304
+ [trait-params]: {{ site.scala3ref }}/other-new-features/trait-parameters.html
0 commit comments