Self-Types in Scala - the Why and the How | Rock the JVM

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

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

  • @jeanfdmelo
    @jeanfdmelo 4 ปีที่แล้ว +8

    Keep videos coming, Daniel! That's gold!

    • @rockthejvm
      @rockthejvm  4 ปีที่แล้ว

      Thanks! Share with whoever you think might find it useful!

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

    This is a great example of one of the fundamental weaknesses of the OOP paradigm, and something that is bound to get exponentially worse as the domain logic gets larger and more complex. IMO, neither option really solves the problem in a satisfactory method -- admittedly, self-types are an elegant approach, albeit still just a Band-Aid to the larger problem.

  • @megadethjohn
    @megadethjohn 4 ปีที่แล้ว +5

    Great video! One of the more concise and informative explanations of self types out there.

    • @rockthejvm
      @rockthejvm  4 ปีที่แล้ว

      Glad you liked it!

  • @XINClover
    @XINClover 3 ปีที่แล้ว +1

    Very clearly explained. I learn new things every day. Thank you Daniel!

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

    Really well explained about self types and difference from inheritance using trait extensions

  • @sergeibatiuk3468
    @sergeibatiuk3468 ปีที่แล้ว

    Nice! But can you use this pattern (the Cake pattern?) to compose classes or is it for traits only?

  • @mompyish
    @mompyish 3 ปีที่แล้ว +1

    This is awesome!!.. I have been struggling to find out the meaning of this syntax in one of libraries..Blogs and article did't help.Thanks Daniel

    • @rockthejvm
      @rockthejvm  3 ปีที่แล้ว

      Glad it was helpful!

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

    Daniel, you rock man! This explains things so clearly!

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

    Amazing video, thx!

  • @tai-hao-le
    @tai-hao-le 3 ปีที่แล้ว +1

    great video but I am still a bit confused about the difference between self-types and inheritance. do you have any additional resources to research this subject? thanks!

    • @rockthejvm
      @rockthejvm  3 ปีที่แล้ว

      How are you confused? Maybe I can help here.

  • @rheafernandeshallu
    @rheafernandeshallu 4 ปีที่แล้ว +1

    Thank you again :) It's really helping me get better at writing code.

  • @harishgontu9285
    @harishgontu9285 4 ปีที่แล้ว +1

    Nice !! You should join Leo Benkel scala server. It is a place where they try to encourage budding scala devs

  • @egorkovesh5033
    @egorkovesh5033 3 ปีที่แล้ว +1

    13:11 people constantly ask me "WHAT THE F...undamental diffrence..."

  • @romanburenko7593
    @romanburenko7593 3 ปีที่แล้ว

    Great explanation! Thanks a lot!

    • @rockthejvm
      @rockthejvm  3 ปีที่แล้ว

      Glad you liked it!

  • @StefanReich
    @StefanReich 3 ปีที่แล้ว +3

    Couldn't you simply put a field of type Person into the Diet trait?

    • @rockthejvm
      @rockthejvm  3 ปีที่แล้ว +1

      As always, there are multiple solutions to the same problem.

    • @sergeibatiuk3468
      @sergeibatiuk3468 ปีที่แล้ว

      @@rockthejvm But what are the pros and cons of both?

  • @avinashc6114
    @avinashc6114 3 ปีที่แล้ว +1

    It's clear thank you 🔥

  • @Jankoekepannekoek
    @Jankoekepannekoek 4 ปีที่แล้ว +1

    I use this mechanism often to access a reference to an outer class when I'm creating inner classes, since in Scala you can't say 'MyOuterClass.this' like you can in Java.

  • @DevInsideYou
    @DevInsideYou 4 ปีที่แล้ว

    Why not just add person to eat?
    def eat(thing: Edible, person: Person): Boolean

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

      This would be very similar to the extension option #1, because we bleed API dependencies from one type to the other.

  • @moy2010
    @moy2010 3 ปีที่แล้ว

    What about an associated type? It seems to me that it achieves exactly the same as the self type, dunno why they needed to add yet another way to express the same.

    • @rockthejvm
      @rockthejvm  3 ปีที่แล้ว

      What do you mean by associated type?

    • @moy2010
      @moy2010 3 ปีที่แล้ว

      @@rockthejvm I found that they are called "Abstract types" in Scala, and seems that this discussion has already been done in StackOverflow: stackoverflow.com/questions/1154571/scala-abstract-types-vs-generics

    • @rockthejvm
      @rockthejvm  3 ปีที่แล้ว +1

      @@moy2010 you mean abstract type members. These are different than self-types and they serve a different purpose.

  • @elgireth
    @elgireth 4 ปีที่แล้ว

    Key difference is that self type can be generic, which is used in F-Bounded polymorphism.

    • @rockthejvm
      @rockthejvm  4 ปีที่แล้ว +1

      F-bounded polymorphism is something else. It can be used in conjunction with self-types, but that's not the defining difference between self-types and inheritance.