Closed jasonrute closed 5 years ago
Isn't variance/subtyping fun :) This is a great suggestion, this is how Scala ADTs are typically defined. I would really appreciate a PR for this. Thanks!
Sure. I'll make a PR for this file (https://github.com/hmemcpy/milewski-ctfp-pdf/blob/master/src/content/1.6/code/scala/snippet22.scala) later today or tomorrow.
Fixed in #180.
In the section 6.3 Sum Types, there is an example called
OneOfThree
with the following Scala code:While this will compile, it won't work. It is impossible (at least without type coercion) to get a type of, say,
OneOfThree[Int, Int, Int]
. The solution is to make the type parameters covariant by adding+
s to the first line as follows:This would agree with the examples of
Either[+A,+B]
andOption[+A]
in the same section of the book, and it is idiomatic Scala.(There is another possible solution which is to change all the other lines to something like
final case class Sinistral[A, B, C](v: A) extends OneOfThree[A, B, C]
, but that would be unwieldy in practice and be different from the other examples in the book.)