Skip to content

Commit ff54f5b

Browse files
adpi2julienrf
authored andcommitted
Translate tour/unified-types to Scala 3
1 parent bc0e90c commit ff54f5b

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

_tour/unified-types.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ In Scala, all values have a type, including numerical values and functions. The
2525

2626
Here is an example that demonstrates that strings, integers, characters, boolean values, and functions are all of type `Any` just like every other object:
2727

28+
{% tabs unified-types-1 %}
29+
{% tab 'Scala 2 and 3' for=unified-types-1 %}
2830
```scala mdoc
2931
val list: List[Any] = List(
3032
"a string",
@@ -36,6 +38,8 @@ val list: List[Any] = List(
3638

3739
list.foreach(element => println(element))
3840
```
41+
{% endtab %}
42+
{% endtabs %}
3943

4044
It defines a value `list` of type `List[Any]`. The list is initialized with elements of various types, but each is an instance of `scala.Any`, so you can add them to the list.
4145

@@ -55,21 +59,31 @@ Value types can be cast in the following way:
5559

5660
For example:
5761

62+
{% tabs unified-types-2 %}
63+
{% tab 'Scala 2 and 3' for=unified-types-2 %}
5864
```scala mdoc
5965
val x: Long = 987654321
6066
val y: Float = x // 9.8765434E8 (note that some precision is lost in this case)
6167

6268
val face: Char = '☺'
6369
val number: Int = face // 9786
6470
```
71+
{% endtab %}
72+
{% endtabs %}
73+
6574

6675
Casting is unidirectional. This will not compile:
6776

68-
```
77+
{% tabs unified-types-3 %}
78+
{% tab 'Scala 2 and 3' for=unified-types-3 %}
79+
```scala
6980
val x: Long = 987654321
7081
val y: Float = x // 9.8765434E8
7182
val z: Long = y // Does not conform
7283
```
84+
{% endtab %}
85+
{% endtabs %}
86+
7387

7488
You can also cast a reference type to a subtype. This will be covered later in the tour.
7589

0 commit comments

Comments
 (0)