Mixins Explained in 3 Minutes (NOT The Same as Multiple Inheritance)

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ม.ค. 2025

ความคิดเห็น • 48

  • @TheCodingGopher
    @TheCodingGopher  19 วันที่ผ่านมา +9

    Thoughts on the new editing style?

    • @debbie8062
      @debbie8062 19 วันที่ผ่านมา +4

      i’m a fan of it 🙂‍↕️

    • @patrickdecabooter411
      @patrickdecabooter411 19 วันที่ผ่านมา +2

      I prefer the old style

    • @joostvisser6508
      @joostvisser6508 19 วันที่ผ่านมา +4

      The fade-in on the subtitles is quite distracting, I'd prefer them to just cut from one line to the next. Otherwise I like it!

    • @yarden-zamir
      @yarden-zamir 19 วันที่ผ่านมา

      Hate the fade in on the subtitles. Means I can't immediately read it

    • @bee_irl
      @bee_irl 19 วันที่ผ่านมา +2

      not a fan of these brainrot tiktok-style captions

  • @Dent42
    @Dent42 18 วันที่ผ่านมา +8

    This seems like composition but with extra steps

    • @phoenixstyle
      @phoenixstyle 17 วันที่ผ่านมา +3

      Yep, mixins are imo a really bad practice, especially when compared to the likes of composition.

  • @ibrahimkoz1983
    @ibrahimkoz1983 18 วันที่ผ่านมา +7

    Mixins do not imply an "is-a" semantics while using inheritance. They are like interfaces with default implementations. You can enforce a specific interface to be implemented in the descendant class by extending this interface in the mixin itself.

  • @sebastianmoser9652
    @sebastianmoser9652 19 วันที่ผ่านมา +9

    Thank you, very clear and simple explanation. Mixins look very similar to what PHP calls Traits.

  • @anon_y_mousse
    @anon_y_mousse 19 วันที่ผ่านมา +12

    I suppose it's kind of like Rust's traits. Maybe you could compare and contrast different languages and how they use the concept.

    • @TheCodingGopher
      @TheCodingGopher  19 วันที่ผ่านมา +5

      Yes, these are very similar. Though traits in Rust achieve the shared behavior without inheritance. That's a solid idea for a future video

  • @dschledermann
    @dschledermann 11 วันที่ผ่านมา +1

    This use case is covered by the traits in Rust and PHP, and interfaces with default implementations in Java.

  • @nathaaaaaa
    @nathaaaaaa 19 วันที่ผ่านมา +18

    Mixins are the foundations of modern Minecraft modding, but we just see it as bytecode modification framework.

    • @thefanboy3285
      @thefanboy3285 18 วันที่ผ่านมา +1

      :o

    • @DreadKyller
      @DreadKyller 17 วันที่ผ่านมา

      While they have the same name, the Mixins of Forge or Fabric are a completely different thing that have no real relation. This is talking about a design pattern while the Forge or Fabric mixins are a form of code injection that act closer to a decorator pattern than a mixin pattern. But really Fabric/Forge mixins are not a programming pattern, they're a specific feature, mixins are effectively a form of instrumentation in Fabric/Forge, whereas the mixin pattern in Computer Science is a specific programming patterns like inheritance or composition. Again despite the names they are largely unrelated to each other.

    • @nathaaaaaa
      @nathaaaaaa 17 วันที่ผ่านมา

      @@DreadKyller deeply they are the same thing. Yes, Spongepowered Mixin are a tool to modify bytecode and this video is about a trait-like design pattern to add functionality to a class.
      But what else Sponge's Mixins are? A tool to add/modify functionality of existing classes without the usage of inheritance. This is sort of stated in their repository: "Mixin is a trait/mixin framework for Java using ASM [...]"

  • @patrickdecabooter411
    @patrickdecabooter411 19 วันที่ผ่านมา +4

    I always love your content, keep it up!

  • @DreadKyller
    @DreadKyller 18 วันที่ผ่านมา +3

    If you could go over a comparison of this versus composition, as they seem like they're attempting the same thing, shared reusable pieces of logic without the need for complex hierarchy chains. What benefits does this have over traditional composition patterns?

  • @phoenixstyle
    @phoenixstyle 17 วันที่ผ่านมา +1

    This video just seems like your trying to build an ECS (Entity Component System) or Rust Traits, Java Interfaces or trying to do composition. Basically what my point is, is that there are many well established ways to achieve the same goals as mixins. Yes, mixins can save those few extra lines, but that's really the only advantage I see: a bit more readability.
    Generally Mixins can have lots of problems (Im using Java Mixins as an example here):
    Mixins rely on naming schemes in projects. This means, when a class / module / method is renamed, the mixin breaks. Any structural changes in the project result in mixins breaking.
    Mixins are usually byte-code inserts, which means they bypass compiler checks and might introduce breaking code.
    Also I believe what you implemented is just composition: One component that does Logging, the other that does File Reading.

  • @kocokan
    @kocokan 16 วันที่ผ่านมา +1

    Reduce duplication by copy pasting the code at compile time

  • @debbie8062
    @debbie8062 19 วันที่ผ่านมา +6

    missed you Gopher! Sad to see no daily uploads but i hope that means ur enjoying the holidays!! i really enjoy this type of video, can’t wait to see what type of content you’ll make this coming year

  •  18 วันที่ผ่านมา

    I’ve been trying to wrap my head around this idea (without putting too much effort, to be fair), and from this explanation I understand the “@“ decorators are basically syntactic sugar for all the boilerplate that would be needed from using [composition + dependency injection + inversion of control]. Ok, that seems nice.

  • @kaylemaclou
    @kaylemaclou 17 วันที่ผ่านมา +1

    ... seems like a mixture of an Aspect (as in AOP) combined with DI (Dependcy Injection.)

  • @dhillaz
    @dhillaz 17 วันที่ผ่านมา +1

    Good video. I wish I had some more time to understand the final example, feels like it got cut off to meet the exact 3 minute mark.
    Perhaps immediately jumping into an example and narrating it would have saved more time at the start.

  • @vvhat
    @vvhat 19 วันที่ผ่านมา +4

    Its the cpp equivalent of interfaces

    • @TheCodingGopher
      @TheCodingGopher  19 วันที่ผ่านมา +3

      Similar, but not exactly. Mixins in Python are more like reusable code snippets added via inheritance, while C++ interfaces (i.e. pure virtual classes) define a contract that classes must implement. Mixins add behavior; interfaces enforce structure.

  • @arijitgogoi5653
    @arijitgogoi5653 19 วันที่ผ่านมา +2

    All right let's mixin

  • @splytrz
    @splytrz 19 วันที่ผ่านมา +7

    I'm still not convinced that mixin is distinct from inheritance. It's just a pattern that uses (multiple) inheritance.

    • @splytrz
      @splytrz 19 วันที่ผ่านมา

      To clarify, I don't have an issue with the video. Maybe with the title though.
      I'm coming from Dart, where mixins have special syntax, which has made me confused for a long time because I couldn't see how they're different, and the video title has made me hope I'll finally understand it. But nope!
      Especially seeing you use inheritance to implement a mixin convinced me more that the Dart team has added them as a hack to enable multiple inheritance.

    • @splytrz
      @splytrz 19 วันที่ผ่านมา +2

      I think the core of my misunderstanding was that I was thinking about a **different category**. There is no difference between mixins and multiple inheritance **as a language mechanism**, but, **as patterns** they are different.
      So, while everyone was talking about a design pattern, I was thinking about the language mechanism. (This is reasonable though, because it was prompted by the language design that presented them as a separate mechanism)

    • @TheCodingGopher
      @TheCodingGopher  17 วันที่ผ่านมา

      @@splytrz Great insights, @splytrz! There's a distinction here; mixins as a pattern versus mixins as a language mechanism.
      I agree that under the hood, mixins rely on inheritance (i.e. often multiple inheritance). As a mechanism, they aren’t distinct. What sets mixins apart is their purpose / use case: they’re not about defining an ‘is-a’ relationship like traditional inheritance, but instead about injecting modular + reusable behavior into a class.
      Dart’s special syntax for mixins can make it feel like something entirely new, but it’s more about enforcing a lightweight way to share functionality without adding the complexity of full multiple inheritance. The main takeaway is that mixins represent a design philosophy rather than a fundamentally separate mechanism.
      Thanks for watching + great insights :)

    • @pilgrimage0
      @pilgrimage0 8 วันที่ผ่านมา

      @@TheCodingGopher I added a comment expanding on this. The example you provide is not distinct from inheritance (neither as a pattern nor as a language mechanism), and I've explained why in my comment and provided an alternative example that is distinct from inheritance. It is distinct from single inheritance, sure, but it quite literally is textbook multiple inheritance, and this is also explained in the python docs. So to say it's different from inheritance is incorrect, but to say it's different from traditional, single inheritance is correct.

  • @davidrieger3160
    @davidrieger3160 18 วันที่ผ่านมา

    Any reason to use this over composition (e.g. make the logger class a field rather than inheriting from it)?

    • @TheCodingGopher
      @TheCodingGopher  17 วันที่ผ่านมา

      Use composition when you need flexibility (e.g., swapping loggers at runtime), better encapsulation, or to avoid multiple inheritance issues (i.e. for Java).
      And generally, prefer mixins for Great lightweight, reusable functionality that directly integrates into the class / if you want to combine multiple behaviors like logging, notifications, or validation.

  • @kocokan
    @kocokan 16 วันที่ผ่านมา +1

    Isn't this C macro?

    • @kaus05
      @kaus05 12 วันที่ผ่านมา +1

      More like libs

  • @guelakais1438
    @guelakais1438 17 วันที่ผ่านมา +1

    to be honest: I don`t see the difference to interfaces or traits

    • @AK-vx4dy
      @AK-vx4dy 17 วันที่ผ่านมา +1

      Depends on language, mixins are more like building finally class from lego parts or adding things to .prototype in JS. Interfaces/traits with implementation which you can't/won't change

    • @dschledermann
      @dschledermann 8 วันที่ผ่านมา

      @@guelakais1438 depends a bit on the language. There are subtle differences. Mixins are perhaps most similar to PHP traits, where you can add behavior to a class without actual inheritance. You can't use mixins or PHP traits as type arguments. Java interfaces and Rust traits can have both (limited) behavior and can be used as type arguments.

  • @lepch
    @lepch 19 วันที่ผ่านมา +1

    so mixins are just pytest fixtures

    • @TheCodingGopher
      @TheCodingGopher  19 วันที่ผ่านมา +3

      Not quite. Mixins are a way to add reusable behavior to classes through inheritance in OOP, while pytest fixtures are specifically designed for setting up / tearing down test preconditions in pytest. Fixtures are automatically injected into test functions (which is the core part of the pytest framework), whereas mixins are a more general programming construct used for sharing code across classes.
      They can overlap in functionality in some cases - but they're fundamentally different.

    • @lepch
      @lepch 17 วันที่ผ่านมา

      @@TheCodingGopher I might've been writing tests wrong)