Skip to content

Commit 900c5cd

Browse files
authored
add scala 2&3 tables arround code in num 7 (#2568)
1 parent 1ebb5e1 commit 900c5cd

File tree

1 file changed

+75
-10
lines changed

1 file changed

+75
-10
lines changed

_overviews/scala3-book/taste-vars-data-types.md

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ next-page: taste-control-structures
99
---
1010

1111

12-
1312
This section provides a look at Scala variables and data types.
1413

15-
1614
## Two types of variables
1715

1816
When you create a new variable in Scala, you declare whether the variable is immutable or mutable:
@@ -38,43 +36,64 @@ When you create a new variable in Scala, you declare whether the variable is imm
3836

3937
These examples show how to create `val` and `var` variables:
4038

39+
{% tabs var-express-1 class=tabs-scala-version %}
40+
{% tab 'Scala 2 and 3' for=var-express-1 %}
41+
4142
```scala
4243
// immutable
4344
val a = 0
4445

4546
// mutable
4647
var b = 1
4748
```
49+
{% endtab %}
50+
{% endtabs %}
4851

4952
In an application, a `val` can’t be reassigned.
5053
You’ll cause a compiler error if you try to reassign one:
5154

55+
{% tabs var-express-2 class=tabs-scala-version %}
56+
{% tab 'Scala 2 and 3' for=var-express-2 %}
57+
5258
```scala
5359
val msg = "Hello, world"
5460
msg = "Aloha" // "reassignment to val" error; this won’t compile
5561
```
62+
{% endtab %}
63+
{% endtabs %}
5664

5765
Conversely, a `var` can be reassigned:
5866

67+
{% tabs var-express-3 class=tabs-scala-version %}
68+
{% tab 'Scala 2 and 3' for=var-express-3 %}
69+
5970
```scala
6071
var msg = "Hello, world"
6172
msg = "Aloha" // this compiles because a var can be reassigned
6273
```
63-
64-
74+
{% endtab %}
75+
{% endtabs %}
6576

6677
## Declaring variable types
6778

6879
When you create a variable you can explicitly declare its type, or let the compiler infer the type:
6980

81+
{% tabs var-express-4 class=tabs-scala-version %}
82+
{% tab 'Scala 2 and 3' for=var-express-4 %}
83+
7084
```scala
7185
val x: Int = 1 // explicit
7286
val x = 1 // implicit; the compiler infers the type
7387
```
88+
{% endtab %}
89+
{% endtabs %}
7490

7591
The second form is known as _type inference_, and it’s a great way to help keep this type of code concise.
7692
The Scala compiler can usually infer the data type for you, as shown in the output of these REPL examples:
7793

94+
{% tabs var-express-5 class=tabs-scala-version %}
95+
{% tab 'Scala 2 and 3' for=var-express-5 %}
96+
7897
```scala
7998
scala> val x = 1
8099
val x: Int = 1
@@ -85,19 +104,24 @@ val s: String = a string
85104
scala> val nums = List(1, 2, 3)
86105
val nums: List[Int] = List(1, 2, 3)
87106
```
107+
{% endtab %}
108+
{% endtabs %}
88109

89110
You can always explicitly declare a variable’s type if you prefer, but in simple assignments like these it isn’t necessary:
90111

112+
{% tabs var-express-6 class=tabs-scala-version %}
113+
{% tab 'Scala 2 and 3' for=var-express-6 %}
114+
91115
```scala
92116
val x: Int = 1
93117
val s: String = "a string"
94118
val p: Person = Person("Richard")
95119
```
120+
{% endtab %}
121+
{% endtabs %}
96122

97123
Notice that with this approach, the code feels more verbose than necessary.
98124

99-
100-
101125
{% comment %}
102126
TODO: Jonathan had an early comment on the text below: “While it might feel like this, I would be afraid that people automatically assume from this statement that everything is always boxed.” Suggestion on how to change this?
103127
{% endcomment %}
@@ -109,6 +133,9 @@ In Scala, everything is an object.
109133

110134
These examples show how to declare variables of the numeric types:
111135

136+
{% tabs var-express-7 class=tabs-scala-version %}
137+
{% tab 'Scala 2 and 3' for=var-express-7 %}
138+
112139
```scala
113140
val b: Byte = 1
114141
val i: Int = 1
@@ -117,38 +144,59 @@ val s: Short = 1
117144
val d: Double = 2.0
118145
val f: Float = 3.0
119146
```
147+
{% endtab %}
148+
{% endtabs %}
120149

121150
Because `Int` and `Double` are the default numeric types, you typically create them without explicitly declaring the data type:
122151

152+
{% tabs var-express-8 class=tabs-scala-version %}
153+
{% tab 'Scala 2 and 3' for=var-express-8 %}
154+
123155
```scala
124156
val i = 123 // defaults to Int
125157
val j = 1.0 // defaults to Double
126158
```
159+
{% endtab %}
160+
{% endtabs %}
127161

128162
In your code you can also append the characters `L`, `D`, and `F` (and their lowercase equivalents) to numbers to specify that they are `Long`, `Double`, or `Float` values:
129163

164+
{% tabs var-express-9 class=tabs-scala-version %}
165+
{% tab 'Scala 2 and 3' for=var-express-9 %}
166+
130167
```scala
131168
val x = 1_000L // val x: Long = 1000
132169
val y = 2.2D // val y: Double = 2.2
133170
val z = 3.3F // val z: Float = 3.3
134171
```
172+
{% endtab %}
173+
{% endtabs %}
135174

136175
When you need really large numbers, use the `BigInt` and `BigDecimal` types:
137176

177+
{% tabs var-express-10 class=tabs-scala-version %}
178+
{% tab 'Scala 2 and 3' for=var-express-10 %}
179+
138180
```scala
139181
var a = BigInt(1_234_567_890_987_654_321L)
140182
var b = BigDecimal(123_456.789)
141183
```
184+
{% endtab %}
185+
{% endtabs %}
142186

143187
Where `Double` and `Float` are approximate decimal numbers, `BigDecimal` is used for precise arithmetic.
144188

145189
Scala also has `String` and `Char` data types:
146190

191+
{% tabs var-express-11 class=tabs-scala-version %}
192+
{% tab 'Scala 2 and 3' for=var-express-11 %}
193+
147194
```scala
148195
val name = "Bill" // String
149196
val c = 'a' // Char
150197
```
151-
198+
{% endtab %}
199+
{% endtabs %}
152200

153201
### Strings
154202

@@ -162,28 +210,43 @@ Scala strings are similar to Java strings, but they have two great additional fe
162210
String interpolation provides a very readable way to use variables inside strings.
163211
For instance, given these three variables:
164212

213+
{% tabs var-express-12 class=tabs-scala-version %}
214+
{% tab 'Scala 2 and 3' for=var-express-12 %}
215+
165216
```scala
166217
val firstName = "John"
167218
val mi = 'C'
168219
val lastName = "Doe"
169220
```
221+
{% endtab %}
222+
{% endtabs %}
170223

171224
You can combine those variables in a string like this:
172225

226+
{% tabs var-express-13 class=tabs-scala-version %}
227+
{% tab 'Scala 2 and 3' for=var-express-13 %}
228+
173229
```scala
174230
println(s"Name: $firstName $mi $lastName") // "Name: John C Doe"
175231
```
232+
{% endtab %}
233+
{% endtabs %}
176234

177235
Just precede the string with the letter `s`, and then put a `$` symbol before your variable names inside the string.
178236

179237
To embed arbitrary expressions inside a string, enclose them in curly braces:
180238

239+
{% tabs var-express-14 class=tabs-scala-version %}
240+
{% tab 'Scala 2 and 3' for=var-express-14 %}
241+
181242
``` scala
182243
println(s"2 + 2 = ${2 + 2}") // prints "2 + 2 = 4"
183244

184245
val x = -1
185246
println(s"x.abs = ${x.abs}") // prints "x.abs = 1"
186247
```
248+
{% endtab %}
249+
{% endtabs %}
187250

188251
The `s` that you place before the string is just one possible interpolator.
189252
If you use an `f` instead of an `s`, you can use `printf`-style formatting syntax in the string.
@@ -194,15 +257,17 @@ For instance, some database libraries define the very powerful `sql` interpolato
194257

195258
Multiline strings are created by including the string inside three double-quotes:
196259

260+
{% tabs var-express-15 class=tabs-scala-version %}
261+
{% tab 'Scala 2 and 3' for=var-express-15 %}
262+
197263
```scala
198264
val quote = """The essence of Scala:
199265
Fusion of functional and object-oriented
200266
programming in a typed setting."""
201267
```
268+
{% endtab %}
269+
{% endtabs %}
202270

203271
> For more details on string interpolators and multiline strings, see the [“First Look at Types” chapter][first-look].
204272
205-
206-
207-
208273
[first-look]: {% link _overviews/scala3-book/first-look-at-types.md %}

0 commit comments

Comments
 (0)