In Scala, I can define an Algebraic data type :
scala> sealed trait Maybe[A] defined trait Maybe scala> case class Just[A](x: A) extends Maybe[A] defined class Just scala> case object NothingHere extends Maybe[Nothing] defined object NothingHere
It is possible to return a function f with the return type Maybe[A] .
scala> def f[A](x: A): Maybe[A] = Just(x) f: [A](x: A)Maybe[A]
However, you can also specify that a Just[A] returned.
scala> def f[A](x: A): Just[A] = Just(x) f: [A](x: A)Just[A]
Now I will do a similar exercise in Haskell:
Prelude> data Option a = None | Some a deriving Show Prelude> let fx = Some x :: Option Int Prelude> f 10 Some 10
But I cannot set the return type of the type constructor.
Prelude> let fx = Some x :: Some Int <interactive>:10:21: Not in scope: type constructor or class `Some' A data constructor of that name is in scope; did you mean DataKinds? Prelude> let fx = None :: None
Is the simple difference that Scala Just is a class, i.e. a legal return type? If in Haskell a type constructor cannot be a return type?
source share