Forums

explicit disambiguation same name from multiple interfaces

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

explicit disambiguation same name from multiple interfaces

Postby nevdelap » Wed Sep 08, 2010 8:56 pm

Hi fellas,

Wondering how to do this in Cobra...

Code: Select all
   class ReadOnlyList<T> : IEnumerable, IEnumerable<T>
   {
      /* Stuff */

      IEnumerator IEnumerable.GetEnumerator()
      {
         return _list.GetEnumerator();
      }

      IEnumerator<T> IEnumerable<T>.GetEnumerator()
      {
         return _list.GetEnumerator();
      }
   }

If I just specify the methods...

Code: Select all
   class ReadOnlyList<of T> implements IEnumerable, IEnumerable<of T>

      # Stuff

      def getEnumerator as IEnumerator?
         return _list.getEnumerator

      def getEnumerator as IEnumerator<of T>?
         return _list.getEnumerator  # line 48

ReadOnlyList.cobra(48): error: Cannot return IEnumerator? because "getEnumerator" is declared to return a IEnumerator<of T>?.

It's calling the non-generic one in both cases thought though _list is IList<of T>, and so complains in the second case.

If I only define the first one then it compiles, so that part is a bug, in that it should say that the class didn't define getEnumerator from Enumerator<T>. I discovered this because using the assembly from C# blows up gmcs.

I'll raise a ticket on the second issue, but first I just wanted to ask if this disambiguation is implemented or if there's a workaround in the meantime.

Thanks!

PS: If it's not implemented yet this is the syntax that seemed natural to me when I was trying various things to see what might work...

Code: Select all
      def getEnumerator from IEnumerable as IEnumerator?
         return _list.getEnumerator

      def getEnumerator from IEnumerable<of T> as IEnumerator<of T>?
         return _list.getEnumerator
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: explicit disambiguation same name from multiple interfac

Postby Charles » Wed Sep 08, 2010 11:57 pm

There are two How-To's for that:

http://cobra-language.com/how-to/ImplementIEnumerable1/

http://cobra-language.com/how-to/ImplementIEnumerable2/

There are also tests under Cobra/Tests/320-misc-two/*.cobra

You may find it useful to search the Cobra source for such things which will hit both How-To's and Tests.

The eventual plan is something like "cue enumerate as T*" which would work regardless of back-end (.NET, JVM, ObjC, etc.), although the .NET-specific approach will always work on .NET. You can search the forums for "cue enumerate" for discussion.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: explicit disambiguation same name from multiple interfac

Postby nevdelap » Thu Sep 09, 2010 12:16 pm

Cool thanks. I was searching for the wrong words - explicit, disambiguation, etc. :)
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: explicit disambiguation same name from multiple interfac

Postby nevdelap » Thu Sep 09, 2010 12:57 pm

That second issue boils down to this...

Code: Select all
   class ReadOnlyList implements IEnumerable<of int>

      var _records = List<of int>()    # If _records is the List<T> like in the How To it compiles.

      def getEnumerator as IEnumerator<of int>
         return _records.getEnumerator

      def getEnumerator as System.Collections.IEnumerator
         implements System.Collections.IEnumerable
         return .getEnumerator

Code: Select all
   class ReadOnlyList implements IEnumerable<of int>

      var _records as IList<of int>    # If _records is IList<T>...

      cue init
         base.init
         _records = List<of int>()

      def getEnumerator as IEnumerator<of int>
         return _records.getEnumerator    # line 18

      def getEnumerator as System.Collections.IEnumerator
         implements System.Collections.IEnumerable
         return .getEnumerator

ReadOnlyList.cobra(18): error: Cannot return IEnumerator? because "getEnumerator" is declared to return a IEnumerator<of int>.

Does that seem wrong? _records is still the templated type but it appears to call the non-templated getEnumerator.
call me Nev.
nevdelap
 
Posts: 61
Location: Buenos Aires

Re: explicit disambiguation same name from multiple interfac

Postby Charles » Fri Sep 10, 2010 10:59 pm

I agree this should compile. I have a forthcoming fix.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: explicit disambiguation same name from multiple interfac

Postby Charles » Sat Sep 11, 2010 10:50 am

Fixed.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 47 guests