What's the point of 'const' in the Haskell Prelude?

后端 未结 9 1188
不思量自难忘°
不思量自难忘° 2020-12-12 18:50

Looking through the Haskell Prelude, I see a function const:

const x _ = x

I can\'t seem to find anything relevant regarding t

9条回答
  •  时光取名叫无心
    2020-12-12 19:30

    Another use is to implement class member functions that have a dummy argument which should not be evaluated (used to resolve ambiguous types). Example that could be in Data.bits:

    instance Bits Int where
      isSigned = const True
      bitSize  = const wordSize
      ...
    

    By using const we explicitly say that we are defining constant values.

    Personally I dislike the use of dummy parameters, but if they are used in a class then this is a rather nice way of writing instances.

提交回复
热议问题