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 tiro
keyword:
// Using dynamic typing door lambarka1 = 42 // Integer door lambarka2 = 3.14 // Floating-point // Using static typing tiro lambarka3 = 100 // Integer type tiro lambarka4 = -25 // Negative integer // Arithmetic operations qor(lambarka1 + lambarka2) // Addition: 45.14 qor(lambarka1 * lambarka3) // Multiplication: 4200 qor(lambarka3 / lambarka1) // Division: 2.38... // Scientific notation door badan = 1.6e6 // 1,600,000 qor(badan)
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 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 labadaran
:
// Using dynamic typing door waa_sax = run // true door waa_qalad = been // false // Using static typing labadaran waa_arday = run labadaran 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
Collection Types
Collection types allow you to group multiple values together. Soplang provides several collection types for different use cases.
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 lmtayada = [1, 2, 3, 4, 5] liis magacyada = ["Cabdi", "Caasha", "Xasan", "Hodan"] liis isku_dhafan = [1, "Soplang", run, 3.14] // Accessing elements (zero-indexed) qor(lmtayada[0]) // Outputs: 1 qor(magacyada[2]) // Outputs: Xasan // Modifying lists lmtayada[0] = 10 // Change the first element qor(lmtayada) // Outputs: [10, 2, 3, 4, 5] // List methods lmtayada.kudar(6) // Add an element to the end qor(lmtayada) // 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: 3 // List slicing qor(lmtayada.qayb(1, 4)) // Outputs: [2, 3, 4] // Checking if an element exists qor("Xasan" ku dhex jira magacyada) // Outputs: true
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
Tuples
Tuples are similar to lists but are immutable (cannot be changed after creation). They are defined using parentheses:
// Creating tuples door labojoog = (1, 2) door seddexjoog = ("Soplang", 2023, run) // Accessing tuple elements qor(labojoog[0]) // Outputs: 1 qor(seddexjoog[1]) // Outputs: 2023 // Tuple unpacking door (x, y) = labojoog qor(x) // Outputs: 1 qor(y) // Outputs: 2 door (luuqad, sanad, xaalad) = seddexjoog qor(luuqad) // Outputs: Soplang qor(sanad) // Outputs: 2023 qor(xaalad) // Outputs: true // Tuples are immutable // labojoog[0] = 10 // This would cause an error // Tuple methods qor(seddexjoog.dherer()) // Outputs: 3
Sets
Sets are unordered collections of unique items. They are useful for membership testing and eliminating duplicate entries:
// Creating sets door urur1 = {1, 2, 3, 4, 5} door urur2 = {3, 4, 5, 6, 7} // Adding elements to a set urur1.kudar(6) qor(urur1) // Outputs: {1, 2, 3, 4, 5, 6} // Duplicate elements are ignored urur1.kudar(2) qor(urur1) // Still outputs: {1, 2, 3, 4, 5, 6} // Set operations door midho = urur1 & urur2 // Intersection qor(midho) // Outputs: {3, 4, 5, 6} door midhayn = urur1 | urur2 // Union qor(midhayn) // Outputs: {1, 2, 3, 4, 5, 6, 7} door farqi = urur1 - urur2 // Difference qor(farqi) // Outputs: {1, 2} // Checking if an element exists qor(3 ku dhex jira urur1) // Outputs: true qor(8 ku dhex jira urur1) // Outputs: false
Special Types
Soplang also provides a few special types for specific use cases.
Null Type
The waxba
keyword represents a null or empty value:
// Using null door qof = waxba qor(qof) // Outputs: null // Checking for null haddii (qof == waxba) { qor("Qofka ma jiro") } haddii_kalena { qor("Qofka wuu jiraa") } // Null coalescing (default value if null) door magac = waxba qor(magac ?? "Aan la aqoon") // Outputs: "Aan la aqoon"
Date and Time
Soplang has built-in support for date and time operations:
// Current date and time door waqtiga = waqti_hadda() qor(waqtiga) // Outputs: 2023-10-24T14:30:45 // Creating specific dates door taariikh = taariikh_cusub(2023, 10, 1) qor(taariikh) // Outputs: 2023-10-01 // Date formatting qor(taariikh.format("dd/MM/yyyy")) // Outputs: 01/10/2023 // Date calculations door berri = taariikh.kudar_maalmo(1) qor(berri) // Outputs: 2023-10-02 door todobaad = taariikh.kudar_todobaadyo(1) qor(todobaad) // Outputs: 2023-10-08 // Time differences door farqi = berri - taariikh qor(farqi.maalmo()) // Outputs: 1
Enumerations
Enumerations allow you to define a set of named constants:
// Defining an enumeration meeqaam Dhinacyada { Waqooyi, Koonfur, Bari, Galbeed } // Using enum values door jihada = Dhinacyada.Bari qor(jihada) // Outputs: Dhinacyada.Bari // Comparing enum values haddii (jihada == Dhinacyada.Waqooyi) { qor("Waa xagga woqooyi") } haddii_kalena haddii (jihada == Dhinacyada.Bari) { qor("Waa xagga bari") } // Enums with associated values meeqaam Midabada { Guduud = "#FF0000", Cagaar = "#00FF00", Buluug = "#0000FF" } qor(Midabada.Guduud) // Outputs: #FF0000
Type Conversion
Soplang provides functions to convert between different data types:
// String to number door qoraal_tiro = "42" door tiro1 = tiro(qoraal_tiro) qor(tiro1) // Outputs: 42 // Number to string door lambarka = 3.14 door qoraal1 = qoraal(lambarka) qor(qoraal1) // Outputs: "3.14" // String to boolean door qoraal_xun = "run" door bool1 = labadaran(qoraal_xun) qor(bool1) // Outputs: true // List to set (removes duplicates) door liiska = [1, 2, 2, 3, 3, 3] door urur = urur(liiska) qor(urur) // Outputs: {1, 2, 3} // String to list door kala_qeybin = qoraal("Soplang").kala_qeybi() qor(kala_qeybin) // Outputs: ["S", "o", "p", "l", "a", "n", "g"] // Explicit vs implicit conversion door a = "10" door b = 20 qor(a + b) // Outputs: "1020" (string concatenation) qor(tiro(a) + b) // Outputs: 30 (numeric addition)
Type Checking
Soplang provides functions to check the type of a value at runtime:
// Checking the type of a value door a = 42 door b = "Soplang" door c = [1, 2, 3] door d = {"magac": "Cabdi"} door e = waxba qor(nooca(a)) // Outputs: "tiro" qor(nooca(b)) // Outputs: "qoraal" qor(nooca(c)) // Outputs: "liis" qor(nooca(d)) // Outputs: "shey" qor(nooca(e)) // Outputs: "waxba" // Type checking in conditionals haddii (nooca(a) == "tiro") { qor("Waa tiro") } // Instance checking fasalka Qof { howl cusub(magac) { nafta.magac = magac } } door qof1 = cusub Qof("Aaden") qor(qof1 ka_tirsan Qof) // Outputs: true
Next Steps
Now that you understand the data types in Soplang, you can explore other aspects of the language: