Scala 3: Opaque Types | Rock the JVM

Поділитися
Вставка
  • Опубліковано 7 січ 2025

КОМЕНТАРІ • 27

  • @燕贻陈
    @燕贻陈 Рік тому +1

    Opaque type is quite good to describe the protocol definitions which contains vast data format🥳

  • @MrCesarification
    @MrCesarification Рік тому

    I love these videos soooo much... all rise for the almighty Daniel!

  • @smurfandturfer
    @smurfandturfer 4 роки тому +9

    Hi, thank you for making this video. I notice you mention several times that you lose the API of the underlying type, it is possible to define `opaque type Name

    • @rockthejvm
      @rockthejvm  4 роки тому +3

      Totally possible, never thought of that. Nice one!

    • @JimBalter
      @JimBalter 4 роки тому +3

      You rarely want to inherit the entire API of the underlying type without restriction. That defeats the purpose of declaring the type opaque.

    • @jonaskoelker
      @jonaskoelker 4 роки тому +1

      A more rudimentary thing you can do, if the underlying type is immutable (or you don't mind sharing mutable parts), is define e.g. `def asString(n: Name): String = n`. That way, if I'm outside the defining scope and I have an `n: Name` in hand, I can do `n.asString.length` or whatever other ways I want to interrogate the underlying string. If you make a `fromString` you can have fairly smooth two way traffic.
      Of course, if `opaque type A

    • @ceyceycey21
      @ceyceycey21 4 роки тому

      @@JimBalter you are right, it defeats the purpose of encapsulation via opaque types. I checked if we could utilize the new export clauses within an extension block to selectively expose some methods, but it doesn't work unfortunately. In my opinion, we should have been able to say `export n.length` but compiler says def expected.

  • @BloodnutXcom
    @BloodnutXcom 3 роки тому

    This is cool.
    In Scala 2 we extend Name to AnyVal so it gets some of the functionality on basic types and and we don't allocate the wrapper on heap.

    • @rockthejvm
      @rockthejvm  3 роки тому +1

      The equivalent technique in Scala 2 is newtypes: github.com/estatico/scala-newtype

  • @aethermass
    @aethermass 3 роки тому +1

    "I can't even spell my name..." lol fun times. I just watched the value types video released today. I am going to use these everywhere.

  • @menphalla
    @menphalla 4 роки тому +1

    Good explanation with examples. Thanks.

  • @TheProximator
    @TheProximator 4 роки тому +1

    Nice and concise, thanks a lot :) I wish if you could add some concrete scenario where it would be better to opaque feature, just to notice the advantage of it over something else

    • @rockthejvm
      @rockthejvm  4 роки тому

      Mostly the combination of API design + no-overhead benefits.

  • @kevalan1042
    @kevalan1042 4 роки тому +2

    Nice explanation! I like the fact that it's zero overhead. What do you mean by "it has its drawbacks, and notably the inability to use the API of the underlying type" -- isn't that the whole point? Or do you mean that there should be a more convenient shorthand for enabling a subset of the underlying type's API?

    • @rockthejvm
      @rockthejvm  4 роки тому

      If you shadow a type and want to use some of its original API, you'd have to re-offer that API. This might be tedious, depending on what your use-case is.

  • @Lumintorious
    @Lumintorious 4 роки тому +1

    Hey man, I love the explanations you are doing on Scala 3, but what would be a possible scenario in which an opaque type would be better than a value class wrapping a type? Value classes don't have overhead and additional data either and you can structure methods in them a little more clearly. You also have the option to hide the value's API or show it through `export` keywords.

    • @rockthejvm
      @rockthejvm  4 роки тому +1

      Nice suggestion - I'll document the difference in a future video/post.

  • @HoD999x
    @HoD999x 2 роки тому

    at 7:50 you say "quote unquote static". why did you not say "quote static unquote"? how am i supposed to know how long the quote is?
    also it's "char" (tshar), not "car".

  • @awadheshsingh7299
    @awadheshsingh7299 3 роки тому

    how is opaque type different from a private variable in java?

    • @rockthejvm
      @rockthejvm  3 роки тому

      We're talking about defining types (not variables) - there's also the concept of a private field/method in Scala as well.

    • @awadheshsingh7299
      @awadheshsingh7299 3 роки тому

      Understood. Thanks.

  • @JimBalter
    @JimBalter 4 роки тому +1

    half transparency is 0x80, not 0x88.

    • @rockthejvm
      @rockthejvm  4 роки тому +1

      😅 you get the idea

    • @JimBalter
      @JimBalter 4 роки тому

      @@rockthejvm Sure, but I've found similar bugs that persisted for years in legacy code, always giving inferior results.