Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`Right 5` in Haskell and Scala

Tags:

haskell

scala

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)
like image 908
Kevin Meredith Avatar asked Sep 04 '15 03:09

Kevin Meredith


1 Answers

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.

like image 182
Cubic Avatar answered Oct 02 '22 13:10

Cubic