@@ -30,14 +30,12 @@ Expressions are computable statements:
30
30
```
31
31
You can output the results of expressions using ` println ` :
32
32
33
- {% scalafiddle %}
34
33
``` scala mdoc
35
34
println(1 ) // 1
36
35
println(1 + 1 ) // 2
37
36
println(" Hello!" ) // Hello!
38
37
println(" Hello," + " world!" ) // Hello, world!
39
38
```
40
- {% endscalafiddle %}
41
39
42
40
### Values
43
41
@@ -109,21 +107,17 @@ On the left of `=>` is a list of parameters. On the right is an expression invol
109
107
110
108
You can also name functions:
111
109
112
- {% scalafiddle %}
113
110
``` scala mdoc
114
111
val addOne = (x : Int ) => x + 1
115
112
println(addOne(1 )) // 2
116
113
```
117
- {% endscalafiddle %}
118
114
119
115
A function can have multiple parameters:
120
116
121
- {% scalafiddle %}
122
117
``` scala mdoc
123
118
val add = (x : Int , y : Int ) => x + y
124
119
println(add(1 , 2 )) // 3
125
120
```
126
- {% endscalafiddle %}
127
121
128
122
Or it can have no parameters at all:
129
123
@@ -138,23 +132,19 @@ Methods look and behave very similar to functions, but there are a few key diffe
138
132
139
133
Methods are defined with the ` def ` keyword. ` def ` is followed by a name, parameter list(s), a return type, and a body:
140
134
141
- {% scalafiddle %}
142
135
``` scala mdoc:nest
143
136
def add (x : Int , y : Int ): Int = x + y
144
137
println(add(1 , 2 )) // 3
145
138
```
146
- {% endscalafiddle %}
147
139
148
140
Notice how the return type ` Int ` is declared _ after_ the parameter list and a ` : ` .
149
141
150
142
A method can take multiple parameter lists:
151
143
152
- {% scalafiddle %}
153
144
``` scala mdoc
154
145
def addThenMultiply (x : Int , y : Int )(multiplier : Int ): Int = (x + y) * multiplier
155
146
println(addThenMultiply(1 , 2 )(3 )) // 9
156
147
```
157
- {% endscalafiddle %}
158
148
159
149
Or no parameter lists at all:
160
150
@@ -167,36 +157,78 @@ There are some other differences, but for now, you can think of methods as somet
167
157
168
158
Methods can have multi-line expressions as well:
169
159
170
- {% scalafiddle %}
160
+ {% tabs get-square-string class=tabs-scala-version %}
161
+
162
+ {% tab 'Scala 2' for=get-square-string %}
171
163
``` scala mdoc
172
164
def getSquareString (input : Double ): String = {
173
165
val square = input * input
174
166
square.toString
175
167
}
176
168
println(getSquareString(2.5 )) // 6.25
177
169
```
178
- {% endscalafiddle %}
170
+ {% endtab %}
171
+
172
+ {% tab 'Scala 3' for=get-square-string %}
173
+ ``` scala
174
+ def getSquareString (input : Double ): String =
175
+ val square = input * input
176
+ square.toString
177
+
178
+ println(getSquareString(2.5 )) // 6.25
179
+ ```
180
+ {% endtab %}
181
+
182
+ {% endtabs %}
179
183
180
184
The last expression in the body is the method's return value. (Scala does have a ` return ` keyword, but it is rarely used.)
181
185
182
186
## Classes
183
187
184
188
You can define classes with the ` class ` keyword, followed by its name and constructor parameters:
185
189
190
+ {% tabs greeter-definition class=tabs-scala-version %}
191
+
192
+ {% tab 'Scala 2' for=greeter-definition %}
186
193
``` scala mdoc
187
194
class Greeter (prefix : String , suffix : String ) {
188
195
def greet (name : String ): Unit =
189
196
println(prefix + name + suffix)
190
197
}
191
198
```
199
+ {% endtab %}
200
+
201
+ {% tab 'Scala 3' for=greeter-definition %}
202
+ ``` scala
203
+ class Greeter (prefix : String , suffix : String ):
204
+ def greet (name : String ): Unit =
205
+ println(prefix + name + suffix)
206
+ ```
207
+ {% endtab %}
208
+
209
+ {% endtabs %}
210
+
192
211
The return type of the method ` greet ` is ` Unit ` , which signifies that there is nothing meaningful to return. It is used similarly to ` void ` in Java and C. (A difference is that, because every Scala expression must have some value, there is actually a singleton value of type Unit, written (). It carries no information.)
193
212
194
213
You can make an instance of a class with the ` new ` keyword:
195
214
215
+ {% tabs greeter-usage class=tabs-scala-version %}
216
+
217
+ {% tab 'Scala 2' for=greeter-usage %}
196
218
``` scala mdoc:nest
197
219
val greeter = new Greeter (" Hello, " , " !" )
198
220
greeter.greet(" Scala developer" ) // Hello, Scala developer!
199
221
```
222
+ {% endtab %}
223
+
224
+ {% tab 'Scala 3' for=greeter-usage %}
225
+ ``` scala
226
+ val greeter = Greeter (" Hello, " , " !" )
227
+ greeter.greet(" Scala developer" ) // Hello, Scala developer!
228
+ ```
229
+ {% endtab %}
230
+
231
+ {% endtabs %}
200
232
201
233
We will cover classes in depth [ later] ( classes.html ) .
202
234
@@ -220,6 +252,9 @@ val yetAnotherPoint = Point(2, 2)
220
252
221
253
Instances of case classes are compared by value, not by reference:
222
254
255
+ {% tabs compare-case-class-equality class=tabs-scala-version %}
256
+
257
+ {% tab 'Scala 2' for=compare-case-class-equality %}
223
258
``` scala mdoc
224
259
if (point == anotherPoint) {
225
260
println(s " $point and $anotherPoint are the same. " )
@@ -233,6 +268,25 @@ if (point == yetAnotherPoint) {
233
268
println(s " $point and $yetAnotherPoint are different. " )
234
269
} // Point(1,2) and Point(2,2) are different.
235
270
```
271
+ {% endtab %}
272
+
273
+ {% tab 'Scala 3' for=compare-case-class-equality %}
274
+ ``` scala
275
+ if point == anotherPoint then
276
+ println(s " $point and $anotherPoint are the same. " )
277
+ else
278
+ println(s " $point and $anotherPoint are different. " )
279
+ // ==> Point(1,2) and Point(1,2) are the same.
280
+
281
+ if point == yetAnotherPoint then
282
+ println(s " $point and $yetAnotherPoint are the same. " )
283
+ else
284
+ println(s " $point and $yetAnotherPoint are different. " )
285
+ // ==> Point(1,2) and Point(2,2) are different.
286
+ ```
287
+ {% endtab %}
288
+
289
+ {% endtabs %}
236
290
237
291
There is a lot more to case classes that we would like to introduce, and we are convinced you will fall in love with them! We will cover them in depth [ later] ( case-classes.html ) .
238
292
@@ -242,6 +296,9 @@ Objects are single instances of their own definitions. You can think of them as
242
296
243
297
You can define objects with the ` object ` keyword:
244
298
299
+ {% tabs id-factory-definition class=tabs-scala-version %}
300
+
301
+ {% tab 'Scala 2' for=id-factory-definition %}
245
302
``` scala mdoc
246
303
object IdFactory {
247
304
private var counter = 0
@@ -251,6 +308,19 @@ object IdFactory {
251
308
}
252
309
}
253
310
```
311
+ {% endtab %}
312
+
313
+ {% tab 'Scala 3' for=id-factory-definition %}
314
+ ``` scala
315
+ object IdFactory :
316
+ private var counter = 0
317
+ def create (): Int =
318
+ counter += 1
319
+ counter
320
+ ```
321
+ {% endtab %}
322
+
323
+ {% endtabs %}
254
324
255
325
You can access an object by referring to its name:
256
326
@@ -269,24 +339,53 @@ Traits are abstract data types containing certain fields and methods. In Scala i
269
339
270
340
You can define traits with the ` trait ` keyword:
271
341
342
+ {% tabs greeter-trait-def class=tabs-scala-version %}
343
+
344
+ {% tab 'Scala 2' for=greeter-trait-def %}
272
345
``` scala mdoc:nest
273
346
trait Greeter {
274
347
def greet (name : String ): Unit
275
348
}
276
349
```
350
+ {% endtab %}
351
+
352
+ {% tab 'Scala 3' for=greeter-trait-def %}
353
+ ``` scala
354
+ trait Greeter :
355
+ def greet (name : String ): Unit
356
+ ```
357
+ {% endtab %}
358
+
359
+ {% endtabs %}
277
360
278
361
Traits can also have default implementations:
279
362
280
- {% scalafiddle %}
363
+ {% tabs greeter-trait-def-impl class=tabs-scala-version %}
364
+
365
+ {% tab 'Scala 2' for=greeter-trait-def-impl %}
281
366
``` scala mdoc:reset
282
367
trait Greeter {
283
368
def greet (name : String ): Unit =
284
369
println(" Hello, " + name + " !" )
285
370
}
286
371
```
372
+ {% endtab %}
373
+
374
+ {% tab 'Scala 3' for=greeter-trait-def-impl %}
375
+ ``` scala
376
+ trait Greeter :
377
+ def greet (name : String ): Unit =
378
+ println(" Hello, " + name + " !" )
379
+ ```
380
+ {% endtab %}
381
+
382
+ {% endtabs %}
287
383
288
384
You can extend traits with the ` extends ` keyword and override an implementation with the ` override ` keyword:
289
385
386
+ {% tabs greeter-implementations class=tabs-scala-version %}
387
+
388
+ {% tab 'Scala 2' for=greeter-implementations %}
290
389
``` scala mdoc
291
390
class DefaultGreeter extends Greeter
292
391
@@ -302,26 +401,60 @@ greeter.greet("Scala developer") // Hello, Scala developer!
302
401
val customGreeter = new CustomizableGreeter (" How are you, " , " ?" )
303
402
customGreeter.greet(" Scala developer" ) // How are you, Scala developer?
304
403
```
305
- {% endscalafiddle %}
404
+ {% endtab %}
405
+
406
+ {% tab 'Scala 3' for=greeter-implementations %}
407
+ ``` scala
408
+ class DefaultGreeter extends Greeter
409
+
410
+ class CustomizableGreeter (prefix : String , postfix : String ) extends Greeter :
411
+ override def greet (name : String ): Unit =
412
+ println(prefix + name + postfix)
413
+
414
+ val greeter = DefaultGreeter ()
415
+ greeter.greet(" Scala developer" ) // Hello, Scala developer!
416
+
417
+ val customGreeter = CustomizableGreeter (" How are you, " , " ?" )
418
+ customGreeter.greet(" Scala developer" ) // How are you, Scala developer?
419
+ ```
420
+ {% endtab %}
421
+
422
+ {% endtabs %}
306
423
307
424
Here, ` DefaultGreeter ` extends only one single trait, but it could extend multiple traits.
308
425
309
426
We will cover traits in depth [ later] ( traits.html ) .
310
427
311
- ## Main Method
428
+ ## Program Entry Point
312
429
313
430
The main method is the entry point of a Scala program. The Java Virtual
314
431
Machine requires a main method, named ` main ` , that takes one
315
432
argument: an array of strings.
316
433
317
- Using an object, you can define the main method as follows:
434
+ {% tabs hello-world-demo class=tabs-scala-version %}
435
+
436
+ {% tab 'Scala 2' for=hello-world-demo %}
437
+
438
+ In Scala 2 you must define a main method manually. Using an object, you can define the main method as follows:
318
439
319
440
``` scala mdoc
320
441
object Main {
321
442
def main (args : Array [String ]): Unit =
322
443
println(" Hello, Scala developer!" )
323
444
}
324
445
```
446
+ {% endtab %}
447
+
448
+ {% tab 'Scala 3' for=hello-world-demo %}
449
+
450
+ In Scala 3, with the ` @main ` annotation, a main method is automatically generated from a method as follows:
451
+
452
+ ``` scala
453
+ @ main def hello () = println(" Hello, Scala developer!" )
454
+ ```
455
+ {% endtab %}
456
+
457
+ {% endtabs %}
325
458
326
459
## More resources
327
460
0 commit comments