Dart: Mixins vs Static Methods

1,812

Mixins vs Static member is like Black vs White. They do the opposite.

Members of a mixin are linked to one specific instance of an object. But static members are common to all objects

If it made sense to implement something like a static function, then it likely means that a mixin is not what you want. It'll just make the object bloated and slower to instantiate.

Share:
1,812
Shahin Mursalov
Author by

Shahin Mursalov

Updated on December 10, 2022

Comments

  • Shahin Mursalov
    Shahin Mursalov over 1 year

    Is using mixins a better practice than using static methods?

    For example:

    • we can create a Utils class, put static methods in it and then use them like Utils.print().
    • or we can create a UtilsMixin class, access it using "with" keyword and just call print().

    How do these two methods compare to each other? Which one is the way to go?

  • Shahin Mursalov
    Shahin Mursalov about 5 years
    Can you tell me a simple case when I would prefer mixins over static methods? and what do you mean by "Members of a mixin are linked to one specific instance of an object. But static members are common to all objects". Can you explain more?
  • Jesse Adam
    Jesse Adam over 4 years
    from what I gather Remi is saying that AFTER you instantiate a class you can add a method to it and then call it and I imagine the method you attach might come from a static method. Obviously the method is going to expect that all the variable names are defined in the class it's being appended to. Good example here: javascript.info/mixins