Home > Net >  is mixin "on" a restriction or an inheritance mechanism?
is mixin "on" a restriction or an inheritance mechanism?

Time:03-27

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

CodePudding user response:

Here is the excerpt from the Dart Language tour:

Sometimes you might want to restrict the types that can use a mixin. For example, the mixin might depend on being able to invoke a method that the mixin doesn’t define. As the following example shows, you can restrict a mixin’s use by using the on keyword to specify the required superclass:

class Musician {
  // ...
}
mixin MusicalPerformer on Musician {
  // ...
}
class SingerDancer extends Musician with MusicalPerformer {
  // ...
}

In the preceding code, only classes that extend or implement the Musician class can use the mixin MusicalPerformer. Because SingerDancer extends Musician, SingerDancer can mix in MusicalPerformer.

Solely based on this, the on for mixins is more of a restriction mechanism rather than inheritance. However, since you know exactly that this restricted mixin will be used on a specific class (type), you can use its variables and methods. When you think about it, it's the same with the typecast or even null safety - if you ensure that the object is of a specific type, you can use its methods.

Also, here is a great explanation of how the mixins work.

  •  Tags:  
  • dart
  • Related