Access variable "on" class
I need a mixin where I can access a variable or methods, from the class it is being mixin'ed into.
I found that mixin and "on" is doing the job. Now I just need a mental picture to remember why it is working - can you help with that?
Is "on" a restriction or an inheritance mechanism?
First off this error got me started, and it makes sense as both "on" and "with" is, "mental picture", is getting into TestErrorClass, and we only need one, from an inheritance perspective.
class TestErrorClass with TestErrorMixin {}
mixin TestErrorMixin on TestErrorClass {}
Solution
This is the solution to my problem, having a variable I can use, from the class where the mixin is getting into or restricted into.
class TestSuper {
String variableWeNeedToAccessFromTestMixin = "";
// It will get TestMixin from "on" in TestMixin, it just doesn't know it yet.
// So we can not use the mixin here - fair enough.
void HelloTestSuper(){}
}
// We need "with" here has it will create the same error as described above if we put it on TestSuper.
// Also we need it to be able to call "HelloTestMixin()" both from here and in instances created from this "TestBase" class.
class TestBase extends TestSuper with TestMixin {
void main(){
HelloTestSuper();
HelloTestMixin();
}
}
// "on" is restricted to only be able to being mixed into classes of this type,
// and there for, we can rely on what ever variables being in the SuperClass and use it here.
// Note variable "variableWeNeedToAccessFromTestMixin".
mixin TestMixin on TestSuper {
void HelloTestMixin(){
print(variableWeNeedToAccessFromTestMixin);
}
}
Mental picture
Now I can understand the error, "on" and "with", from a inheritance perspective, the mixin can not end up in the same place twice.
But I can only understand the solution from a restricted perspective, that is the mixin is only allowed to mixin into TestSuper.
Inheritance vs restriction
If it is an inheritance issue, then extending from "TestSuper" should include whats being "on"'ed into it. If it is a restriction issue, then why the error in the first place.
Hope you can see what I am thinking, and please ask.
Thanks