From Inline to Value Classes

Поделиться
HTML-код
  • Опубликовано: 15 сен 2024

Комментарии • 30

  • @juanherrera9521
    @juanherrera9521 3 года назад +18

    So basically value classes help to write more explicit code without runtime overhead. Nice 🎉

  • @divyanshbhardwaj3453
    @divyanshbhardwaj3453 3 года назад +4

    It just goes to show the brilliance and hard work of the Kotlin language engineers! Feels awesome to be a part of such a nice ecosystem.

  • @Codetutor-DemystifyCoding
    @Codetutor-DemystifyCoding 2 года назад +1

    If you are wondering if everything is same between inline and value classes, why the fuss over it by having two things , then directly jump at 10:10

  • @bboydarknesz
    @bboydarknesz 3 года назад

    Finally I can understard why it starts with "value" keyword
    to replace it to value

  • @droidcasts7350
    @droidcasts7350 3 года назад +1

    nice! to learn how useful it is, looking at go lang usage of type, scala's value classes and haskell's newtype would help

  • @Das.Kleine.Krokodil
    @Das.Kleine.Krokodil 11 месяцев назад

    Thanke

  • @andy_lamax
    @andy_lamax 3 года назад +5

    is the `@JvmInline` annotation a must? What happens if you don't write it?

    • @PeteC62
      @PeteC62 3 года назад +5

      Eventually you'll get a value-based class that isn't inlined in the same way, ie the JVM code generation is different. A value-based class is one where equality is based on the underlying values in the class, and there is no concept of identity (so you can't use === to compare instances of a value-based class, only ==). If you omit the @JvmInline, you can still use the class in exactly the same way, but you won't get the under-the-covers inlining of the primitive value, auto boxing and unboxing, function name mangling etc. It will compile just like any other class.
      ETA: But for now non-inlined value classes aren't supported, so the annotation is required for JVM backends.

    • @abdonasr1222
      @abdonasr1222 3 года назад

      My Understanding is that value classes provided for all kotlin targets so, you need to annotate it with @JvmInline when you're writing code for Jvm backend (i.e Android)

    • @juanherrera9521
      @juanherrera9521 3 года назад

      I’m not sure, but maybe this is for Java Interop, so if you’re using kotlin alone maybe you don’t need the annotation (?

    • @ben.woodworth
      @ben.woodworth 3 года назад +5

      It's required for now, and it limits the class to one property since that's the only way inlining like this is possible. It's because the JVM doesn't support value types yet (project Valhalla), with multiple properties, but the other platforms do.

  • @JozuaSijsling
    @JozuaSijsling 3 года назад

    Would be nice to know in what scenarios the compiler is unable to inline the value class.

  • @e-wag
    @e-wag 3 года назад

    Does the other video about the specific use cases when you would use value classes mentioned at the end already exist?

  • @SriHarshaChilakapati
    @SriHarshaChilakapati 3 года назад +6

    How is this handled on non-JVM platforms? Kotlin Native and / or JS?

    • @PeteC62
      @PeteC62 3 года назад +6

      Because there's no requirement for interoperability with other JVM languages (in particular, Java), a value class with a single property is automatically inlined in /JS and /Native. So you get the same performance advantages as you do with /JVM when annotated with the @JvmInline.

    • @forresthopkinsa
      @forresthopkinsa 3 года назад

      @@PeteC62 Wouldn't you encounter the same interoperability problems in Objective-C or JS though?

    • @PeteC62
      @PeteC62 3 года назад +1

      @@forresthopkinsa Not really. In those environments, as long as you know the code-generation strategy (inline references to the wrapped object), you can write code to interop with it. With JVM code generation, there are two mutually incompatible options: generate code compatible with Java value-based classes, or perform inlining for optimization. Since the code generated if different in each case, the annotation (or its absence) tells the compiler which variant to generate code for.

    • @SriHarshaChilakapati
      @SriHarshaChilakapati 3 года назад

      @@PeteC62 I guess in native side, there was no specific need as LLVM IR (which both Swift and Objective-C compiles to) has support for inline attribute. This would become a problem only if Kotlin Native uses a different compiler backend other than LLVM. Is my understanding correct?

    • @SriHarshaChilakapati
      @SriHarshaChilakapati 3 года назад

      And in the case of JS I wonder what happens. As far as I know JS has no inline support, so how is it done there?

  • @FoRRestDp
    @FoRRestDp 3 года назад

    Вы забыли добавить ссылку на видео Романа Елизарова про value-классы

  • @cisimon7
    @cisimon7 3 года назад

    Amazing, kinda like newtype in Haskell I think

  • @Alberto_Cavalcante
    @Alberto_Cavalcante 3 года назад

    Cool!

  • @pixelPlex
    @pixelPlex 3 года назад

    Which data types can be used with a Value Class?

  • @vipulthakur1060
    @vipulthakur1060 3 года назад

    very informative video...but it's seriously a little bit difficult to understand because of her accent :(