Dart mixin specification doesn’t allow interface implementation

This tracker is for issues related to:

  • Analyzer

Env:

$ flutter --version
Flutter 1.12.13+hotfix.6 • channel unknown • unknown source
Framework • revision 18cd7a3601 (9 weeks ago) • 2019-12-11 06:35:39 -0800
Engine • revision 2994f7e1e6
Tools • Dart 2.7.0

I also confirmed the issue on Dartpad.


Shortest code for representation is below:

abstract class Y {
  void hello();
}

mixin X on Y {
  void callHello() => hello();
}

// error!
class YX with X implements Y {
  @override
  void hello() => print('hello YX');
}

void main() {
  final yx = YX();
  yx.callHello();
}

class YX with X implements Y got an error say

‘X’ can’t be mixed onto ‘Object’ because ‘Object’ doesn’t implement ‘Y’.
Try extending the class ‘X’.dart(mixin_application_not_implemented_interface)

That message sounds pretty weird for me so I guess it might be a bug?
Looks like mixin restriction refers a superclass of “YX” so it claims “can’t be mixed onto ‘Object'”.

To convince analyzer I have to create a class which implement the interface then extends it like below:

class YI implements Y {
  @override
  void hello() => print('hello YI');
}

class YIX extends YI with X {}

My thoughts class YX with X implements Y and class YIX extends YI with X are fundamentally same as type level and also behavior.

Author: Fantashit

1 thought on “Dart mixin specification doesn’t allow interface implementation

  1. This is working as intended. class YX with X .. means class YX extends Object with X .., and the on clause specifies a constraint on the superclass that X is combined with in a ‘mixin application’ operation. And Object does indeed not implement Y.

    Maybe you did not intend to specify a requirement on the superclass that X can be mixin-applied to? The purpose of an on clause in a mixin is basically to ensure that you can perform certain super invocations, like super.hello(). If you just want to ensure that the members of Y are available such that they can be called on this in the body of X then you should use mixin X implements Y.

Comments are closed.