文より式
すべてが式として扱われます。
「文より式」はCoddyKit上の無料Scala for Backend Engineering & Functional Programmingレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはScala for Backend Engineering & Functional Programming学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Scala for Backend Engineering & Functional Programmingコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Expressions vs Statements
A statement does something but returns nothing useful. An expression evaluates to a value.
In Scala, almost everything is an expression. This is a core idea that shapes how you write code.
if Is an Expression
In many languages if is a statement. In Scala, if/else returns a value, so you can assign it directly to a val.
No need for a separate variable assigned in each branch.
object Main {
def main(args: Array[String]): Unit = {
val temp = 30
val label = if (temp > 25) "hot" else "mild"
println(label)
}
}Blocks Are Expressions
A block { ... } is an expression too. Its value is the value of its last line.
So you can compute intermediate values, then end with the result.
object Main {
def main(args: Array[String]): Unit = {
val result = {
val a = 2
val b = 3
a * b
}
println(result)
}
}match Is an Expression
Pattern matching with match also returns a value. Each branch produces a result, and the whole expression yields the matched branch's value.
object Main {
def main(args: Array[String]): Unit = {
val day = 3
val name = day match {
case 1 => "Mon"
case 2 => "Tue"
case 3 => "Wed"
case _ => "Other"
}
println(name)
}
}The Unit Type
Some expressions exist only for their side effect, like println. They return Unit, written ().
Unit is similar to void in other languages but is still a real value.
object Main {
def main(args: Array[String]): Unit = {
val nothing: Unit = println("side effect")
println(nothing)
}
}Returning From Functions
Because the body of a function is an expression, the last expression is its return value. You rarely need the return keyword.
Here the function body is a single expression.
object Main {
def max(a: Int, b: Int): Int = if (a > b) a else b
def main(args: Array[String]): Unit = {
println(max(7, 4))
}
}Expression-Oriented Functions
A function can be a block expression whose last line is the result. This keeps logic compact and avoids mutable temporaries.
object Main {
def discount(price: Double): Double = {
val rate = if (price > 100) 0.2 else 0.1
price * (1 - rate)
}
def main(args: Array[String]): Unit = {
println(discount(150.0))
}
}No Need for Mutation
Because if and match are expressions, you can assign their result to a val instead of mutating a var.
This leads to cleaner, more functional code.
object Main {
def main(args: Array[String]): Unit = {
val score = 85
val grade = if (score >= 90) "A"
else if (score >= 80) "B"
else "C"
println(grade)
}
}Composing Expressions
Because everything returns a value, expressions nest and compose freely. The output of one becomes the input of another.
This composability is a big reason Scala feels expressive.
object Main {
def main(args: Array[String]): Unit = {
val n = 7
val message = "n is " + (if (n % 2 == 0) "even" else "odd")
println(message)
}
}Why It Matters
Expression-oriented code:
- Reduces the need for mutable variables
- Makes the flow of values explicit
- Composes naturally into larger programs
- Pairs perfectly with immutability
All Together
Here a block expression contains an if expression, all assigned to a single immutable result.
object Main {
def main(args: Array[String]): Unit = {
val total = {
val items = 3
val each = 12
val raw = items * each
if (raw > 30) raw - 5 else raw
}
println(total)
}
}Quick Check
Check your grasp of expressions.
Recap
You learned that in Scala nearly everything is an expression:
if/else,match, and blocks all return values- A block's value is its last expression
- Side-effecting expressions return
Unit(()) - The last expression of a function is its return value
- This reduces mutation and improves composability
よくある質問
「文より式」レッスンは無料ですか?
はい。「文より式」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Scala for Backend Engineering & Functional Programmingコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Scala for Backend Engineering & Functional Programmingコースには全4レッスンが含まれています。
「文より式」で何を学びますか?
すべてが式として扱われます。 ブラウザで直接実行するハンズオンコードでScala for Backend Engineering & Functional Programmingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Scala for Backend Engineering & Functional Programmingを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのScala for Backend Engineering & Functional Programmingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「文より式」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このScala for Backend Engineering & Functional Programmingレッスンでコードを書いて実行できますか?
はい。すべてのScala for Backend Engineering & Functional Programmingレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。