Pick any Card: Write Better Data Structures with Scala | Let's talk about Scala 3

Поделиться
HTML-код

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

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

    Sorry if I could not answer your question in the live chat, please ask in the comments here!

    • @kumarisuman4565
      @kumarisuman4565 2 года назад

      Hi Jamie, this video was wonderful. I would love to see more videos from you.
      The masters of scala language should do videos like this, so that students can know and learn about scala and write scala the scala way.

  • @trevor369
    @trevor369 3 года назад +3

    Thanks for sharing, currently am learning scala at work and am amazed it's a very powerful language. @Jamie thanks for the clear explanations and the video was very clear. Am asking where can I access more of your tutorial (Project-based and preferably Scala 2) or a course. Thanks am ready to learn.

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

    There's something I don't understand about the generic enumeration: (the questions are somewhat related, I assume)
    1. What difference does it make to have Awaiting or Awaiting()? (With two non-generic enums, either seems fine)
    2. Why is the compiler not happy with
    enum Guess[T] {
    case Guess(value: T)
    case Awaiting
    }?
    3. With the generic enum, if we have val a: Guess[Card] = Guess.Awaiting() and val b: Guess[Suit] = Guess.Awaiting(), then a==b evaluates to true. What is a clean way to have separate Awaits?
    4. It appears that a and by have different types, but somehow they are equal. Can you use this example to explain what a generic enum is and does, exactly?

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

      Hi, thank you for your question. To answer part 3, you can stop a==b from compiling if you define Guess to derive CanEqual, like this:
      ```
      enum Guess[T] derives CanEqual { case Guess(value: T); case Awaiting() }.
      ```
      the `derives CanEqual` clause means that we should prove that `a` and `b` have the same type before we are allowed to compare for equality.
      Now if you try to compile `a == b` you get an error "Values of type Guess[Card] and Guess[Suit] cannot be compared with == or !="

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

      To be complete and answer part 4, equality for the `Awaiting` case is defined by checking if the other value is also an instance of the class `Guess.Awaiting`: the generic type parameter has no footprint at runtime, so can not be used for equality checking. But in Scala 3 we can use the type parameters to prevent nonsensical comparisons at compile time: dotty.epfl.ch/docs/reference/contextual/multiversal-equality.html

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

      @@jamie_thompson_ Nice, thanks.

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

    Nice intro into the new enums. By adding a type to the enum Guess, you're again allowing wrong values since T can also be an Int or String. How to narrow this down using union types?
    I tried out `enum Guess[T : (Card | Suite)] { .. }` on Scastie but it's giving errors like 'Card or Suite does not take type parameters' and 'enum class does not extend its enum class Guess' which I do not fully understand. Card and Suite indeed have no type parameters, but why does it complain about this?

    • @joostdenboer1154
      @joostdenboer1154 3 года назад +2

      Whoops. Had to use `[T

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

      @@joostdenboer1154 the other way would be to restrict at everywhere that takes a Guess to a specific `T` like
      ```
      def displayCardGuess(guess: Guess[Card]) = ???
      ```
      or
      ```
      def displayCardOrSuitGuess[T

    • @TJ-hs1qm
      @TJ-hs1qm 3 года назад

      But is T covariant or contravariant :p

  • @TJ-hs1qm
    @TJ-hs1qm 3 года назад

    or enum driven DDD

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

    I find that java enum syntax is more convinient

    • @Stumashedpotatoes
      @Stumashedpotatoes 3 года назад +2

      you can't even do generic enums in java, which is what is shown here

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

      1-2 word less = convinient? Generic programming in java sucks, that's enough for opting out of it

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

    Srry, I don't understand.. I could do the same with Java 1.6... nothing special, what am I missing? Other then, really small language decoration..

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

      A Java enum can only define constant values, and may not have a generic type parameter.
      The Scala enum syntax can be used to define a recursive list structure:
      enum List[+T] {
      case Concat(head: T, tail: List[T])
      case Terminal
      }

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

      @@jamie_thompson_ thnx, but you explained to me a language feature. You do not need it to do what he explained in the video....