Soplang Logo
Soplang

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.

KeywordMeaningEnglish Equivalent
qaabDefine a classclass
dhisConstructor, called by cusubconstructor
naftaThe current instancethis / self
cusubCreate an instancenew
dhaxalInherit from another classextends

Defining a Class – qaab

A class body may contain method definitions only. Store instance data as fields on nafta, usually from the dhis constructor.

class_basic.sop
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.

class_instances.sop
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.

class_inheritance.sop
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.

class_collections.sop
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 hawl method definitions only. A declaration such as door x = 1 directly inside a class produces a parser error.
  • dhis is the constructor name and is called automatically by cusub.
  • nafta is 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:

parent_calls.sop
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.

On this page