Page 1 of 1

explicit disambiguation same name from multiple interfaces

PostPosted: Wed Sep 08, 2010 8:56 pm
by nevdelap
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

Re: explicit disambiguation same name from multiple interfac

PostPosted: Wed Sep 08, 2010 11:57 pm
by Charles
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.

Re: explicit disambiguation same name from multiple interfac

PostPosted: Thu Sep 09, 2010 12:16 pm
by nevdelap
Cool thanks. I was searching for the wrong words - explicit, disambiguation, etc. :)

Re: explicit disambiguation same name from multiple interfac

PostPosted: Thu Sep 09, 2010 12:57 pm
by nevdelap
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.

Re: explicit disambiguation same name from multiple interfac

PostPosted: Fri Sep 10, 2010 10:59 pm
by Charles
I agree this should compile. I have a forthcoming fix.

Re: explicit disambiguation same name from multiple interfac

PostPosted: Sat Sep 11, 2010 10:50 am
by Charles
Fixed.