Calling mixin "super" methods

1,540 views
Skip to first unread message

Steven Roose

unread,
Feb 26, 2014, 12:13:57 PM2/26/14
to mi...@dartlang.org
I have a question regarding mixins.

With regular inheritance, when you override a method of your superclass, you can call the original method of the superclass using the super.method() construct.

Is a similar thing possible for mixins?

On line 23 you can see what I'm looking for.

If it's not possible, was it ever considered? Is there an open issue for it?

THanks

Steven

Ivan Zaera Avellon

unread,
Feb 26, 2014, 12:34:42 PM2/26/14
to mi...@dartlang.org
I think it's forbidden. Read here: https://www.dartlang.org/articles/mixins/#syntax-and-semantics

===

However, in this proposal, a mixin may only be extracted from a class that obeys the following restrictions:

The class has no declared constructors.
The class’ superclass is Object.
The class contains no super calls.

===

Sorry ;-).



El 26/02/14 18:13, Steven Roose escribió:
> --
> 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.

signature.asc

Steven Roose

unread,
Feb 26, 2014, 12:40:24 PM2/26/14
to mi...@dartlang.org
Those restrictions are for Mixins themselves. A mixin should be a top-class in the inheritance tree just under Object and so it should not call super methods.

In my example, I want to call a super method in a class that uses a Mixin. Not in the mixin itself.
If you look at my example, the restrictions you linked to would forbid it to call a super method on line 9 f.e.

But still thanks for the fast reply, hopefully people don't consider my case solved when reading your reply :)

Bob Nystrom

unread,
Feb 26, 2014, 12:53:06 PM2/26/14
to General Dart Discussion

On Wed, Feb 26, 2014 at 9:13 AM, Steven Roose <steve...@gmail.com> wrote:
Is a similar thing possible for mixins?

On line 23 you can see what I'm looking for.

If it's not possible, was it ever considered? Is there an open issue for it?

It sure is:

class Superclass {
  void doIt() {
    print("Superclass.doIt");
  }
}

class MixinClass {
  void doMixinThing() {
    print("MixinClass.doMixinThing");
  }
}

class Subclass extends Superclass with MixinClass {
  void doIt() {
    print("Subclass.doIt");
    super.doIt();
  }

  void doMixinThing() {
    print("Subclass.doMixinThing");
    super.doMixinThing();
  }
}

main() {
  var sub = new Subclass();
  sub.doIt();
  sub.doMixinThing();
}

This prints:

Subclass.doIt
Superclass.doIt
Subclass.doMixinThing
MixinClass.doMixinThing

Cheers!

- bob

Steven Roose

unread,
Feb 26, 2014, 1:02:16 PM2/26/14
to mi...@dartlang.org
Hmm, I guess a requirement for this is that SuperClass does not define a method with the same name?
What happens on a conflict?

Thanks!

Steven


--

Bob Nystrom

unread,
Feb 26, 2014, 1:57:47 PM2/26/14
to General Dart Discussion
On Wed, Feb 26, 2014 at 10:02 AM, Steven Roose <steve...@gmail.com> wrote:
I guess a requirement for this is that SuperClass does not define a method with the same name?

No, that's not a problem either, except that you won't be able to get to that method any more.
 
What happens on a conflict?

The mixin wins. You can basically read the inheritance chain from right to left. Say you have:

class A extends B with C, D, E {
  void foo() {}
}

When you call foo(), it looks it up in A (i.e. inside the class body, which is rightmost) then E, D, C, and finally B and up B's inheritance chain.

Cheers!

- bob

Lasse R.H. Nielsen

unread,
Feb 26, 2014, 1:58:36 PM2/26/14
to mi...@dartlang.org
On Wed, Feb 26, 2014 at 7:02 PM, Steven Roose <steve...@gmail.com> wrote:
Hmm, I guess a requirement for this is that SuperClass does not define a method with the same name?
What happens on a conflict?

When you mix in a class, you extend the superclass with the mixin to create a new class.
The restrictions on mixins does not change anything, except which classes you can use as a mixin. 
If the superclass and the mixin both define a member with the same name, then the one from te mixin overrides the one in the superclass.

Example:
  
class C {
  int foo() => 42;
}

class M {
  int foo() => 37;
}

class D extends C with M {
  int foo() => super.foo();  // returns 37.
}


/L
--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Gilad Bracha

unread,
Feb 26, 2014, 1:59:23 PM2/26/14
to General Dart Discussion
Mixin inheritance works just like regular inheritance. When you mixin in a class, you get a single inheritance chain, and overriding works exactly the same as normal inheritance.

The restrictions are on what kind of class can be mixed in. They don't impact the behavior if mixing in the class is allowed. The plan is to remove all these these restrictions in the future. 


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



--
Cheers, Gilad

Steven Roose

unread,
Feb 26, 2014, 7:17:18 PM2/26/14
to mi...@dartlang.org
The way mixin inheritance works surprises me, but it seems perfectly logical, I just didn't think of it this way.
Many thanks for the helpful responses!

Regards
Steven
Reply all
Reply to author
Forward
0 new messages