Wiki

Ticket #176: lit-collections-dynamic.patch

File lit-collections-dynamic.patch, 5.8 KB (added by hopscc, 14 years ago)
  • Source/Expr.cobra

     
    28102810                for i in 1 : exprs.count 
    28112811                    exprs[i].bindImp 
    28122812                    type = exprs[i].type.greatestCommonDenominatorWith(type) 
     2813                if type.isSystemObjectClass # make dynamic if heterogenous and all non Object 
     2814                    allObj = all for expr in exprs get expr.type.isSystemObjectClass 
     2815                    if not allObj, type = .compiler.dynamicType  
    28132816            if type inherits NilType, type = .compiler.defaultType 
    28142817            _type = _makeTypeWith(type) 
    28152818 
     
    29682971                keyType   = entries[0][0].type to ! 
    29692972                valueType = entries[0][1].type to ! 
    29702973                i = 1 
     2974                keysAllSysObject = valuesAllSysObject = true 
    29712975                while i < entries.count 
    29722976                    keyType   = entries[i][0].type.greatestCommonDenominatorWith(keyType) 
     2977                    if not entries[i][0].type.isSystemObjectClass, keysAllSysObject = false 
    29732978                    valueType = entries[i][1].type.greatestCommonDenominatorWith(valueType) 
     2979                    if not entries[i][1].type.isSystemObjectClass, valuesAllSysObject = false  
    29742980                    i += 1 
     2981                # make types dynamic if heterogenous and all non Object 
     2982                if keyType.isSystemObjectClass and not keysAllSysObject 
     2983                    keyType = .compiler.dynamicType  
     2984                if valueType.isSystemObjectClass and not valuesAllSysObject 
     2985                    valueType = .compiler.dynamicType  
    29752986            if keyType inherits NilType, keyType = .compiler.defaultType 
    29762987            if valueType inherits NilType, valueType = .compiler.defaultType 
    29772988            _type = .compiler.dictionaryOfType.constructedTypeFor([keyType, valueType]) 
  • Tests/240-generics/500-misc/800-streams.cobra

     
    3737        assert count == 0 
    3838 
    3939        # work with the stream object var 
    40         _stuff = [1, 'x'] 
     40        _stuff = [1, 'x'] to List<of Object> 
    4141        count = 0 
    4242        for x in _stuff 
    4343            assert x in {1, 'x'} 
  • Tests/300-type-inference/900-literals/505-lit-collections-errors.cobra

     
     1# List, Array, Set, Dict  literals not dynamic if all are explict Object type 
     2# Ticket 176 
     3class X 
     4    def main  
     5        a = 'aoeu' to Object  
     6        b = 5 to Object  
     7        #List 
     8        l = [a, b]  # List<of Object> 
     9        x = l[0] 
     10        y = l[1] 
     11        assert x.length == 4 # .error. type is "Object"      
     12        assert y*2 == 10     # .error. Cannot apply STAR to Object and int 
     13         
     14        #array 
     15        l2 = @[a, b]    # List<of Object> 
     16        x1 = l2[0]  
     17        y1 = l2[1]  
     18        assert x1.length == 4   # .error. type is "Object"       
     19        assert y1*2 == 10       # .error. Cannot apply STAR to Object and int 
     20         
     21        # dict 
     22        d = {0:a, 1:b}  # Dict<of int,Object> 
     23        x2 = d[0]  
     24        y2 = d[1]  
     25        assert x2.length == 4 # .error. type is "Object"         
     26        assert y2 * 2 == 10   # .error. Cannot apply STAR to Object and int 
     27         
  • Tests/300-type-inference/900-literals/500-lit-collections-dynamic.cobra

     
     1# List, Array, Set, Dict literals dynamic if heterogenous type and not object 
     2# ticket 176 
     3class DynLitSeq 
     4    def main  
     5        .testList 
     6        .testArray 
     7        .testSet 
     8        .testDict 
     9        .testStream 
     10         
     11    def testList     
     12        a = 'aoeu' to Object  
     13        b = 5 to Object  
     14        l = [a to String, b to int]     # List<of HeterogenousObject> 
     15     
     16        x = l[0]  
     17        y = l[1]  
     18        assert x.length == 4  
     19        assert y * 2 == 10 
     20         
     21        l1 = ['ae', 'aeiaei' ] # List<of String> 
     22        x1=l1[0] 
     23        assert x1.length == 2 
     24        assert l1[1].length == 6 
     25     
     26    def testArray    
     27        a = 'aoeu' to Object  
     28        b = 5 to Object  
     29        l = @[a to String, b to int]    # Array<of HeterogenousObject> 
     30     
     31        x = l[0]  
     32        y = l[1]  
     33        assert x.length == 4  
     34        assert y * 2 == 10 
     35     
     36    def testSet 
     37        a = 'aoeu' to Object  
     38        b = 5 to Object  
     39        s = {a to String, b to int}     # Set<of HeterogenousObject> 
     40         
     41        assert a in s 
     42        assert b in s 
     43        assert a to String in s 
     44        assert b to int in s 
     45        assert 'z' not in s 
     46         
     47        l = s.toList 
     48        x = l[0]  
     49        y = l[1]  
     50        assert x.length == 4  
     51        assert y * 2 == 10 
     52     
     53    def testDict     
     54        a = 'aoeu' to Object  
     55        b = 5 to Object  
     56        #l as Dictionary<of int,dynamic> = {0:a to String, 1:b to int}  
     57        l = {0:a to String, 1:b to int}     # Dict<of int,HeterogenousObject> 
     58         
     59        x = l[0]  
     60        y = l[1]  
     61        assert x.length == 4  
     62        assert y * 2 == 10 
     63         
     64    def testStream 
     65        stuff as dynamic* 
     66        stuff = [1, 'x'] 
     67        count = 0 
     68        for x in stuff 
     69            assert x in {1, 'x'} 
     70            count += 1 
     71        assert count == 2 
     72 
     73        objStm as Object* 
     74        objStm = [1, 'x'] to List<of Object> # Heterogeneous lists are List<of dynamic> now 
     75        #objStm = [1 to Object, 'x' to Object] # also OK 
     76        count = 0 
     77        for x in objStm 
     78            assert x in {1, 'x'} 
     79            count += 1 
     80        assert count == 2 
     81         
     82         
     83         
  • Developer/IntermediateReleaseNotes.text

     
    414414* Fixed: `a += b` does not work if `a` is dynamically typed and referring to a string. 
    415415 
    416416* Fixed: Partial class merge problem re: inheritance. ticket:67 
     417 
     418* Fixed: Heterogeneous collection literals are inferred as <of Object> instead of <of dynamic>. ticket:176