Skip to content

Commit 30f18d5

Browse files
committed
use code tabs for tour/basics
1 parent 09ec37c commit 30f18d5

File tree

1 file changed

+149
-16
lines changed

1 file changed

+149
-16
lines changed

_tour/basics.md

Lines changed: 149 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ Expressions are computable statements:
3030
```
3131
You can output the results of expressions using `println`:
3232

33-
{% scalafiddle %}
3433
```scala mdoc
3534
println(1) // 1
3635
println(1 + 1) // 2
3736
println("Hello!") // Hello!
3837
println("Hello," + " world!") // Hello, world!
3938
```
40-
{% endscalafiddle %}
4139

4240
### Values
4341

@@ -109,21 +107,17 @@ On the left of `=>` is a list of parameters. On the right is an expression invol
109107

110108
You can also name functions:
111109

112-
{% scalafiddle %}
113110
```scala mdoc
114111
val addOne = (x: Int) => x + 1
115112
println(addOne(1)) // 2
116113
```
117-
{% endscalafiddle %}
118114

119115
A function can have multiple parameters:
120116

121-
{% scalafiddle %}
122117
```scala mdoc
123118
val add = (x: Int, y: Int) => x + y
124119
println(add(1, 2)) // 3
125120
```
126-
{% endscalafiddle %}
127121

128122
Or it can have no parameters at all:
129123

@@ -138,23 +132,19 @@ Methods look and behave very similar to functions, but there are a few key diffe
138132

139133
Methods are defined with the `def` keyword. `def` is followed by a name, parameter list(s), a return type, and a body:
140134

141-
{% scalafiddle %}
142135
```scala mdoc:nest
143136
def add(x: Int, y: Int): Int = x + y
144137
println(add(1, 2)) // 3
145138
```
146-
{% endscalafiddle %}
147139

148140
Notice how the return type `Int` is declared _after_ the parameter list and a `:`.
149141

150142
A method can take multiple parameter lists:
151143

152-
{% scalafiddle %}
153144
```scala mdoc
154145
def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier
155146
println(addThenMultiply(1, 2)(3)) // 9
156147
```
157-
{% endscalafiddle %}
158148

159149
Or no parameter lists at all:
160150

@@ -167,36 +157,78 @@ There are some other differences, but for now, you can think of methods as somet
167157

168158
Methods can have multi-line expressions as well:
169159

170-
{% scalafiddle %}
160+
{% tabs get-square-string class=tabs-scala-version %}
161+
162+
{% tab 'Scala 2' for=get-square-string %}
171163
```scala mdoc
172164
def getSquareString(input: Double): String = {
173165
val square = input * input
174166
square.toString
175167
}
176168
println(getSquareString(2.5)) // 6.25
177169
```
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 %}
179183

180184
The last expression in the body is the method's return value. (Scala does have a `return` keyword, but it is rarely used.)
181185

182186
## Classes
183187

184188
You can define classes with the `class` keyword, followed by its name and constructor parameters:
185189

190+
{% tabs greeter-definition class=tabs-scala-version %}
191+
192+
{% tab 'Scala 2' for=greeter-definition %}
186193
```scala mdoc
187194
class Greeter(prefix: String, suffix: String) {
188195
def greet(name: String): Unit =
189196
println(prefix + name + suffix)
190197
}
191198
```
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+
192211
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.)
193212

194213
You can make an instance of a class with the `new` keyword:
195214

215+
{% tabs greeter-usage class=tabs-scala-version %}
216+
217+
{% tab 'Scala 2' for=greeter-usage %}
196218
```scala mdoc:nest
197219
val greeter = new Greeter("Hello, ", "!")
198220
greeter.greet("Scala developer") // Hello, Scala developer!
199221
```
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 %}
200232

201233
We will cover classes in depth [later](classes.html).
202234

@@ -220,6 +252,9 @@ val yetAnotherPoint = Point(2, 2)
220252

221253
Instances of case classes are compared by value, not by reference:
222254

255+
{% tabs compare-case-class-equality class=tabs-scala-version %}
256+
257+
{% tab 'Scala 2' for=compare-case-class-equality %}
223258
```scala mdoc
224259
if (point == anotherPoint) {
225260
println(s"$point and $anotherPoint are the same.")
@@ -233,6 +268,25 @@ if (point == yetAnotherPoint) {
233268
println(s"$point and $yetAnotherPoint are different.")
234269
} // Point(1,2) and Point(2,2) are different.
235270
```
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 %}
236290

237291
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).
238292

@@ -242,6 +296,9 @@ Objects are single instances of their own definitions. You can think of them as
242296

243297
You can define objects with the `object` keyword:
244298

299+
{% tabs id-factory-definition class=tabs-scala-version %}
300+
301+
{% tab 'Scala 2' for=id-factory-definition %}
245302
```scala mdoc
246303
object IdFactory {
247304
private var counter = 0
@@ -251,6 +308,19 @@ object IdFactory {
251308
}
252309
}
253310
```
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 %}
254324

255325
You can access an object by referring to its name:
256326

@@ -269,24 +339,53 @@ Traits are abstract data types containing certain fields and methods. In Scala i
269339

270340
You can define traits with the `trait` keyword:
271341

342+
{% tabs greeter-trait-def class=tabs-scala-version %}
343+
344+
{% tab 'Scala 2' for=greeter-trait-def %}
272345
```scala mdoc:nest
273346
trait Greeter {
274347
def greet(name: String): Unit
275348
}
276349
```
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 %}
277360

278361
Traits can also have default implementations:
279362

280-
{% scalafiddle %}
363+
{% tabs greeter-trait-def-impl class=tabs-scala-version %}
364+
365+
{% tab 'Scala 2' for=greeter-trait-def-impl %}
281366
```scala mdoc:reset
282367
trait Greeter {
283368
def greet(name: String): Unit =
284369
println("Hello, " + name + "!")
285370
}
286371
```
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 %}
287383

288384
You can extend traits with the `extends` keyword and override an implementation with the `override` keyword:
289385

386+
{% tabs greeter-implementations class=tabs-scala-version %}
387+
388+
{% tab 'Scala 2' for=greeter-implementations %}
290389
```scala mdoc
291390
class DefaultGreeter extends Greeter
292391

@@ -302,26 +401,60 @@ greeter.greet("Scala developer") // Hello, Scala developer!
302401
val customGreeter = new CustomizableGreeter("How are you, ", "?")
303402
customGreeter.greet("Scala developer") // How are you, Scala developer?
304403
```
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 %}
306423

307424
Here, `DefaultGreeter` extends only one single trait, but it could extend multiple traits.
308425

309426
We will cover traits in depth [later](traits.html).
310427

311-
## Main Method
428+
## Program Entry Point
312429

313430
The main method is the entry point of a Scala program. The Java Virtual
314431
Machine requires a main method, named `main`, that takes one
315432
argument: an array of strings.
316433

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:
318439

319440
```scala mdoc
320441
object Main {
321442
def main(args: Array[String]): Unit =
322443
println("Hello, Scala developer!")
323444
}
324445
```
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 %}
325458

326459
## More resources
327460

0 commit comments

Comments
 (0)