Ticket #146: VariStreamOverload.patch
File VariStreamOverload.patch, 3.2 KB (added by jonathandavid, 16 years ago) |
---|
Patch to fix the overload resolution algorithm so that now it correctly prefers a vari T overload over T* |
-
Source/Expr.cobra
661 661 if arg.type == param.type 662 662 score += 20 663 663 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 665 668 else if arg.type.nonNil.isAssignableTo(param.type) 666 669 # Cobra's code and data flow analysis sometimes leaves us with a nilable type that's not actually nil anymore 667 670 # due to an assignment, possibly wrapped in an if statement. Eventually this will be corrected, but for now -
Source/Types.cobra
1651 1651 def isAssignableTo(type as IType) as bool is override 1652 1652 if base.isAssignableTo(type) 1653 1653 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)" ?? 1654 1658 if type inherits ArrayType 1655 1659 if _wrappedType == type.theWrappedType 1656 return true 1660 return true # TODO: wouldn't it be faster to make "return _wrappedType == type.theWrappedType" ?? 1657 1661 if type inherits StreamType 1658 1662 return .isAssignableTo(type.box to !) 1659 1663 return false … … 1677 1681 return member 1678 1682 1679 1683 1684 # TODO: Why all the indirections? Vari[Nilable[Array[T]]]? 1685 # Can't we just inherit Vari from Array? 1680 1686 class VariType 1681 1687 is partial 1682 1688 inherits ArrayType … … 1696 1702 1697 1703 get innerType as IType? is override 1698 1704 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) 1699 1716 1700 1717 def isEquatableTo(t as IType) as bool is override 1701 1718 # vari type inherits IList<of> -
Tests/120-classes/115-overload-stream.cobra
1 class 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