Covariance and Contravariance

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 พ.ย. 2024

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

  • @markojovanovic8444
    @markojovanovic8444 7 หลายเดือนก่อน +36

    It's rare to find a YT channel that explains such content, especially in such an approachable way. Thanks a bunch! 🙏

  • @ChristopherOkhravi
    @ChristopherOkhravi  8 หลายเดือนก่อน +56

    Reupload because of a mistake in the previous upload. My apologies 🙏

    • @dev22221
      @dev22221 8 หลายเดือนก่อน +3

      i felt like deja vu :)

    • @NicolasChanCSY
      @NicolasChanCSY 8 หลายเดือนก่อน +4

      May I know what mistake was in the previous upload? I have watched this re-upload and it seems the same as the previous one.

    • @barisaxo
      @barisaxo 8 หลายเดือนก่อน +2

      @@NicolasChanCSY I would also like to know specific spots that are different

    • @ChristopherOkhravi
      @ChristopherOkhravi  8 หลายเดือนก่อน +16

      Notice the crazier than usual cuts at 08:02. In the old video I said “returns” instead of “takes” but it was at a very very unfortunate moment which made the explanation very confusing 😊 I figured it was better to accept the mistake and just reupload so that I do not create unnecessary confusion 😊

  • @leonpierre6810
    @leonpierre6810 8 หลายเดือนก่อน +23

    This was an amazing explanation of this. Especially the final part when discussing input and output portion. Great job

    • @ChristopherOkhravi
      @ChristopherOkhravi  8 หลายเดือนก่อน

      Thank you. I'm happy to hear that you appreciate it.

  • @VishalGahlot
    @VishalGahlot 4 วันที่ผ่านมา

    Crazy explanation, bro! No one will forget this concept now.

  • @uuuummm9
    @uuuummm9 8 หลายเดือนก่อน +3

    I guess it is much simpler to think first about functions and their input and output parameters. Everything is very intuitive there.
    When you move to classes and objects you can consider them as set of functions with their parameters and that is why it becomes important to define the direction of the Type parameter just same way as you specify it in the functions.

    • @PedroBarbosaRoman
      @PedroBarbosaRoman 2 หลายเดือนก่อน

      Wow, never heard someone explain that in this way, relating the need of using the out and in directions in classes with functions. Thank you so much!

  • @faduregis
    @faduregis 8 หลายเดือนก่อน +3

    This concept took me two day to understand when I was learning C#. Thanks for bringing it up, great job.

  • @lutopastialis8673
    @lutopastialis8673 2 หลายเดือนก่อน

    Best explanation of in-, co- and contravariance ever. Finally, I get the concept after using it for so many years without fully understandig it.

  • @Eureka60
    @Eureka60 5 หลายเดือนก่อน +1

    Literally the only TH-cam channel I use for coding stuff, since college to when I need to brush up on stuff at my job!

  • @omack47
    @omack47 8 หลายเดือนก่อน +3

    Christopher I am eternally grateful for the great content you have created, thanks to your videos of design patterns and thoughts of these topics I have grown a lot, hope to see you some time in Seattle!
    Saludos 🫡 master! 🙇‍♂️

  • @jussiheino
    @jussiheino 6 หลายเดือนก่อน +2

    Here's my invariant: sometimes I'd like to give a bigger thumbs-up than others. This is a good lesson! Worth re-uploading the corrected version, good stuff, can recommend.

  • @nirmalhasposted
    @nirmalhasposted 8 หลายเดือนก่อน +1

    He is the Hero.. Always explain the concepts with passion and energetic which drives us to understand the concept clearly. Thank you 🎉

    • @ChristopherOkhravi
      @ChristopherOkhravi  8 หลายเดือนก่อน

      Thank you. And thank you for watching 😊🙏

  • @StuartLoria
    @StuartLoria 8 หลายเดือนก่อน

    Great video Mostachón, I liked how you explained using “general” and “specific” rather than using the terms “wide” and “narrow” as some British devs I follow did

  • @francescotaurisano8965
    @francescotaurisano8965 7 หลายเดือนก่อน +1

    I can say that you are one of the best that explain this abstract concept. Thanks for all the passion, you really inspired me and helped in my work. Keep going!!!

  • @tonnie7079
    @tonnie7079 7 หลายเดือนก่อน

    I am a Kotlin Student and this made more sense to me than trying understanding it from ChatGPT. Thanks lots mate for the intuitive live example to sink it in!!

  • @CristopherVergaraColombo1
    @CristopherVergaraColombo1 5 หลายเดือนก่อน +1

    Este canal tiene puro oro 🪙

  • @bb-ln9ge
    @bb-ln9ge 8 หลายเดือนก่อน

    The best explanation I have even seen including MSDN why List can not be assigned to IList ! Thank you!

  • @tomlee7073
    @tomlee7073 8 หลายเดือนก่อน

    After all the years of watching you, I have to say I am very impressed with your determination to teach Especially when combated with the youtube comments of hatred that everyone deals with!!! Keep up the good work :) You helped me and others I recommended your channel to in my early days so thank you very much

    • @ChristopherOkhravi
      @ChristopherOkhravi  8 หลายเดือนก่อน

      Thank you. I’m very happy to hear that 😊🙏

  • @moawyahabdulrahman8782
    @moawyahabdulrahman8782 7 หลายเดือนก่อน

    My friends and I watch your videos together whenever you upload a video as such and discuss the ideas.
    We've never fully understood how generics behave before this video (whether it be in java or in C#).
    You're great man keep up the new style videos and we're waiting for the LSP sequel video.

    • @ChristopherOkhravi
      @ChristopherOkhravi  7 หลายเดือนก่อน

      Wow. That’s awesome. Makes me very happy to hear that 😊

  • @arteqppp6223
    @arteqppp6223 7 หลายเดือนก่อน

    Thanks for this video, its really confusing for not native english speaker, but its even more confusing when you try to read about this topic somewhere online! haha. Looking forward for next great videos!

  • @ryanbragabrito
    @ryanbragabrito หลายเดือนก่อน

    Simply Amazing Explanation!

  • @tatokutalia
    @tatokutalia 7 หลายเดือนก่อน

    Absolutely amazing explanation. Your content is always on top! Always easy to recomment to anyone. Thanks for the effort🎉❤

  • @logannday
    @logannday 5 หลายเดือนก่อน

    Amazing presentation, so clear

  • @andersmalmgren6528
    @andersmalmgren6528 5 หลายเดือนก่อน

    I really hate that Microsoft made Task instead of ITask. With the latter a a method declared with return type ITask would be able to return a ITask without roundtripping over an extra await.

  • @nickbarton3191
    @nickbarton3191 7 หลายเดือนก่อน

    If I take something out of a bag of apples, I expect an apple, not an orange. If it was a bag of fruit, I might get a cherry. You should explain genetic in and out.
    I love the chopped edited videos.

  • @friedwm
    @friedwm 3 วันที่ผ่านมา

    great explanation! Thanks bro.

  • @DeepWorksStudios
    @DeepWorksStudios 8 หลายเดือนก่อน

    Thanks for sharing this valuable lesson 🎉

  • @TOAOGG
    @TOAOGG หลายเดือนก่อน

    Hmm I never got to know these principles or forgot them again right after, but I am happy that C++ just prevents me from doing any of this :D I cannot override methods with different signatures and I cannot assign a vector of apples to a vector of fruits :)

  • @tekforge
    @tekforge 7 หลายเดือนก่อน

    Wow, I loved your examples at the beginning!

  • @TheCmarques23
    @TheCmarques23 8 หลายเดือนก่อน

    Very well explanation. It was amazing, thank you!!

  • @andriybalitskyy4029
    @andriybalitskyy4029 8 หลายเดือนก่อน +2

    Thank you for video! Could you share some books about OOP, design patterns, SOLID, etc, that helped you to understand these themes.

    • @ChristopherOkhravi
      @ChristopherOkhravi  8 หลายเดือนก่อน +3

      Thank you for the question. In my design pattern series I recommend the following two books which are absolute classics:
      * Design Patterns: Elements of Reusable object oriented software (geni.us/PsXmo)
      * Head First: Design Patterns (geni.us/nlbA6)
      I've also got a few videos planned that will make more book recommendations with a bit more context 😊
      The design patterns playlist:
      th-cam.com/video/v9ejT8FO-7I/w-d-xo.html

    • @andriybalitskyy4029
      @andriybalitskyy4029 8 หลายเดือนก่อน

      Thanks@@ChristopherOkhravi

    • @emilvideo
      @emilvideo 8 หลายเดือนก่อน

      @@ChristopherOkhraviThe geni links are not valid due to ) in the formatting

  • @shawnpaiva2347
    @shawnpaiva2347 7 หลายเดือนก่อน

    Amazing work! Great examples and explanation!

  • @patrickstephen7885
    @patrickstephen7885 6 หลายเดือนก่อน

    Your content is amazing. Great stuff

  • @ritwikgupta7540
    @ritwikgupta7540 2 หลายเดือนก่อน

    Hey Chris..thanks for the explanation, I have a question though. How have you defined a "IS A" inheritance relationship between FruitJuicer and OrangeJuice? I dont think that a FruitJuicer "IS A" OrangeJuicer, although a FruitJuicer can juice any fruit(including orange), but how can we say that the FruitJuicer is a sub type of the OrangeJuicer?

  • @batek34
    @batek34 2 หลายเดือนก่อน

    Amazing content.

  • @useful_short_videos
    @useful_short_videos 20 วันที่ผ่านมา

    Thank you so much for making this video. Would you be so kind as to explain why FruitJuicer is a subtype of OrangeJuicer and not the other way around? I mean while the UML diagram follows the statement "FruitJuicer is a subtype of OrangeJuicer" isn't FruitJuicer a superclass of OrangeJuicer in real world?

  • @tooru
    @tooru 4 หลายเดือนก่อน

    great explanation!

  • @devvorpian7255
    @devvorpian7255 6 หลายเดือนก่อน +1

    Thanks for explanation but I have a question, if FruitJuicer overrides the method in OrangeJuicer, the interface of method must be same? But the method in fruitjuicer is taking Fruit as input but the parent method is taking Orange type as input?

    • @rinner2801
      @rinner2801 6 หลายเดือนก่อน

      I thought the whole contravariance was that the method signatures don't have to be the same, based on the type hierarchy?

  • @bfdhtfyjhjj
    @bfdhtfyjhjj 7 หลายเดือนก่อน

    Are there any plans to start a series of videos about Domain Driven Design?

  • @ankboss
    @ankboss 7 หลายเดือนก่อน

    Thank you so much for this. This is so great. Would love to see you do some type system specific videos (which caters to Functional programming) .. Once again, thanks for the great content

    • @ChristopherOkhravi
      @ChristopherOkhravi  7 หลายเดือนก่อน

      Thank you very much for the feedback. 😊🙏 Are you the thinking along the lines of my series on Ramda JS or more like my video on type signatures?

    • @ankboss
      @ankboss 7 หลายเดือนก่อน

      I never saw your type signatures video before. Just watching it now, and yes that's exactly the kind of videos , I would love to see. Although, this might be a niche set of topic.
      I am also watching the following videos
      th-cam.com/play/PLA_-EWSPTJcu4i7RFCl_KeGrrz37C4_Oc.html&si=d02STCKZbOYPCGxM
      and they are good. But I was wishing you had a playlist of functional programming topics .. something like a crash course of the topics..
      ( with some book as a reference, the way you have done for your design pattern videos )
      Once again, thanks for covering interesting topics
      Good to see you uploading new content again.

  • @我的小人书
    @我的小人书 8 หลายเดือนก่อน +5

    how come FruitJuicer is a subtype of OrangeJuicer?? 4:37

    • @ChristopherOkhravi
      @ChristopherOkhravi  8 หลายเดือนก่อน +7

      Think of it in terms of substitutability. If a method expects an OrangeJuicer but all you have is a FruitJuicer then you should still be able to pass it since it will always be able to juice oranges.
      A concrete example is the IComparer interface in C# which is contravariant. If you create a comparer for Fruit you can use that comparer to sort lists of Orange. Even though a list of orange wants an IComparer but all we have is an IComparer.
      Makes more sense?
      Thank you for the question.

    • @gregbell2117
      @gregbell2117 8 หลายเดือนก่อน +6

      @@ChristopherOkhraviI understand your explanation but am still confused - aren't subtypes usually more specific?

    • @我的小人书
      @我的小人书 8 หลายเดือนก่อน

      @@ChristopherOkhravi thank you bud you are the best !

    • @ivankudinov4153
      @ivankudinov4153 7 หลายเดือนก่อน

      I was confused with this as well. I hope the following would help. Keep in mind the idea of substitutability from above.
      From wiki: "A subtype is a datatype that is related to another datatype (the supertype) by some notion of substitutability". It's not always what is 'larger', the context matters.
      - in the example of covariance we talked about lists, or arrays (consider this as source or output!), it's for them Cat is a subtype of Animal, one can substitute another. An array of cats is a substitution for an array of animals, when speaking in terms of output;
      - in the example of contravariance we talked about actions, or function parameters (consider this as sink or input!), it's for them FruitJuicer is a subtype of OrangeJuicer, one can substitute another. An action of squeezing out the fruit is a substitution for an action of squeezing out the orange, when speaking in terms of input.
      P.S. I highly recommend the article on covariance and contravariance in CS on Wikipedia @@gregbell2117

    • @ohm62
      @ohm62 7 หลายเดือนก่อน +1

      Yes. I am struggling with that inversion too.

  • @ginko3353
    @ginko3353 8 หลายเดือนก่อน

    Very enlightening video, I think I've a better understanding now about Producer Extends Consumer Super (PECS) rule applied in Java 😀

    • @ohm62
      @ohm62 7 หลายเดือนก่อน

      Sorry, you lost me at “Fruit Juicer” is a subtype of “Orange Juicer” ! Nope…. Not in my world 😮

  • @liva236muzika
    @liva236muzika 5 หลายเดือนก่อน

    Back to the drawing board... Gotta figure this out with pen and paper.

  • @ghostradiodelete
    @ghostradiodelete 8 หลายเดือนก่อน

    I honestly never knew this was a thing, I guess I don't think about it, but I also don't think I've ever seen that kind of error before so maybe I just planned things well enough to avoid it. I kinda want to go see if I can break my program doing this now, just because I've never encountered it. I don't know if this is a good practice, but if I have a list of types and I don't know or expect a certain type but can't guarantee it, and I absolutely cannot avoid a situation where I don't know what I'm getting, like a list of Animals that could be Cats, then I'll embed that object with an enum like AnimalType. Then I create the object with the type during construction, and check it to be sure whatever I have is, as your example, a Cat. Anyway, I'd prefer not to have to think so much about it and just be sure I'm not going to do something to a cat that only dogs or ducks are expected to do.

  • @sizur
    @sizur 5 หลายเดือนก่อน

    Bivariance makes sense in a few cases, bith theoretically and also practically.

    • @ChristopherOkhravi
      @ChristopherOkhravi  5 หลายเดือนก่อน

      I have yet to wrap my head around bivariance completely. If you have good examples of where it makes sense I would really appreciate it if you would like to share them 😊🙏 😊 Thank you for watching 😊

  • @MarianoAquino
    @MarianoAquino 7 หลายเดือนก่อน

    great video! I'm wondering, is 'upcasting' and 'downcasting' the way to try to make arrays temporarily covariant/contravariant in runtime (risking a runtime exception)?

    • @ChristopherOkhravi
      @ChristopherOkhravi  7 หลายเดือนก่อน

      Close. Variance regards what is considered a subtype or something else. And if A is a subtype of B then objects of type A can safely be upcast to type B. Covariance and Contravariance regards which type can be considered a subtype of which. So it’s always about upcasting. Thanks for watching 😊

  • @liquidpebbles
    @liquidpebbles 8 หลายเดือนก่อน

    I didn't even know there were words for these concepts.

  • @nasimnajand9697
    @nasimnajand9697 5 หลายเดือนก่อน

    you are just amazing 😇

  • @GiovanniOrlandoi7
    @GiovanniOrlandoi7 8 หลายเดือนก่อน

    Great video!

  • @Briogus
    @Briogus 2 หลายเดือนก่อน

    Amazing

  • @felipe2637
    @felipe2637 7 หลายเดือนก่อน

    You're amazing, keep up the good work!

  • @MenukaWeerasooriya
    @MenukaWeerasooriya 8 หลายเดือนก่อน

    Great one!!!

  • @gummansgubbe6225
    @gummansgubbe6225 7 หลายเดือนก่อน

    Nice.

  • @align2source
    @align2source 6 หลายเดือนก่อน

    💚

  • @numeritos1799
    @numeritos1799 8 หลายเดือนก่อน

    If I understood the video correctly, Java lists could be covariant (since generic types are erased at runtime) but are still are inviariant. Do you know if there's any other reason for this?

    • @numeritos1799
      @numeritos1799 8 หลายเดือนก่อน

      Btw, for my programmer brain, what clicked was the explanation from 8:20 to 10:55.
      Just as feedback if you ever want to flip your explanations to be practical-then-theorical rather than theorical-then-practical. Still awesome stuff :)

    • @ChristopherOkhravi
      @ChristopherOkhravi  8 หลายเดือนก่อน

      Very valuable feedback. Thank you very much 🙏😊

  • @FitsumWondessen
    @FitsumWondessen 7 หลายเดือนก่อน

    Can you do more functional programming

  • @nullset11
    @nullset11 6 หลายเดือนก่อน

    How many attempts at the orange juggle before camera perfect? 😄

    • @ChristopherOkhravi
      @ChristopherOkhravi  6 หลายเดือนก่อน

      Don’t underestimate my juggling skills 😁😁😁

  • @krozaine
    @krozaine 8 หลายเดือนก่อน

    I have a problem statement, I will try to frame it & seems related to the video:
    My program has 2 things to do -
    i) Fetch some fruit responses from various places and
    ii) Transform each fruit responses differently into views
    Example: I bring AppleResponse and BananaResponse (extends FruitResponse) data by making different API calls (irrelevant detail)
    I now want to transform AppleResponse and BananaResponse into
    AppleView1, AppleView2 from AppleResponse & BananaView1 (extends FruitView) from BananaResponse
    I have an intermediate ResponseStore Map on FruitType { APPLE -> AppleResponse, BANANA -> BananaResponse }
    ^This is the output of Step-1 : Map

  • @devinlauderdale9635
    @devinlauderdale9635 3 วันที่ผ่านมา

    so contravariance === generics?

  • @prasanthomanakuttan8391
    @prasanthomanakuttan8391 7 หลายเดือนก่อน

    Earned a sub

  • @khatdubell
    @khatdubell 7 หลายเดือนก่อน

    The ending 😂

  • @akjoker79
    @akjoker79 7 หลายเดือนก่อน

    I know this is undoubtedly the best explanation but still am confused 😂

  • @yonishachar1887
    @yonishachar1887 8 หลายเดือนก่อน

    Why did you reupload this?

    • @ChristopherOkhravi
      @ChristopherOkhravi  8 หลายเดือนก่อน +3

      There was a mistake in the previous upload that makes it too confusing. My apologies! 🙏

    • @theOptimisticCoder
      @theOptimisticCoder 8 หลายเดือนก่อน

      ​@@ChristopherOkhravi
      Seems the same video to me. I watched both of them 😅

  • @othmanothman4530
    @othmanothman4530 8 หลายเดือนก่อน

    Ye that is too confusing
    Me *doing leonardo de caprio pointing at tv* : pssst psssst pssst yup that is me

  • @iliadmitriev01
    @iliadmitriev01 8 หลายเดือนก่อน

    5:35

  • @ianokay
    @ianokay 7 หลายเดือนก่อน

    Just because you're fed a sequence of Cat to start your declared list of Animals, I don't see why that doesn't allow the more generic list of Animal to accept further things than the Cats it was initialized with 🤨🤔🤔 We aren't trying to put dogs in the Cat list, we're trying to (clearly, as defined) craft an Animal list that we started from a Cat list 🤔 You say "it doesn't support all Animals", while talking about a clearly defined and declared List, so of course it does. Only the List we started our Animal sequence from, does not. 🤨 This is my confusion despite/during your explanation. Thanks!

  • @user-zp1dv4yh5e
    @user-zp1dv4yh5e 7 หลายเดือนก่อน

    I am not a native english speaker, can you replace some words with simpler variants

    • @ChristopherOkhravi
      @ChristopherOkhravi  7 หลายเดือนก่อน

      Thank you for this very important comment! It would be great if you could give examples of words that I am using that are complicated. Either way I will do my best to speak simpler in the future. Thank you again 🙏😊

    • @jay31415
      @jay31415 29 วันที่ผ่านมา

      ​@@ChristopherOkhraviYour video was perfect. I wonder if this comment was a joke 😂

    • @ChristopherOkhravi
      @ChristopherOkhravi  29 วันที่ผ่านมา +1

      @@jay31415oh 😬😊🤦‍♂️

  • @wjrasmussen666
    @wjrasmussen666 6 หลายเดือนก่อน

    Bag of fruit has apples, bananas, grapes, oranges.....

  • @javajavelin1
    @javajavelin1 3 หลายเดือนก่อน

    You just have to practice some coding to be good in the language. Then come on this channel to ace in every project or work place.

  • @BangsarRia
    @BangsarRia 6 หลายเดือนก่อน

    Didn't expect an analysis of politics here. "Be conservative in what you do." "Be liberal in what you accept from others." Are you telling us that Conservatives tend to be more productive while Liberals tend to be more tolerant of others? Or am I mixing Apples and Oranges?

    • @ChristopherOkhravi
      @ChristopherOkhravi  6 หลายเดือนก่อน

      Haha 😊 Hadn’t thought of it that way. This is a popular saying (stemming from the TCP spec if I’m not mistaken) so I should not take credit for it. Either way we are here using the terms in their literal sense. So we mean conservative as in that we should not return anything wild (meaning that we in the case of TCP should stick to the spec). And we mean liberal as in that we should be open to alternative interpretations (meaning that we in the case of TCP should allow deviations from the spec).
      Thank you for the comment and for the interesting interpretation 😊. I don’t want to get dragged into politics but my hunch would tell me that I agree with you 😊😊

    • @BangsarRia
      @BangsarRia 6 หลายเดือนก่อน

      @@ChristopherOkhravi You're agreeing with yourself which shouldn't be too surprising.
      Taking this into Game Theory, Contravariance says that it's a Zero-Sum Game ("I win - you lose") or at least "You have to do it my way", while Covariance says it's Win-Win; we can have our Orange and eat it and you can have any Fruit you want.