Object Interfaces
Duck Typing
If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands.
— Douglas Adams
Like PHP, THT is a dynamic language, so an object’s class or type does not need to be checked before you can call its methods.
This technique is called Duck Typing, because if an object “walks like a duck, and talks like a duck... then it’s a duck.”
This allows you to use objects from different classes in the same way, as long as they define the same methods.
Example
In this example, two classes have a talk
method.
class Cat { fn talk: print('>Meow<') } class Coder { fn talk: print('1001010011101') } // Another File //--------------------------------------- fn greet($creature) { // Any object that has a 'talk' method will work. $creature.talk() } $kitty = Cat() greet($kitty) //= '>Meow<' $kody = Coder() greet($kody) //= '1001010011101'
Meta-Method Checks
You can also use meta-methods like zHasMethod
to explicitly check if an object has the given method.
fn greet($creature) { if $creature.zHasMethod('talk') { $creature.talk() } }
Interfaces
TODOTHT will support interfaces in the future. This section coming soon.