Values and types
Scala has two keywords for a name. val binds a value once and var binds a variable that can be reassigned; the compiler infers the type of both from the expression on the right, and a declared type is a check rather than a requirement.
val and var
val is the default of the language and of this course. var is there for a loop counter and for a value the standard library hands back by mutating, and a var in the middle of a method is a line a reader stops at. The program below declares both, and a val of every common type:
values.scala
@main def values(): Unit =
val name = "Scala"
var count = 2
count = count + 1
val version: String = "3.10.1"
val year: Int = 2026
val ratio: Double = 0.5
val enabled: Boolean = true
println(s"$name $version, $year, $ratio, $enabled")
println(count)
val lines = 3
val written: Unit = println(s"$lines lines")
println(written)
scala-cli run --server=false values.scala
Scala 3.10.1, 2026, 0.5, true
3
3 lines
()
The last two lines are the point of Unit: println writes its line and returns (), the one value of the type Unit, so a val can hold the result and a program can print it. A method that returns nothing useful declares Unit.
Declared types and inference
Inference works from the right-hand side, so val year = 2026 is an Int and val ratio = 0.5 a Double. A declared type on the left is checked, not converted: val year: Long = 2026 compiles, because an Int widens to a Long, and val year: Int = 2026L does not, because a Long does not narrow by itself.
The type of a block is the type of its last expression. A block is a value like any other, which is how a computation gets a name without a method:
block.scala
@main def block(): Unit =
val total =
val first = 10
val second = 32
first + second
println(total)
println(total.getClass.getSimpleName)
scala-cli run --server=false block.scala
42
int
getClass.getSimpleName is a Java method on every JVM object and it prints the erased name of the class, which is how a program can show the type the compiler chose. It is a diagnostic, not something a program should branch on.
A type error
A declared type is where a mistake stops. The program below assigns a string to an Int, and the compiler refuses the file:
bad_type.scala
@main def badType(): Unit =
val count: Int = "three"
println(count)
mkdir -p out
scalac -color:never -d out bad_type.scala 2>&1; echo "exit status: $?"
-- [E007] Type Mismatch Error: bad_type.scala:2:19 -----------------------------
2 | val count: Int = "three"
| ^^^^^^^
| Found: ("three" : String)
| Required: Int
|
| longer explanation available when compiling with `-explain`
1 error found
exit status: 1
The header names the file, the line and the column; the caret points at the expression, and Found and Required give both types. The error kind E007 is the same code a report quotes, and -explain adds the longer explanation the last line mentions. Nothing was produced: the folder out stays empty.
Related
- Numbers: the numeric types and their limits.
- Strings and interpolation: the other type this lesson printed.
- Blocks, indentation, packages and imports: the block as syntax.
- Sealed hierarchies: the types that make an exhaustive check possible.
Автор статьи: Arthur Isaev