Wiki

Ticket #162: array-coVar.patch

File array-coVar.patch, 3.9 KB (added by hopscc, 15 years ago)
  • Source/Types.cobra

     
    16651665        if type inherits ArrayType 
    16661666            if _wrappedType == type.theWrappedType 
    16671667                return true 
     1668            # arrays assignable to arrays if element assignable (covariance) 
     1669            if _wrappedType.isAssignableTo( type.theWrappedType) 
     1670                return true  
    16681671        if type inherits StreamType 
    1669             return .isAssignableTo(type.box to !) 
     1672            #trace 'array assignable to streamType' 
     1673            if .isAssignableTo(type.box to !) 
     1674                return true 
     1675            # arrays being IEnumerable assignable to stream/IEnumerable if element assignable    
     1676            if _wrappedType.isAssignableTo( type.innerType to !) 
     1677                return true  
    16701678        return false 
    16711679 
    16721680    def _bindInh 
  • Source/Container.cobra

     
    247247                return true 
    248248            if type inherits NilableType 
    249249                return .isAssignableTo(type.theWrappedType to passthrough)  # CC: weird cast. bug. has to do with if-inherits and invoking the same method 
    250             result_ = .isDescendantOf(type) # CC: rename to result 
    251             return result_ 
     250            result = .isDescendantOf(type) 
     251            return result 
    252252 
    253253    # CC: duplicated from Type 
    254254    def isComparableTo(t as IType) as bool 
  • Tests/820-errors/600-other/140-coVariance-not.cobra

     
     1# Assignment from an array (to stream and array) for Types that are unrelated - expected to fail compilation 
     2class CoVar 
     3    shared 
     4        var foo2 as IFoo* 
     5     
     6        var afoo2 as IFoo[] 
     7         
     8        def main 
     9            #array to stream covariance - - except they are unrelated 
     10            CoVar.foo2 = @[BarNoFoo(), BarNoFoo()] # .error. cannot assign value of type BarNoFoo[] on the right to IFoo* on the left 
     11             
     12            # array to array covariance - except they are unrelated 
     13            CoVar.afoo2 = @[BarNoFoo(), BarNoFoo()] # .error. Cannot assign value of type BarNoFoo[] on the right to IFoo[] on the left  
     14             
     15 
     16interface IFoo 
     17    pass 
     18 
     19class Bar 
     20    implements IFoo 
     21    pass 
     22     
     23interface INoFoo 
     24    pass 
     25 
     26class BarNoFoo 
     27    implements INoFoo 
     28    pass 
     29     
  • Tests/200-misc/830-coVariance-from-array.cobra

     
     1# Assignment from an array (to stream and array) for element Types that are related  
     2#  
     3class CoVar 
     4    shared 
     5        var fooSingleNil as IFoo? 
     6        var foo as IFoo* 
     7        var fooNil as IFoo*? 
     8     
     9        var afoo as IFoo[] 
     10        var afooNil as IFoo[]? 
     11         
     12        def main 
     13            CoVar.fooSingleNil = Bar() 
     14             
     15            #array to stream covariance 
     16            CoVar.foo = @[Bar(), Bar()]  
     17            CoVar.fooNil = @[Bar(), Bar()]  
     18             
     19            # array to array covariance 
     20            CoVar.afoo = @[Bar(), Bar()]  
     21            CoVar.afooNil = @[Bar(), Bar()]  
     22         
     23            # should stream to stream also be allowed? 
     24            #CoVar.foo = [Bar(), Bar()]   
     25             
     26             
     27 
     28interface IFoo 
     29    pass 
     30 
     31class Bar 
     32    implements IFoo 
     33    pass 
     34     
     35     
  • Developer/IntermediateReleaseNotes.text

     
    4242    def foo(p as Type has Attribute(args))  # attribute applies to parameter 
    4343        has return Attribute(args)  # attribute applies to return value due to `return` keyword 
    4444 
     45* Support array coVariance from arrays to arrays and streams  ticket:162 
    4546 
    4647================================================================================ 
    4748Library