مطابقة الأنواع والقيم
أنماط الأنواع
مطابقة الأنواع والقيم درس مجاني في Scala for Backend Engineering & Functional Programming على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Scala for Backend Engineering & Functional Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Type Patterns
Beyond matching literal values, match can branch on the type of a value. This is called a type pattern.
You write case x: Type => and the branch runs when the value is of that type, binding it to x.
Matching by Type
When a value has a general type like Any, you can discover its concrete type with a type pattern.
Each case x: T checks the runtime type.
object Main {
def describe(x: Any): String = x match {
case i: Int => "an Int"
case s: String => "a String"
case _ => "something else"
}
def main(args: Array[String]): Unit = {
println(describe(5))
println(describe("hi"))
println(describe(3.14))
}
}Using the Bound Value
The bound name has the matched type, so you can call its methods safely in that branch.
No casting needed: the compiler knows the type inside the case.
object Main {
def shout(x: Any): String = x match {
case s: String => s.toUpperCase
case i: Int => (i * 2).toString
case _ => "?"
}
def main(args: Array[String]): Unit = {
println(shout("hello"))
println(shout(21))
}
}Type and Value Together
You can mix value patterns and type patterns in the same match. Value patterns are usually more specific, so place them first.
object Main {
def check(x: Any): String = x match {
case 0 => "zero int"
case i: Int => s"nonzero int $i"
case _ => "not an int"
}
def main(args: Array[String]): Unit = {
println(check(0))
println(check(7))
println(check("x"))
}
}Matching Double and Boolean
Type patterns work for any type, including Double, Boolean, and Char.
object Main {
def kind(x: Any): String = x match {
case d: Double => s"double $d"
case b: Boolean => s"bool $b"
case _ => "other"
}
def main(args: Array[String]): Unit = {
println(kind(2.5))
println(kind(true))
}
}Matching Collections by Type
You can also match container types like List. Here we distinguish a list from other values.
object Main {
def info(x: Any): String = x match {
case l: List[_] => s"a list of size ${l.size}"
case _ => "not a list"
}
def main(args: Array[String]): Unit = {
println(info(List(1, 2, 3)))
println(info("nope"))
}
}A Common Use: Handling Any
Type patterns shine when a function receives Any and must react differently per type, such as logging or serialization.
object Main {
def render(x: Any): String = x match {
case i: Int => s"int=$i"
case d: Double => s"dbl=$d"
case s: String => s"str=$s"
case _ => "unknown"
}
def main(args: Array[String]): Unit = {
List(1, 2.0, "three").foreach(v => println(render(v)))
}
}Order Still Matters
Like all matches, type patterns are checked top to bottom. A broader type placed first can hide a narrower one below.
Always order from most specific to most general.
object Main {
def label(x: Any): String = x match {
case s: String => "string first"
case _: Any => "anything"
}
def main(args: Array[String]): Unit = {
println(label("hi"))
println(label(99))
}
}Type Erasure Caveat
At runtime, generic type parameters are erased. So List[Int] and List[String] look the same. Matching on List[Int] only checks that it is a List.
Use List[_] to be explicit and avoid warnings.
Why Type Patterns?
Type patterns let you:
- Safely narrow a general type without casting
- Handle heterogeneous values cleanly
- Combine with value patterns and guards
They are a stepping stone to matching on case classes and ADTs.
Putting It Together
Combine value and type patterns to classify mixed input.
object Main {
def classify(x: Any): String = x match {
case 0 => "zero"
case i: Int => "int"
case s: String => "text of length " + s.length
case _ => "other"
}
def main(args: Array[String]): Unit = {
println(classify(0))
println(classify(5))
println(classify("hey"))
}
}Quick Check
Test your knowledge of type patterns.
Recap
You learned to match on types:
case x: Typematches by runtime type and bindsx- The bound value has the matched type, so no cast is needed
- Mix value and type patterns, most specific first
- Generic parameters are erased, so use
List[_]
الأسئلة الشائعة
هل درس «مطابقة الأنواع والقيم» مجاني؟
نعم — نص درس «مطابقة الأنواع والقيم» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Scala for Backend Engineering & Functional Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.
ماذا ستتعلم في «مطابقة الأنواع والقيم»؟
أنماط الأنواع تتمرن على Scala for Backend Engineering & Functional Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Scala for Backend Engineering & Functional Programming؟
لا تُشترط خبرة سابقة. Scala for Backend Engineering & Functional Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «مطابقة الأنواع والقيم»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Scala for Backend Engineering & Functional Programming هذا؟
نعم. كل درس في Scala for Backend Engineering & Functional Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعبيرات match
- مطابقة الأنواع والقيم
- الحراس والربط
- التفكيك