Wiki

Changes between Version 3 and Version 4 of DynamicType

Show
Ignore:
Timestamp:
03/11/14 11:42:14 (11 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DynamicType

    v3 v4  
    11= Dynamic Type = 
    22 
     3 As well as (explicit and inferred) static typing, Cobra supports dynamic typing (or late binding) where type determination is deferred to runtime rather than being specified at coding/compile time. 
     4 
     5Any kind of object, whether primitive, value, reference (or nil), can be used where a dynamic (dynamic?) type is expected. 
     6 
     7Variables, fields, parameters and return types can be explicitly specified as dynamically-typed using the cobra keyword '''dynamic'''.[[BR]] 
     8 
     9If a type is not provided (either as an explicit type clause or as an initialiser expression) on an (otherwise untyped) object variable, method argument or property, the type defaults to dynamic (dynamic?). 
     10 
     11 
     12{{{ 
     13#!cobra 
     14class X 
     15    var x 
     16    # no explicit type given so field x is default typed as dynamic 
     17 
     18    var y = 99 # initialser expression so type inferred from that (int) 
     19    var z as String?  # explicit type specified - nilable String 
     20 
     21    cue init(x)  # arg type x here is also dynamic 
     22        .x = x 
     23 
     24    def echo 
     25        print 'x=[.x] x.getType=[.x.getType]' 
     26}}} 
     27 
     28Local variables are ''not'' defaulted to dynamic type but instead are statically typed with the type inferred 
     29from the righthand side of an (assignment) expression.  
     30Of course, if the right hand expression is of type "dynamic" then so is the variable. 
     31 
     32{{{ 
     33#!cobra 
     34  i = 0 # i inferred as int (from value 0) 
     35  s = 'aString' # s inferred as type String  
     36 
     37  s1 as dynamic # local variable explicitly typed as dynamic/late bound 
     38  s1 = s   # works  - s1 bound at runtime to same type as s  
     39}}} 
     40 
     41 
     42If need be expressions can be cast to dynamic to preserve late binding semantics with dynamic fields/variables 
     43 
     44{{{ 
     45#!cobra 
     46    var x 
     47.... 
     48    .x = .lookItUp(42) to dynamic   
     49}}} 
     50  
     51 
     52Operations on dynamic expressions are late bound like in Smalltalk and Python. 
     53{{{ 
     54#!cobra 
     55    def (x, y) as dynamic 
     56        return (x + y) 
     57 
     58}}} 
     59 
     60 
    361 * See: http://cobra-language.com/how-to/UseDynamicTyping/ 
    4  * "dynamic" is a keyword in Cobra. 
    5  * Any kind of object, whether primitive, value, reference or nil, can be used where dynamic is expected. 
    6  * "dynamic" is the default type of untyped object variables, method arguments and properties. 
    7  * "dynamic" is *not* the default type for local variables which are statically typed as inferred by the right hand side of an expression ("i = 0" makes "i" an "int"). 
    8    * Of course, if the right hand expression is of type "dynamic" then so is the variable. 
    9  * You can also cast an expression to dynamic with `<expr> to dynamic`. 
    10  * Operations on dynamic expressions are late bound like in Smalltalk and Python. 
     62  
    1163 
    1264See also: TypesOverview, LanguageTopics