0Pricing
Scala for Backend Engineering & Functional Programming · درس

إنشاء Tuples

اجمع القيم المرتبطة معًا

إنشاء Tuples درس مجاني في 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 a Tuple?

A tuple groups a fixed number of values together, even when those values have different types.

Unlike a list, where every element shares one type, a tuple can hold an Int, a String, and a Boolean all at once.

Tuples are perfect for returning or bundling a handful of related values without writing a whole class.

Creating Your First Tuple

You build a tuple by wrapping comma-separated values in parentheses.

Here we pair a name with an age. Scala figures out the types automatically.

val person = ("Alice", 30)
println(person)

Mixing Types Freely

One of the best features of tuples is mixing types in a single value.

This tuple holds a String, an Int, and a Boolean together. Each slot keeps its own type.

val record = ("Bob", 42, true)
println(record)

Tuple Types

The type of a tuple shows every element type in order.

A pair of a String and an Int has type (String, Int). You can write the type explicitly if you want to be clear.

val pair: (String, Int) = ("Cara", 25)
println(pair)

Pairs Are Common

A tuple with exactly two elements is called a pair, and pairs appear everywhere in Scala.

For example, maps are built from key-value pairs. Learning pairs first makes the rest of the language easier.

val score = ("Math", 95)
println(score)

Arrow Syntax for Pairs

For two-element tuples, Scala offers a handy arrow syntax.

Writing key -> value is exactly the same as (key, value). It reads nicely, especially when building maps.

val entry = "name" -> "Dana"
println(entry)

Larger Tuples

Tuples can hold more than two or three values.

Scala supports tuples up to 22 elements, though small tuples are easiest to read. If you need many fields, a case class is usually clearer.

val data = (1, 2, 3, 4, 5)
println(data)

Single-Element Note

There is no useful one-element tuple. Putting one value in parentheses just gives you that value back.

So (5) is simply the number 5, not a tuple. Tuples really start at two elements.

val notATuple = (5)
println(notATuple)

Tuples Are Immutable

Once you create a tuple, its values never change.

This immutability makes tuples safe to share and easy to reason about. To get a different tuple, you simply build a new one.

val a = (1, "one")
val b = (2, "two")
println(a)
println(b)

Returning Multiple Values

A common use of tuples is returning several values from a function at once.

Here a function returns both a sum and a product as a single tuple, avoiding the need for a separate class.

def stats(a: Int, b: Int) = (a + b, a * b)
println(stats(3, 4))

Nesting Tuples

Because a tuple is just a value, you can place a tuple inside another tuple.

This lets you group related sub-pairs together. Keep nesting shallow so your code stays readable.

val nested = (("x", 1), ("y", 2))
println(nested)

Quick Check

Test what you learned about creating tuples.

Recap

You learned that tuples group a fixed number of values of possibly different types inside parentheses.

Pairs can also use key -> value syntax, tuples are immutable, and they are great for returning multiple values. Next you will learn how to pull those values back out.

الأسئلة الشائعة

هل درس «إنشاء Tuples» مجاني؟

نعم — نص درس «إنشاء Tuples» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Scala for Backend Engineering & Functional Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.

ماذا ستتعلم في «إنشاء Tuples»؟

اجمع القيم المرتبطة معًا تتمرن على Scala for Backend Engineering & Functional Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Scala for Backend Engineering & Functional Programming؟

لا تُشترط خبرة سابقة. Scala for Backend Engineering & Functional Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «إنشاء Tuples»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Scala for Backend Engineering & Functional Programming هذا؟

نعم. كل درس في Scala for Backend Engineering & Functional Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. إنشاء Tuples
  2. تفكيك Tuples
  3. إنشاء النطاقات
  4. النطاقات في الحلقات
← العودة إلى Scala for Backend Engineering & Functional Programming