""" CheckInheritanceAndImplementation.cobra To check if a value inherits a particular class, use the `inherits` operator which gives a boolean value (true or false): isACar = vehicle inherits Car To check if a value implements a particular interface, use the `implements` operator: isComparable = x implements IComparable If you use these in an `if` statement with a variable, you can treat the variable as the given class or interface with no explicit typecasting: if vehicle inherits Car vehicle.drive carCrusher.crush(vehicle) else if vehicle inherits Plane vehicle.fly airplaneGraveyard.take(vehicle) Of course, it would be nice if Car and Plane were derived from a common Vehicle class with an `operate` method, but sometimes you don't have control over the class hierarchy you are working with. """ class BankAccount def deposit(x as Object) if x inherits Money _balance += x.amount else if x inherits decimal _balance += x else if x inherits int _balance += x else throw FallThroughException(x) # ^ In a way this is a bad example. Since we have control over the code, # providing method overloads of `deposit` would make the BankAccount interface # more clear and the program run faster. def deposit2(x) # dynamic typed arg if x inherits Money _balance += x.amount else _balance = _balance + x # woops, += isn't implemented for dynamic yet get balance from var as decimal class Money cue init(amount as decimal) base.init _amount = amount get amount from var as decimal class Program def main ba = BankAccount() ba.deposit(10) ba.deposit(Money(10)) assert ba.balance == 20