Overloaded Literals

Download haskell eBook

Remarks

Integer Literals

is a numeral without a decimal point

for example 0, 1, 42, ...

is implicitly applied to fromInteger which is part of the Num type class so it indeed has type Num a => a - that is it can have any type that is an instance of Num


Fractional Literals

is a numeral with a decimal point

for example 0.0, -0.1111, ...

is implicitly applied to fromRational which is part of the Fractional type class so it indeed has type a => a - that is it can have any type that is an instance of Fractional


String Literals

If you add the language extension OverloadedStrings to GHC you can have the same for String-literals which then are applied to fromString from the Data.String.IsString type class

This is often used to replace String with Text or ByteString.


List Literals

Lists can defined with the [1, 2, 3] literal syntax. In GHC 7.8 and beyond, this can also be used to define other list-like structures with the OverloadedLists extension.

By default, the type of [] is:

> :t []
[] :: [t]

With OverloadedLists, this becomes:

[] :: GHC.Exts.IsList l => l

Related Examples

Stats

212 Contributors: 5
Friday, July 22, 2016
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook