تعبيرات match
التفرع حسب البنية
تعبيرات match درس مجاني في Scala for Backend Engineering & Functional Programming على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Scala for Backend Engineering & Functional Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What Is match?
match is Scala's powerful branching construct. You give it a value and a series of case patterns, and it runs the first matching branch.
Think of it as a more capable, expression-based version of a switch statement.
Basic Syntax
You write value match { case pattern => result }. Each case defines a pattern and the code to run when it matches.
Because match is an expression, the whole thing returns a value.
object Main {
def main(args: Array[String]): Unit = {
val n = 2
val word = n match {
case 1 => "one"
case 2 => "two"
case 3 => "three"
}
println(word)
}
}The Wildcard Case
The underscore _ is a wildcard that matches anything. Use it as a catch-all default at the end.
Without it, an unmatched value throws a MatchError at runtime.
object Main {
def main(args: Array[String]): Unit = {
val n = 9
val word = n match {
case 1 => "one"
case 2 => "two"
case _ => "many"
}
println(word)
}
}Matching Strings
Patterns are not limited to numbers. You can match string literals directly.
object Main {
def main(args: Array[String]): Unit = {
val cmd = "start"
val msg = cmd match {
case "start" => "Starting up"
case "stop" => "Shutting down"
case _ => "Unknown command"
}
println(msg)
}
}match Returns a Value
Since each branch produces a value, you can assign the result of match to a val or use it inline.
This keeps your code clean and avoids mutable variables.
object Main {
def main(args: Array[String]): Unit = {
val light = "red"
println(s"Action: " + (light match {
case "red" => "stop"
case "green" => "go"
case _ => "caution"
}))
}
}Combining Cases
You can match several values in one case using | (the pipe). All listed values lead to the same branch.
object Main {
def main(args: Array[String]): Unit = {
val day = "Sat"
val kind = day match {
case "Sat" | "Sun" => "weekend"
case _ => "weekday"
}
println(kind)
}
}match in a Function
A common pattern is to make the entire function body a single match expression. The matched result becomes the return value.
object Main {
def describe(n: Int): String = n match {
case 0 => "zero"
case 1 => "one"
case _ => "other"
}
def main(args: Array[String]): Unit = {
println(describe(0))
println(describe(5))
}
}Binding the Whole Value
You can bind the matched value to a name in the wildcard case using case other =>. Then you can use other in that branch.
object Main {
def main(args: Array[String]): Unit = {
val n = 42
val result = n match {
case 0 => "nothing"
case other => s"got $other"
}
println(result)
}
}Order Matters
Cases are tested top to bottom, and the first match wins. Put more specific patterns before general ones.
A wildcard placed too early would shadow the cases below it.
object Main {
def main(args: Array[String]): Unit = {
val n = 1
val msg = n match {
case 1 => "specific one"
case _ => "anything else"
}
println(msg)
}
}Why Use match?
Pattern matching is central to Scala because it:
- Reads clearly for multi-way branching
- Returns a value as an expression
- Scales up to types, data structures, and guards
- Can be checked for completeness by the compiler
Putting It Together
Here is a small classifier using literals, a combined case, and a default.
object Main {
def grade(score: Int): String = score match {
case 10 | 9 => "excellent"
case 8 | 7 => "good"
case _ => "keep trying"
}
def main(args: Array[String]): Unit = {
println(grade(9))
println(grade(7))
println(grade(3))
}
}Quick Check
Test your understanding of match expressions.
Recap
You learned the basics of match:
- Syntax is
value match { case pattern => result } _is the wildcard catch-all- Combine values with
| matchis an expression that returns a value- Cases are tried top to bottom; first match wins
الأسئلة الشائعة
هل درس «تعبيرات match» مجاني؟
نعم — نص درس «تعبيرات match» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Scala for Backend Engineering & Functional Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.
ماذا ستتعلم في «تعبيرات match»؟
التفرع حسب البنية تتمرن على Scala for Backend Engineering & Functional Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Scala for Backend Engineering & Functional Programming؟
لا تُشترط خبرة سابقة. Scala for Backend Engineering & Functional Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «تعبيرات match»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Scala for Backend Engineering & Functional Programming هذا؟
نعم. كل درس في Scala for Backend Engineering & Functional Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعبيرات match
- مطابقة الأنواع والقيم
- الحراس والربط
- التفكيك