Page 1 of 1

classes inherit functions from other classes

Posted: 19 Sep 2018, 15:41
by iseahound
Alternatively titled, "Objects inherit functions from other objects"

Code: Select all

a := big.small ; "new" is optional here.
a.set("hey", "you") ; polymorphic!
MsgBox % a.hey

class big {

   class small {
      static set := ObjBindMethod(big, "set") ; object is the name of the class.
      ; Note that we don't need to bind "key", or "value", because we can specify those parameters. 
      ; If you remove the static declaration, then the new keyword is required. 
   }

   set(self, key, value){ ; A function that is bound must contain "self" as its first parameter. This is because the 1st parameter is the calling object. 
      ; The reason you don't see "self" in a normal declaration is because there is ownership in a normal declaration. 
      ; ownership means you get to use the word "this". If you used "this" in this function, you'd be referring to class big!
      self[key] := value ; Note self[key] and not self.key because we want to elevate a string to the name of a variable. 
      return self ; Always returns itself!
   }
}
The above code allows chaining of set().

As in you can construct an object like person.set("hair", "blonde").set("eyes", "green").set("freckles", true)
as opposed to person := {"hair":"blonde", "eyes":"green", "freckles":true}