Classes
Object-oriented programming in Soplang: classes, constructors, instances, inheritance, parent calls, and type annotations.
Classes in Soplang
Define a class with qaab and create an instance with cusub. Inside a method,
nafta refers to the current instance.
| Keyword | Meaning | English Equivalent |
|---|---|---|
qaab | Define a class | class |
dhis | Constructor, called by cusub | constructor |
nafta | The current instance | this / self |
cusub | Create an instance | new |
dhaxal | Inherit from another class | extends |
Defining a Class – qaab
A class body may contain method definitions only. Store instance data as
fields on nafta, usually from the dhis constructor.
qaab Qof { hawl dhis(magac, da) { nafta.magac = magac nafta.da = da } hawl salaam() { celi "Salaan, waxaan ahay " + nafta.magac } hawl dhalasho() { nafta.da = nafta.da + 1 } }
Creating Instances – cusub
cusub creates an instance and automatically calls dhis with the supplied
arguments.
qaab Qof { hawl dhis(magac, da) { nafta.magac = magac nafta.da = da } hawl salaam() { celi "Salaan, waxaan ahay " + nafta.magac } hawl dhalasho() { nafta.da = nafta.da + 1 } } door aamina = cusub Qof("Aamina", 20) qor(aamina.salaam()) // Salaan, waxaan ahay Aamina aamina.dhalasho() qor(aamina.da) // 21 // Read and update instance fields aamina.magac = "Aamina Cali" qor(aamina.magac) // Aamina Cali qor(nooc(aamina)) // walax
A class without a constructor can also be instantiated, for example with
cusub Tiriye().
Inheritance – dhaxal
Use dhaxal to inherit from another class. The child inherits the parent's
methods, including dhis, and can override a method by defining one with the
same name.
qaab Xayawaan { hawl dhis(magac) { nafta.magac = magac } hawl cod() { celi "..." } hawl sharax() { celi nafta.magac + " wuxuu yiraahdaa " + nafta.cod() } } qaab Eey dhaxal Xayawaan { hawl cod() { celi "Waf!" } } qaab Bisad dhaxal Xayawaan { hawl cod() { celi "Myaaw!" } } door e = cusub Eey("Libaax") door b = cusub Bisad("Mia") qor(e.sharax()) // Libaax wuxuu yiraahdaa Waf! qor(b.sharax()) // Mia wuxuu yiraahdaa Myaaw!
Although sharax() is defined on Xayawaan, its call to nafta.cod() uses
the method belonging to the actual instance's class.
Objects in Collections
Instances are ordinary values: you can store them in lists, pass them to functions, and return them from functions.
qaab Akoon { hawl dhis(lacag) { nafta.lacag = lacag } hawl dhig(x) { nafta.lacag = nafta.lacag + x } } door a = cusub Akoon(100) a.dhig(50) qor(a.lacag) // 150 teed akoonno = [cusub Akoon(1), cusub Akoon(2)] qor(akoonno[1].lacag) // 2
Rules
- Class bodies accept
hawlmethod definitions only. A declaration such asdoor x = 1directly inside a class produces a parser error. dhisis the constructor name and is called automatically bycusub.naftais added to each method automatically; do not declare it as a parameter.- Method lookup starts at the instance's class and follows its parent chain.
Parent Calls and Class Annotations
waalid starts method lookup at the parent of the class that defined the current
method. A variable annotated with a parent class can hold a child instance:
qaab Xayawaan { hawl dhis(magac) { nafta.magac = magac } hawl cod() { celi nafta.magac } } qaab Bisad dhaxal Xayawaan { hawl dhis(magac) { waalid.dhis(magac) } hawl cod() { celi waalid.cod() + ": Mia" } } Xayawaan x = cusub Bisad("Bilan") qor(x.cod()) // Bilan: Mia
Inheritance cycles and invalid class-typed assignments are rejected. The
constructor name dhaw remains supported for compatibility; prefer dhis.
Built-in Functions in Soplang
Reference for Soplang built-ins: console input and output, type conversion, collection creation, ranges, randomness, and numeric helpers.
Error Handling
Catch runtime errors with tijaabi and qabo, understand Somali diagnostics, and look up error codes and source locations.