mixins - forEach

51 views
Skip to first unread message

Massimiliano Tomassoli

unread,
Apr 18, 2014, 10:47:55 AM4/18/14
to mi...@dartlang.org
Hi,
the article Mixins in Dart shows the following example:

  abstract class Collection<E> {
    Collection<E> newInstance();
    Collection<E> map((f) {
      var result = newInstance();
      forEach((E e) { result.add(f(e)); });
      return result;
    }
  }

  abstract class DOMElementList<E> = DOMList with Collection<E>;

  abstract class DOMElementSet<E> = DOMSet with Collection<E>;

  // ... 28 more variants

I don't understand where forEach comes from. Shouldn't it be a method of Collection<E>?
BTW, it seems there's one parenthesis too many on the third line.

Gilad Bracha

unread,
Apr 18, 2014, 1:13:20 PM4/18/14
to General Dart Discussion
On Fri, Apr 18, 2014 at 7:47 AM, Massimiliano Tomassoli <kiuh...@gmail.com> wrote:
Hi,
the article Mixins in Dart shows the following example:

  abstract class Collection<E> {
    Collection<E> newInstance();
    Collection<E> map((f) {
      var result = newInstance();
      forEach((E e) { result.add(f(e)); });
      return result;
    }
  }

  abstract class DOMElementList<E> = DOMList with Collection<E>;

  abstract class DOMElementSet<E> = DOMSet with Collection<E>;

  // ... 28 more variants

I don't understand where forEach comes from. Shouldn't it be a method of Collection<E>?

Yes, it should, but it is elided due to lack of space. This should be clarified. 

BTW, it seems there's one parenthesis too many on the third line.

Yes. Thanks for catching this. 
  


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.



--
Cheers, Gilad

Gilad Bracha

unread,
Apr 18, 2014, 1:16:49 PM4/18/14
to General Dart Discussion

Justin Fagnani

unread,
Apr 18, 2014, 1:44:27 PM4/18/14
to General Dart Discussion
On Fri, Apr 18, 2014 at 7:47 AM, Massimiliano Tomassoli <kiuh...@gmail.com> wrote:
Hi,
the article Mixins in Dart shows the following example:

  abstract class Collection<E> {
    Collection<E> newInstance();
    Collection<E> map((f) {
      var result = newInstance();
      forEach((E e) { result.add(f(e)); });
      return result;
    }
  }

  abstract class DOMElementList<E> = DOMList with Collection<E>;

  abstract class DOMElementSet<E> = DOMSet with Collection<E>;

  // ... 28 more variants

I don't understand where forEach comes from. Shouldn't it be a method of Collection<E>?

Not necessarily. The assumption here is probably that the class mixing Collection in has the forEach() implementation, as DOMList might. They probably should have added an abstract forEach() declaration to Collection to be more clear and as a contractor for users, but at one point abstract methods would throw a NoSuchMethodError even if a super class implemented them. Luckily that's been fixed.

Another option would be for a mixin class to implement an interface, like Iterable<E> in this case.
 

BTW, it seems there's one parenthesis too many on the third line.

 
Reply all
Reply to author
Forward
0 new messages