Kotlin In Out types in Generics in 20 minutes

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

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

  • @InterviewDOT
    @InterviewDOT 2 ปีที่แล้ว +1

    Very nice explanation :)

  • @karandeepsingh1898
    @karandeepsingh1898 2 ปีที่แล้ว

    AWESOME EXPERIENCE

  • @juanfresco3874
    @juanfresco3874 2 ปีที่แล้ว

    thank you , as you said , it is a bit confusing , i would like that you made another video on the same subject because generics in and out are really difficult to understand to me, hope you will be able to...

    • @superlazycoder1984
      @superlazycoder1984  2 ปีที่แล้ว +1

      Hey Juan, thank u so much for your kind words. Yes will try to make more example videos in future

    • @mehmoodrehman6336
      @mehmoodrehman6336 2 ปีที่แล้ว +2

      Jalde buna main be purhaon ga!

  • @deepakborade4123
    @deepakborade4123 2 ปีที่แล้ว

    Woow this is a good article

  • @ponchoolveramendoza8115
    @ponchoolveramendoza8115 2 ปีที่แล้ว

    Great Explanation!

    • @superlazycoder1984
      @superlazycoder1984  2 ปีที่แล้ว

      Thank u so much Poncho

    • @mohammedharoon1167
      @mohammedharoon1167 2 ปีที่แล้ว

      mam can u please make a playlist of
      google kotlin course which in documentation

    • @superlazycoder1984
      @superlazycoder1984  2 ปีที่แล้ว

      @@mohammedharoon1167 Google Kotlin fundamentals playlist th-cam.com/video/IfW0b1a1TrA/w-d-xo.html

  • @asadullahilyas
    @asadullahilyas 2 ปีที่แล้ว

    Wonderful explanation
    But I don’t see any use case where these are going to get used. Can you also provide a simple use case? You can even provide it in reply to this comment if you think it’s feasible.
    Furthermore, in your consumer example, in main function, you created object of ConsumerPhone(). I don’t understand what purpose this served at the end. Can you please elaborate on that too?
    Thank you very much, you’ve got yourself a subscriber
    The exact 700th subscriber 😄

    • @superlazycoder1984
      @superlazycoder1984  2 ปีที่แล้ว +2

      Thank you so much Asad for subscribing. In out types are basically useful when we want to restrict the usage of our functions. For example like in Java we have generic functions say
      interface Source {
      static final T next();
      }
      Now in this case the next function which is called is a producer function. But it can only return object of subclass of Object. In this case we can store something like
      Object o = Source.next()
      Now this is ok if we are only passing objects of same data type everytime and reading them but not writing in them.
      However if we create a write function also
      static final write(T obj)
      Now in this case write function can take any argument which is extending Object class. Now this can cause issues because lets say the producer function returned us type String and we called write function and try to write in object something other than String then it will give us error.
      To prevent this Kotlin introduced in out types so that compiler knows which class can have producer functions that return only certain type of values and which Consumer classes can have write functions on which write methods can only be called.
      For more understanding this link is also helpful
      kotlinlang.org/docs/generics.html#type-projections
      Hope it helps