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.
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.
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?
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 !="
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
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 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
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 }
Sorry if I could not answer your question in the live chat, please ask in the comments here!
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.
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.
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?
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 !="
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
@@jamie_thompson_ Nice, thanks.
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?
Whoops. Had to use `[T
@@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
But is T covariant or contravariant :p
or enum driven DDD
I find that java enum syntax is more convinient
you can't even do generic enums in java, which is what is shown here
1-2 word less = convinient? Generic programming in java sucks, that's enough for opting out of it
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..
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
}
@@jamie_thompson_ thnx, but you explained to me a language feature. You do not need it to do what he explained in the video....