Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixins shadow super methods #12456

Closed
blois opened this issue Aug 14, 2013 · 11 comments
Closed

Mixins shadow super methods #12456

blois opened this issue Aug 14, 2013 · 11 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@blois
Copy link
Contributor

blois commented Aug 14, 2013

Given the following class hierarchy:
class Superclass {
  void init() {
    print('init super!');
  }
}

class Subclass extends Superclass with Mixin {
  void init() {
    print('init sub!');
    super.init();
  }
}

class Mixin {
  void init() {
    print('init mixin!');
  }
}

Is it possible to call Superclass.init() when Subclass.init() is invoked? The Mixin class is prevented from calling super.init().

@gbracha
Copy link
Contributor

gbracha commented Aug 14, 2013

Nice. Of course, the restriction on mixins is an unfortunate expediency that will be relaxed at some point. In the meantime, this is a regrettable consequence. I think the correct thing to do is file a big against the restriction on mixins and then close this as working as intended.


Added Accepted label.

@blois
Copy link
Contributor Author

blois commented Aug 14, 2013

I encountered this while porting code to new DOM Custom Elements, I believe that getting an implicit initializer call will alleviate the need for the problematic initialization pattern.

So yes, hopefully this won't cause any pain.

@DartBot
Copy link

DartBot commented Nov 15, 2013

This comment was originally written by @a14n


What should be the output in the original example when init() is called on SubClass ? I expect the following :

  init sub!
  init mixin!

To get the following output :

  init sub!
  init mixin!
  init super!

I think Mixin should be :

class Mixin implements Superclass { // implements
  void init() {
    print('init mixin!');
    super.init(); // super call
  }
}

Unfortunatly using super in mixin is not allowed for now.

I think this kind of mixin (with implements and super. call) could be really useful and there're several place where I could use this pattern.

@gbracha
Copy link
Contributor

gbracha commented Nov 15, 2013

Alexandre, you are correct. I hope to relax these restrictions in time. They are only their for implementation reasons.

@sgjesse
Copy link
Contributor

sgjesse commented Jan 23, 2014

Issue #16256 has been merged into this issue.

@blois blois added Type-Defect area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jan 23, 2014
@a14n
Copy link
Contributor

a14n commented Jun 4, 2015

The DEP to remove some of restrictions on mixins is available at https://github.com/gbracha/lessRestrictedMixins/blob/master/proposal.md

@kevmoo kevmoo added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed priority-unassigned labels Feb 29, 2016
@Zectbumo
Copy link

Zectbumo commented Apr 4, 2016

No the answer is not as simple as allowing mixins to call super. There needs to be a way to access the individual super/mixin classes because you may not have access to edit those super/mixin classes to add the needed super call. One can't expect that ALL mixin methods in ALL libraries are going to have a super call either. There most likely won't be a super call in these methods. There will be cases where you can't edit the mixin method to add your needed super call. Then what?? Simply allowing super to be called is not a complete solution.

@Zectbumo
Copy link

Zectbumo commented Apr 4, 2016

Hopefully you guys can come up with a better elegant syntax than me but I'll throw out an idea to get people thinking.
If you want the output to be:

init sub!
init mixin!
init super!

then my made up example syntax could be:

class Subclass extends Superclass with Mixin {
  void init() {
    print('init sub!');
    (Mixin)super.init();
    (Superclass)super.init();
  }
}

This makes things simple. 1) You don't get caught up in unknown and unclear super call paths. 2) It gives you access to the required super classes you want. 3) Easy to read and understand what is going on. 4) And is always available to edit since you have access to edit your Subclass source code any way you wish. Think about it and get back to me please.

@Zectbumo
Copy link

Zectbumo commented Apr 6, 2016

I created #26194 to show more problems with super

@munificent
Copy link
Member

While it's not implemented everywhere yet, the language now supports super calls in mixins.

@munificent
Copy link
Member

Hmm, I guess it's still not done, so I'll consider this a duplicate of that main bug: #23770

@munificent munificent added closed-duplicate Closed in favor of an existing report and removed Resolution: fixed labels Dec 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

8 participants