Skip to content

Instantly share code, notes, and snippets.

@hereisderek
Last active September 11, 2018 15:34
Show Gist options
  • Save hereisderek/d7319c36b930b063359a13d8558b38d7 to your computer and use it in GitHub Desktop.
Save hereisderek/d7319c36b930b063359a13d8558b38d7 to your computer and use it in GitHub Desktop.
dart 'with' mixin classes
abstract class SayI{
String say();
String get str;
}
abstract class SayMixinA implements SayI{
String get str;
String say() {
print('SayMixinA:say:print runtimeType: $runtimeType str: $str');
return 'SayMixinA:say:string runtimeType: $runtimeType str: $str';
}
}
abstract class SayMixinB implements SayI{
String get str;
String say() {
print('SayMixinB:say:print runtimeType: $runtimeType str: $str');
return 'SayMixinB:say:string runtimeType: $runtimeType str: $str';
}
}
class SayImplA extends SayI with SayMixinA, SayMixinB{
final String str = 'str-SayImplA';
SayImplA(){
print('SayImplA:constructor...');
print('SayImplA:constructor return: ${say()}');
}
}
class SayImplB extends SayI with SayMixinB, SayMixinA{
final String str = 'str-SayImplB';
SayImplB(){
print('SayImplB:constructor...');
print('SayImplB:constructor return: ${say()}');
}
}
void main() {
print(' ====================== ');
final SayI sayImplA = SayImplA();
print('\n\n\n\n ====================== ');
final SayI sayImplB = SayImplB();
}
======================
SayImplA:constructor...
SayMixinB:say:print runtimeType: SayImplA str: str-SayImplA
SayImplA:constructor return: SayMixinB:say:string runtimeType: SayImplA str: str-SayImplA
======================
SayImplB:constructor...
SayMixinA:say:print runtimeType: SayImplB str: str-SayImplB
SayImplB:constructor return: SayMixinA:say:string runtimeType: SayImplB str: str-SayImplB
@hereisderek
Copy link
Author

hereisderek commented Sep 11, 2018

dart --version: Dart VM version: 2.1.0-dev.4.0 (Fri Sep 7 16:44:38 2018 +0200) on "macos_x64"

order of mixin class matters, it's executed from backward. and once it found the requested method, it won't further execute the corresponding method that's implemented by other mixin class
(but is this intended behavior? I for one would expect that all the mixins will be executed, though the results/returned value being overridden is kinda as expected)

run it in dart pad yourself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment