You can use "by" in Kotlin e.g.: class CounterSet( private val innerSet: MutableSet = mutableSetOf() ) : MutableSet by innerSet { //only new and or overwrite func's }
@pokefreak21126 ай бұрын
The cleanest implementation of composition I've come across is the one in Odin and Jai. They let you put a 'using' keyword in front of any field, variable, or namespace to make its members directly accessible in the current scope. So not only can you use it in structs/classes, you can also use it in fuctions to essentially destructure the object or in namespaces to compose a larger API from smaller API subsets
@ProfessorRS6 ай бұрын
If I understand correctly, what you're describing is very similar to the 'with' keyword in kotlin
@pokefreak21126 ай бұрын
@@ProfessorRS Had to look it up, but looks like it yeah! Except apparently *with* is a function in kotlin and not a keyword. If you could do 'with var app = makeApp()' it would basically be equivalent for the function use case
@gonzo1916 ай бұрын
Not sure why we switched over to Go to demo composition, when the same could've been done in Java (OOP languages) but that's beside the point. These arguments oftentimes glorify composition as if it doesn't have any downsides when compared. Inheritance (Pros: Reusability, Polymorphism, Code organization | Cons: Tight coupling, Fragility, Inflexibility), Composition (Pros: Flexibility, Loose coupling, Code reuse | Cons: Complexity, Overhead (Memory), Code organization). Inheritance should be used when there is a direct class relationship whilst composition should be used when there is a need for more dynamic behavior via loose class coupling.
@awesome-coding6 ай бұрын
Hey! I'm not sure why composition would require more memory overhead? We are talking about the same data being stored in the memory, right? I switched to Go for two reasons: 1. To bring on the argument that some languages don't allow inheritance at all since the language designers felt like the cons outweigh the pros. 2. I really like the inner type promotion aspect of Go, and I believed it would be an interesting detail when it comes to different ways in which software could be written. But to your point, yes, you are right, composition can be achieved in any OOP languages as well.
@carlosmspk6 ай бұрын
@@awesome-coding you also added the disclaimer that this is a segment called "The Snippet", but I'd argue it doesn't work very well. If you're trying to explain a concept and change languages midway two things happen: 1-Any viewer not familiar with one of the languages will have trouble understanding that part 2-Even viewers that are familiar with both languages, will have some mental burden to see the conceptual differences of what you're trying to show, because the actual differences from the language itself (syntax, strucutre, etc.) will be extremely noticeable. I understand the idea of having multiple languages to kind make the concept come accross in a more "pure", language-agnostic form, but I don't think it works very well. Just my 2 cents.
@awesome-coding6 ай бұрын
@@carlosmspk Hey! I really appreciate your feedback, and I actually believe you are right on this one. The context switching between languages makes things harder to follow along. I'm still exploring and testing this "The Snippet" concept, so your feedback is very valuable. Thank you!
@kevinb15946 ай бұрын
@@awesome-coding As someone who didn't know the first language and is only vaguely familiar with GO, I wanted to tell you that you did just fine and it was understandable. I think the only sticking point is that the viewer needs to understand how GO doesn't use a keyword for explicitly stating the association of methods to structs - though you did state it if they don't 'get' it then the rest of the explanation could be lost.
@awesome-coding6 ай бұрын
@@kevinb1594 Thank you for your input!
@HermesSoilder4 ай бұрын
Great video! I’m teaching my self rust coming from typescript and this makes traits much more clear.
@awesome-coding4 ай бұрын
Glad to hear!
@julian_handpan6 ай бұрын
In OOP dependency injection + composition. 😌😘👌🏻
@carlosmspk6 ай бұрын
You're throwing away 25% of OOP by doing that. Not saying it's a bad thing, but can it still be called OOP?
@plaintext72886 ай бұрын
@@carlosmspkwhy not? A subset of c++ is still the same language :)
@carlosmspk6 ай бұрын
@@plaintext7288 C basic is not C++ I know over the years C++ changed to a point where it no longer is a superset of C, but even when it was, they were different languages. Either way, you're talking about languages. I was talking about the core principles of OOP, if you throw one of them out the window, I'd argue it's no longer OOP
@julian_handpan6 ай бұрын
@@carlosmspk that’s not true my friend. Because OOP means objects sending messages, so is all objects. Even if the object is a composed object or a dependency object, still object talking to objects.
@carlosmspk6 ай бұрын
@@julian_handpan hah, you're coming from Smalltalk. Alright, makes sense, then
@netssrmrz6 ай бұрын
Nice video but I didn't enjoy the cognitive load of switching between languages (Java vs Go) and data models (shapes vs cars). Also, for me, not detailing composition cons and inheritance pros is a red flag. Personally, knowing when to use each method effectively makes me a better dev than fanboys trying to shoehorn composition into everything.
@awesome-coding6 ай бұрын
Noted! Thank you for pointing out these issues. Clearly I'll have to do a better job next time.
@alinghinea986 ай бұрын
Great video 👏
@bonquaviusdingle57202 ай бұрын
“Composition over inheritance” is what a lot of beginners tend to say. Which to use depends entirely on the use case and design pattern.
@dei8bit5 ай бұрын
composition makes you happy 🙂 the inheritance, only when your parents were millionaires and considerate
@awesome-coding5 ай бұрын
I really need some millionaire parents 😅
@dei8bit5 ай бұрын
@@awesome-coding well, composing them will probably be possible in the future(?
@awesome-coding5 ай бұрын
@@dei8bit haha!
@bear458ziif-s6 ай бұрын
never used java before, but for some reason i always assumed that composition wasn't even a thing in java. obviously it's just a pattern but i've never seen it before in java and it has a reputation of being _the_ oop language. i associate inheritance with oop more than i do with composition.
@awesome-coding6 ай бұрын
Yep exactly! This is the miss conception in OOP, because the OOP principles are usually enforced.
@maxamed144 ай бұрын
No wonder Go is trending so hard! Amazing video as always mate!
@awesome-coding4 ай бұрын
Glad you liked it!
@krtirtho6 ай бұрын
Ok, got it. Now spit out the cons of Cumposition. The memory overhead, complexity etc...
@awesome-coding6 ай бұрын
Hmm I'm not sure why people are mentioning memory overhead in this scenario. Are talking about the references to the type embeddings? I'm pretty sure that's highly optimised. When it comes to complexity, the same argument can be made about Inheritace. As I said, Inheritance is not bad, it just has very specific use-cases, and is not a good idea to always use it when code reusability is required.
@fakedevdutt6 ай бұрын
damn the languages are hard
@awesome-coding6 ай бұрын
Which one?
@VigneshwarDev6 ай бұрын
@@awesome-coding which one is not ?😅 just kidding bro
@awesome-coding6 ай бұрын
@@VigneshwarDev 😅
@theklr6 ай бұрын
🔥🔥🔥
@TechBuddy_6 ай бұрын
let's goooooo
@awesome-coding6 ай бұрын
✌️✌️✌️
@atlantic_love3 ай бұрын
Maybe learn a little more about inheritance before you start blabbering nonsense. The car, minivan and truck are subclasses of Vehicle. It's not the first two being a subclass of the last, or the first one being a subclass of the second.
@antibioticaddict6 ай бұрын
Or just use interfaces
@awesome-coding6 ай бұрын
Right... but languages don't allow state in interfaces most of the time so how would you reuse state?