Return specific type within Haskell

前端 未结 3 2047
我寻月下人不归
我寻月下人不归 2020-12-01 14:36

I have a pretty general question about Haskell\'s type system. I\'m trying to become familiar with it, and I have the following function:

getN :: Num a =>         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 15:08

    To add to sth's answer: Haskell is not object-oriented. It's not true that Double is a subclass of Num, so you cannot return a Double if you promise to return a polymorphic Num value, like you can in, say, Java.

    When you write getN :: Num a => a you promise to return a value that is fully polymorphic within the Num constraint. Effectively this means that you can only use functions from the Num type class, such as +, *, - and fromInteger.

提交回复
热议问题