Wiki

Ticket #146: VariStreamOverload.patch

File VariStreamOverload.patch, 3.2 KB (added by jonathandavid, 15 years ago)

Patch to fix the overload resolution algorithm so that now it correctly prefers a vari T overload over T*

  • Source/Expr.cobra

     
    661661                    if arg.type == param.type 
    662662                        score += 20 
    663663                    else if arg.canBeAssignedTo(param.type) 
    664                         score += 10 
     664                        if param.type inherits VariType   # Rational: T[] -> vari T should be preferred over T[] -> T*,  
     665                            score += 15                   # although both satisfy the "assignability" condition 
     666                        else 
     667                            score += 10 
    665668                    else if arg.type.nonNil.isAssignableTo(param.type)  
    666669                        # Cobra's code and data flow analysis sometimes leaves us with a nilable type that's not actually nil anymore 
    667670                        # due to an assignment, possibly wrapped in an if statement. Eventually this will be corrected, but for now 
  • Source/Types.cobra

     
    16511651    def isAssignableTo(type as IType) as bool is override 
    16521652        if base.isAssignableTo(type) 
    16531653            return true 
     1654        if type inherits VariType   
     1655            # A[] is assignable to "vari B" if A assignable to B 
     1656            if _wrappedType.isAssignableTo(type.trueInner)  
     1657                return true       # TODO: wouldn't it be faster to make "return _wrappedType.isAssignableTo(type.trueInner)" ?? 
    16541658        if type inherits ArrayType 
    16551659            if _wrappedType == type.theWrappedType 
    1656                 return true 
     1660                return true       # TODO: wouldn't it be faster to make "return _wrappedType == type.theWrappedType" ?? 
    16571661        if type inherits StreamType 
    16581662            return .isAssignableTo(type.box to !) 
    16591663        return false 
     
    16771681        return member 
    16781682 
    16791683 
     1684# TODO: Why all the indirections? Vari[Nilable[Array[T]]]?  
     1685# Can't we just inherit Vari from Array? 
    16801686class VariType 
    16811687    is partial 
    16821688    inherits ArrayType 
     
    16961702 
    16971703    get innerType as IType? is override 
    16981704        return _wrappedType 
     1705         
     1706    get trueInner as IType           
     1707        # TODO: this does not seem especially fast: cache it 
     1708        inner1 = .theWrappedType 
     1709        assert inner1.getType == NilableType 
     1710        if inner1 inherits NilableType 
     1711            inner2 = inner1.theWrappedType 
     1712            assert inner2.getType == ArrayType 
     1713            if inner2 inherits ArrayType 
     1714                return inner2.theWrappedType 
     1715        throw FallThroughException(this)  
    16991716 
    17001717    def isEquatableTo(t as IType) as bool is override 
    17011718        # vari type inherits IList<of> 
  • Tests/120-classes/115-overload-stream.cobra

     
     1class X 
     2     
     3    # this tests one aspect of the overload resolution algorithm 
     4    # namely, that the vari overload  is preferred  
     5    # over the stream overlad when passing an array 
     6    # (see Expr.cobra, method _computeBestOverload) 
     7     
     8    def foo(stream as int*) is shared 
     9        assert false 
     10  
     11    def foo(v as vari int) is shared 
     12        pass 
     13 
     14    # just in case, test it as well with a non-primitive 
     15    # type and instance methods 
     16     
     17    def bar(stream as String*) 
     18        assert false 
     19  
     20    def bar(v as vari String) 
     21        pass 
     22 
     23    def main is shared 
     24         
     25        X.foo(@[1, 2, 3]) 
     26        X().bar(@['hi', 'there']) 
     27         
     28         
     29         
     30     
     31