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 dynamically typed language. This means that an object’s class or type does not need to be checked before you can call its methods.
This lets you use objects from different classes in the same way, as long as they define the same methods.
This is also referred to as Duck Typing, because if an object “walks like a duck, and talks like a duck... then it’s a duck.”
Example
In this example, two classes have a talk
method.
class Cat { fun talk: print('>Meow<') } class Coder { fun talk: print('1001010011101') } // Another File //--------------------------------------- fun 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.
fun greet($creature) { if $creature.zHasMethod('talk') { $creature.talk() } }
Interfaces
TODOTHT will support interfaces in the future. This section coming soon.