Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Polymorphic length
Synopsis
- length :: (Integral i, Container a, DefaultToInt (IsIntSubType Length i) i) => a -> i
Documentation
length :: (Integral i, Container a, DefaultToInt (IsIntSubType Length i) i) => a -> i Source #
Polymorphic version of length
.
Defaults to Int
, same as the non-polymorphic version, if return value is
ambiguous.
>>>
let list = [1..100]
>>>
:t length list
length list :: Int
>>>
length list :: Word
100>>>
length list :: Int
100>>>
length list :: Word63
100>>>
length list :: Natural
100>>>
length list :: Integer
100>>>
length list :: Word8
... ... error: ... Can not safely cast 'Morley.Prelude.Length.Length' to 'Word8': ... 'Morley.Prelude.Length.Length' is not a subtype of 'Word8' ...
One caveat that with an unsuitable monomorphic type, length
will also default
to Int
:
>>>
length list :: Bool
... ... error: ... Couldn't match type ‘Bool’ with ‘Int’ ...
However, this lets us avoid the issue with integral literals.