Skip to content

add scala 2&3 tables arround code in num 7 #2568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 75 additions & 10 deletions _overviews/scala3-book/taste-vars-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ next-page: taste-control-structures
---



This section provides a look at Scala variables and data types.


## Two types of variables

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

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

{% tabs var-express-1 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-1 %}

```scala
// immutable
val a = 0

// mutable
var b = 1
```
{% endtab %}
{% endtabs %}

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

{% tabs var-express-2 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-2 %}

```scala
val msg = "Hello, world"
msg = "Aloha" // "reassignment to val" error; this won’t compile
```
{% endtab %}
{% endtabs %}

Conversely, a `var` can be reassigned:

{% tabs var-express-3 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-3 %}

```scala
var msg = "Hello, world"
msg = "Aloha" // this compiles because a var can be reassigned
```


{% endtab %}
{% endtabs %}

## Declaring variable types

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

{% tabs var-express-4 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-4 %}

```scala
val x: Int = 1 // explicit
val x = 1 // implicit; the compiler infers the type
```
{% endtab %}
{% endtabs %}

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

{% tabs var-express-5 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-5 %}

```scala
scala> val x = 1
val x: Int = 1
Expand All @@ -85,19 +104,24 @@ val s: String = a string
scala> val nums = List(1, 2, 3)
val nums: List[Int] = List(1, 2, 3)
```
{% endtab %}
{% endtabs %}

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

{% tabs var-express-6 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-6 %}

```scala
val x: Int = 1
val s: String = "a string"
val p: Person = Person("Richard")
```
{% endtab %}
{% endtabs %}

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



{% comment %}
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?
{% endcomment %}
Expand All @@ -109,6 +133,9 @@ In Scala, everything is an object.

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

{% tabs var-express-7 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-7 %}

```scala
val b: Byte = 1
val i: Int = 1
Expand All @@ -117,38 +144,59 @@ val s: Short = 1
val d: Double = 2.0
val f: Float = 3.0
```
{% endtab %}
{% endtabs %}

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

{% tabs var-express-8 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-8 %}

```scala
val i = 123 // defaults to Int
val j = 1.0 // defaults to Double
```
{% endtab %}
{% endtabs %}

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:

{% tabs var-express-9 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-9 %}

```scala
val x = 1_000L // val x: Long = 1000
val y = 2.2D // val y: Double = 2.2
val z = 3.3F // val z: Float = 3.3
```
{% endtab %}
{% endtabs %}

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

{% tabs var-express-10 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-10 %}

```scala
var a = BigInt(1_234_567_890_987_654_321L)
var b = BigDecimal(123_456.789)
```
{% endtab %}
{% endtabs %}

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

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

{% tabs var-express-11 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-11 %}

```scala
val name = "Bill" // String
val c = 'a' // Char
```

{% endtab %}
{% endtabs %}

### Strings

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

{% tabs var-express-12 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-12 %}

```scala
val firstName = "John"
val mi = 'C'
val lastName = "Doe"
```
{% endtab %}
{% endtabs %}

You can combine those variables in a string like this:

{% tabs var-express-13 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-13 %}

```scala
println(s"Name: $firstName $mi $lastName") // "Name: John C Doe"
```
{% endtab %}
{% endtabs %}

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

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

{% tabs var-express-14 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-14 %}

``` scala
println(s"2 + 2 = ${2 + 2}") // prints "2 + 2 = 4"

val x = -1
println(s"x.abs = ${x.abs}") // prints "x.abs = 1"
```
{% endtab %}
{% endtabs %}

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

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

{% tabs var-express-15 class=tabs-scala-version %}
{% tab 'Scala 2 and 3' for=var-express-15 %}

```scala
val quote = """The essence of Scala:
Fusion of functional and object-oriented
programming in a typed setting."""
```
{% endtab %}
{% endtabs %}

> For more details on string interpolators and multiline strings, see the [“First Look at Types” chapter][first-look].




[first-look]: {% link _overviews/scala3-book/first-look-at-types.md %}