""" Cobra supports both static and dynamic typing. See also: http://cobra-language.com/docs/release-notes/Cobra-0.5.0.html """ class Person get name as String return 'Blaise' class Car get name as String return 'Saleen S7' class Program shared def main assert .add(2, 3) == 5 assert .add('Hi ', 'there.') == 'Hi there.' .printName(Person()) .printName(Car()) def add(a, b) as dynamic return a + b def printName(x) print x.name # dynamic binding class Notes def add(a, b) as dynamic return a + b # + flexible (any type with "+" operator works) # + fast prototyping # + less brittle wrt other software components that change unpredictably # + more reusable # - errors detected late (run-time) # - one error reported at a time (the first one that throws an exception) # - slow at run-time # - fat at run-time (values must carry type information; boxing) # - difficult IDE support # (Intellisense/autosuggestion requires complex analysis and/or execution of code) def add(a as decimal, b as decimal) as decimal return a + b # - inflexible # - slower coding / more typing # - more brittle (to change program to `float` you must find and replace everywhere) # - less reusable (cannot use with int and float) # + errors detected early (compile-time) # + multiple errors reported (every one that the compiler can find) # + fast at run-time # + slim at run-time (values need only carry data) # + easy IDE support (Intellisense/autosuggestion) |
How To Use Dynamic Typing