Wiki

Changeset 2305

Show
Ignore:
Timestamp:
03/10/10 06:47:00 (2 years ago)
Author:
Chuck.Esterbrook
Message:

Fixed: The compiler does not find valid extension methods in some circumstances.

Location:
cobra/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Source/Boxes.cobra

    r2299 r2305  
    835835        Returns true if this type is a construction of the given generic type def, even going more than level deep through `genericDef`. 
    836836        """ 
     837        # TODO: can this implementation be replaced by: 
     838        # return .constructedTypeOf(box) is not nil 
    837839        if .genericDef is nil 
    838840            return false 
     
    17641766                    if box.isDescendantOf(cto) and .declForName(name) 
    17651767                        return .constructedTypeFor(cto.genericParams).declForName(name) 
     1768            else if .isGeneric 
     1769                # Example: 
     1770                # this == Extension of IEnumerable<of T> 
     1771                # box == X implements IEnumerable<of int> 
     1772                # name == 'toList' 
     1773                # The IEnumerable<of T> has .genericDef IEnumerable<> 
     1774                # The X's IEnumerable<of int> has .genericDef IEnumerable<> 
     1775                # Note that X and the Extension are not directly connected. 
     1776                # Only by tracing up to the generic def IEnumerable<> do they "connect". 
     1777                # see cobra/Tests/420-extensions/400-extend-generics/220-extend-ancestor-interface.cobra 
     1778                constructed = box.constructedTypeOf(_extendedBox.genericDef to !) 
     1779                if constructed, return .extensionMemberFor(constructed, name) 
    17661780        return nil 
    1767              
     1781 
    17681782    def memberForName(name as String) as IMember? 
    17691783        result = base.memberForName(name) 
    1770         if result is nil 
    1771             result = .extendedBox.memberForName(name) 
     1784        if result is nil, result = .extendedBox.memberForName(name) 
    17721785        return result 
    17731786 
  • cobra/trunk/Source/NameSpace.cobra

    r2299 r2305  
    1 interface INameSpaceMember 
    2     inherits IMember, ISyntaxNode 
     1interface INameSpaceMember inherits IMember, ISyntaxNode 
    32    """ 
    43    Anything that can be a member of a namespace should implement INameSpaceMember. 
  • cobra/trunk/Tests/420-extensions/400-extend-generics/220-extend-ancestor-interface.cobra

    r2240 r2305  
    1 # Invoke a method in an extension of a generic, ancestor interface. 
    2 # In this case, the class is Dictionary<of TKey, TValue>.KeyCollection 
    3 # and the extension method is declared on IEnumerable<of TKey> which KeyCollection implements. 
    4  
    5 # Then do it again for Dictionary<of TKey, TValue>.ValueCollection 
    6 # Note that it implement IEnumerable<of TValue> which is using the *2nd* generic parameter, not the first. 
    7  
    81class Test 
    92 
    103    def main 
     4        .test1 
     5        .test2 
     6         
     7    def test1 
     8        # Invoke a method in an extension of a generic, ancestor interface. 
     9        # In this case, the class is Dictionary<of TKey, TValue>.KeyCollection 
     10        # and the extension method is declared on IEnumerable<of TKey> which KeyCollection implements. 
     11 
     12        # Then do it again for Dictionary<of TKey, TValue>.ValueCollection 
     13        # Note that it implement IEnumerable<of TValue> which is using the *2nd* generic parameter, not the first. 
    1114        d = {'x': 1, 'y': 1} 
    1215 
     
    1821            assert value == 1 
    1922        assert (for value in d.values.toList get value) == [1, 1] 
     23 
     24    def test2 
     25        x = X() 
     26        .takeEnum(x) 
     27        y = x.toList  # previously broken. Cobra would not find the extension method. 
     28        assert y == List<of int>(x) 
     29        assert y == [1, 2, 3] 
     30 
     31    def takeEnum(e as IEnumerable<of int>) 
     32        assert e.toList is not e 
     33        assert e.toList == List<of int>(e) 
     34 
     35 
     36class X implements IEnumerable<of int> 
     37 
     38    def getEnumerator as IEnumerator<of int> 
     39        return [1, 2, 3].getEnumerator 
     40 
     41    def getEnumerator as System.Collections.IEnumerator 
     42        implements System.Collections.IEnumerable 
     43        return .getEnumerator