Wiki

root/cobra/trunk/HowTo/220-UseDynamicTyping.cobra

Revision 1983, 1.5 KB (checked in by Chuck.Esterbrook, 3 years ago)

Update How To's to match the current Cobra syntax and feature set.

  • Property svn:eol-style set to native
Line 
1"""
2Cobra supports both static and dynamic typing.
3
4See also: http://cobra-language.com/docs/release-notes/Cobra-0.5.0.html
5"""
6
7class Person
8
9    get name as String
10        return 'Blaise'
11
12
13class Car
14
15    get name as String
16        return 'Saleen S7'
17
18
19class Program
20
21    shared
22
23        def main
24            assert .add(2, 3) == 5
25            assert .add('Hi ', 'there.') == 'Hi there.'
26            .printName(Person())
27            .printName(Car())
28
29        def add(a, b) as dynamic
30            return a + b
31
32        def printName(x)
33            print x.name  # dynamic binding
34
35
36class Notes
37
38    def add(a, b) as dynamic
39        return a + b
40        # + flexible (any type with "+" operator works)
41        #   + fast prototyping
42        #   + less brittle wrt other software components that change unpredictably
43        # + more reusable
44        # - errors detected late (run-time)
45        # - one error reported at a time (the first one that throws an exception)
46        # - slow at run-time
47        # - fat at run-time (values must carry type information; boxing)
48        # - difficult IDE support
49        #   (Intellisense/autosuggestion requires complex analysis and/or execution of code)
50
51    def add(a as decimal, b as decimal) as decimal
52        return a + b
53        # - inflexible
54        #   - slower coding / more typing
55        #   - more brittle (to change program to `float` you must find and replace everywhere)
56        # - less reusable (cannot use with int and float)
57        # + errors detected early (compile-time)
58        # + multiple errors reported (every one that the compiler can find)
59        # + fast at run-time
60        # + slim at run-time (values need only carry data)
61        # + easy IDE support (Intellisense/autosuggestion)
Note: See TracBrowser for help on using the browser.