简体   繁体   中英

`Right 5` in Haskell and 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)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM