Data Types in Soplang
Soplang provides a rich set of data types to represent different kinds of values. This guide covers all the built-in data types available in Soplang and how to use them effectively.
Primitive Types
Primitive types are the most basic data types in Soplang. They represent simple values and are the building blocks for more complex types.
Numbers
Soplang supports both integers and floating-point numbers. You can define a number variable using either the dynamic door
keyword or the static abn
keyword:
// Using dynamic typing door lambarka1 = 42 // Integer door lambarka2 = 3.14 // Floating-point // Using static typing abn lambarka3 = 100 // Integer type abn lambarka4 = -25 // Negative integer // Arithmetic operations qor(lambarka1 + lambarka2) // Addition: 45.14 qor(lambarka1 * lambarka3) // Multiplication: 4200 qor(lambarka3 / lambarka1) // Division: 2.38...
Strings
Strings in Soplang are sequences of characters. You can define strings using either single '
or double quotes "
and manipulate them with various operations:
// Using dynamic typing door magac1 = "Soplang" door magac2 = 'Programming Language' // Using static typing qoraal magac3 = "Soplang: Luuqadda Barnaamijyada ee Afka Soomaaliga" // String concatenation qor(magac1 + " - " + magac2) // Outputs: Soplang - Programming Language // String length qor(magac1.dherer()) // Outputs: 7 // String methods qor(magac1.sareyn()) // Outputs: SOPLANG qor(magac1.hooseyn()) // Outputs: soplang qor(magac3.qayb(0, 7)) // Outputs: Soplang // String interpolation door version = 2.0 qor(f"{magac1} v{version}") // Outputs: Soplang v2.0
Booleans
Boolean values represent truth values with two possible states: run
(true) or been
(false). You can define boolean variables using door
or bool
:
// Using dynamic typing door waa_sax = run // true door waa_qalad = been // false // Using static typing bool waa_arday = run bool waa_macalin = been // Logical operations qor(waa_sax && waa_qalad) // Logical AND: false qor(waa_sax || waa_qalad) // Logical OR: true qor(!waa_sax) // Logical NOT: false // Comparison operations door x = 10 door y = 20 qor(x > y) // false qor(x <= y) // true qor(x == 10) // true qor(x != y) // true
Lists
Lists are ordered collections of items that can be of any type. You can define a list using the liis
keyword or use door
with square brackets:
// Creating lists door tiro = [1, 2, 3, 4, 5] liis magacyada = ["Cabdi", "Caasha", "Xasan", "Hodan"] liis isku_dhafan = [1, "Soplang", run, 3.14] // Accessing elements (zero-indexed) qor(tiro[0]) // Outputs: 1 qor(magacyada[2]) // Outputs: Xasan // Modifying lists tiro[0] = 10 // Change the first element qor(magacyada) // Outputs: [10, 2, 3, 4, 5] // List methods tiro.kudar(6) // Add an element to the end qor(magacyada) // Outputs: [10, 2, 3, 4, 5, 6] magacyada.saar(1) // Remove element at index 1 qor(magacyada) // Outputs: ["Cabdi", "Xasan", "Hodan"] // List length qor(magacyada.dherer()) // Outputs: 4
Dictionaries
Dictionaries are collections of key-value pairs where each key must be unique. You can define a dictionary using door
with curly braces or using the shey
keyword:
// Creating dictionaries door qof = { "magac": "Cabdilaahi", "da": 25, "waa_arday": run } shey luuqad = { "magac": "Soplang", "version": 2.0, "sanadka": 2023, "features": ["OOP", "Dynamic Typing", "Easy Syntax"] } // Accessing values qor(qof["magac"]) // Outputs: Cabdilaahi qor(luuqad["version"]) // Outputs: 2.0 // Alternative dot notation qor(qof.magac) // Outputs: Cabdilaahi qor(luuqad.features) // Outputs: ["OOP", "Dynamic Typing", "Easy Syntax"] // Modifying dictionaries qof["da"] = 26 // Update a value qof["degaan"] = "Muqdisho" // Add a new key-value pair qor(qof) // Outputs: {"magac": "Cabdilaahi", "da": 26, "waa_arday": true, "degaan": "Muqdisho"} // Checking if a key exists qor("magac" ku dhex jira qof) // Outputs: true qor("luuqad" ku dhex jira qof) // Outputs: false // Dictionary methods qor(qof.fureyaasha()) // Outputs: ["magac", "da", "waa_arday", "degaan"] qor(qof.qiimeyaasha()) // Outputs: ["Cabdilaahi", 26, true, "Muqdisho"] qor(luuqad.dherer()) // Outputs: 4