In ghci
, I ran:
ghci> :t Right 5
Right 5 :: Num b => Either a b
What's the meaning of a
?
How does it compare with Scala's version?
scala> Right(5)
res0: scala.util.Right[Nothing,Int] = Right(5)
a
is, like b
in this example, a type variable. It can be instantiated with any type (whereas b
can be instantiated with any type that satisfies the constraint that it is also an instance of Num
).
The scala example works quite differently due to scala's type system being quite different; There is no real concept of a value ever having a not fully instantiated type, so you need to assign a type to the Left
possibility of your Either
value. Barring further constraints, this just ends up being Nothing
. Due to the way scala's type system works (Nothing
being a subtype of any other type, so you can think of it as a dual to the Any
type) an Either[Nothing,B]
is also an Either[A,B]
for any A
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With