Type Lambdas in Scala 3 | Rock the JVM

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

КОМЕНТАРІ • 23

  • @vicc8881
    @vicc8881 4 роки тому +14

    Hello, great video. These video series is like Netflix series, it ends one chapter and you want the next one.

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

    Here's how I learned about kinds in an ML context:
    - Types that either take no parameters or have all their parameters applied have kind "*".
    - Generic types that expect one parameter have kind "* => *". They're like a function, they take one type (of kind "*") as input and produce a type (of kind "*"). For example, List can take Int as input and produce the type List[Int], or it can take String as input and produce List[String], and so on.
    - Generic types that take two arguments have kind "(*, *) -> *". Note that types are not curried in Scala; they are in ML and Haskell.
    - Generic types that take one-argument generics as an argument have kind "(STAR -> STAR) -> STAR". For example, your Functor and Monad examples: you had "trait/class Functor[F[_]]", so Functor takes a type F as argument, where F itself takes one type as an argument. So F is of kind "STAR -> STAR", and Functor takes an F and produces a type, so Functor has kind "[The-kind-of-F] -> STAR", or "(STAR -> STAR) -> STAR". [edit: turn "*" into "STAR" such that youtube doesn't bold the paragraph. Thanks, youtube.]
    - And so on.
    - If you represent kinds as a tree with "*" as leaves and arrows as inner nodes, your "kind level" concept equals the height of this tree, at least if all right subtrees are arrow-free. But note, here's a tree: "* -> (* -> *)". I think this kind is inexpressible in current Scala?
    I look at the Monad example with type lambdas, and it got me thinking: what can you express with type lambdas that you couldn't express with kind projection?
    Actually, the first thing I thought was "why can't I use underscore for kind projection?" So the syntax would be e.g. "Class EitherMonad[E] extends Monad[Either[E, _]] { ... }". The rule would be that an application of a generic type to some "_" type arguments is a type with n arguments where n is the number of underscores, filled in left to right. I expect the kind projector to do exactly this, but with '?'. Underscore already means wildcard/unspecified in Scala, using it here seems a natural fit? What problems does it run into?
    This example also suggests an answer to my previous questions: If you have a type "T[U[_, _]]" that is the same as "T[[A, B] =>> U[A, B]]" if I understood correctly. This means you can't express "T[[A, B] =>> U[B, A]]" with kind projection, since the order is left-to-right.
    Is "[T] =>> [U] =>> Either[T, U]" a valid thing in Scala 3? In Scala 2 you can do "{ type T[A] = blah }#T", so I guess we're used to apply type arguments interleaved with selecting type members. I guess the logic of the type system doesn't become less solvable or anything if type member selection is taken out of the process, and you can directly say "MyTypeLambda[ConcreteT][ConcreteU]"?
    Out of curiousity, would using the already familiar "=>" instead of "=>>" cause any logical problems? Was that a constrained or free decision, and in the latter case what was the basis for the decision?

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

      Wow, there's a lot in here. Let me add some (short) color:
      1 - The underscore is already quite overloaded (see blog.rockthejvm.com/underscore-in-scala/ ), there's a balance between using a familiar symbol and having a new symbol ('?'), and I suspect the Scala team chose '_' for a better fit.
      2 - You can only express `T[[A, B] =>> U[B, A]]` explicitly. The underscores indeed assume left-to-right.
      3 - Curried type lambdas are legal in Scala 3.
      4 - I think there would have been confusions between A => B as a function type and A => B as a type lambda, so a new symbol was needed.

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

    Thank you! I've read about these a few times but didn't really get it until now.

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

    Now that's powerful content by scala master. Thanks!!!! and please upload video for microservices in scala..humble request by Fan!!!!!!

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

      Will do! Lots of ideas for videos, so you'll definitely see something on this front.

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

    Nice work Daniel! Hope you will release a course on Scala 3 :-)

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

    Really good content, subscribed!

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

      Thanks! Just getting started

  • @mackler
    @mackler Рік тому +2

    Thank you!

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

    Cool video, and again it boils down to functions. Type lambdas or in this case partially applied types work exact the same as partially applied functions but a the type level. You apply types to the type constructor until you get back a partially applied type constructor with the remaining holes/types you need. I like the kind projector which just generates the ugly lambda code for me. Having this natively in the language is even cooler. Can't wait to adopt scala 3. Next video stack safety and trampolines 🤪?

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

      Lots of topics upcoming!

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

    really cool

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

    Nice video. The type lambda syntax is greatly improved. In Scala 2.x it looked like this class EitherMonad[E] extends Monad[({type IntEither[A] = Either[Int, A]})#IntEither]

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

      Yep, we have it in the article version.

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

    Feel like I'm watching magical spells written by a wizard. Difficult, but enjoyable.

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

    As someone who wrote a library translating Monads and stuff from Haskell to Java, I'm jealous.

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

    Everybody gangsta until Light Mode IntelliJ