morley-prelude-0.6.0: A custom prelude used in Morley
Safe HaskellSafe-Inferred
LanguageHaskell2010

Morley.Prelude.Length

Description

Polymorphic length

Synopsis

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.