Typeclasses for type conversion mappings.
This package provides the typeclasses From and TryFrom.
The From.From typeclass provides From.from :: a -> b. It is an interface a pair of types that can be converted from one to another.
The From.TryFrom typeclass provides From.tryFrom :: a -> Prelude.Maybeb. It is an interface for a pair of types that can be converted from one to another, safely handling the possibility of a failure.
Default From.From instances are provided for some integral types in base, using Prelude.fromIntegral.
This is the core package. This package itself is not spectacularly useful. One example of immediate usefulness would be the from-string package.
If you are interested, you may want to check the project README.md (not the package README.md) as well.
from
Note that this package is a part of the "project" that includes from and from-string. For the general rationale behind this idea, see README.md under the project root.
This Haskell library package provides the From and TryFrom typeclasses.
The From typeclass provides from :: a -> b. It is an interface for types that can be converted from/to each other.
The TryFrom typeclass provides tryFrom :: a -> Maybe b. It is an interface for types that can be converted from/to each other, with the possibility of a failure.
This package has 1 dependency: base.
Instances
It is difficult and often impossible to "selectively" import/export typeclass instances. Therefore, we need to be careful about what instances to provide by default.
There are base functions, some of them even in Prelude, that basically serve as a less general version of from. Examples include fromIntegral and fromEnum. However, naively defining
instance (Integral a, Num b) => From a bandinstance (Enum a) => From a Int
will quickly lead us to trouble:
• Overlapping instances for From Int16 Int
arising from a use of ‘from’
Matching instances:
instance (Integral a, Num b) => From a b
instance Enum a => From a Int
...
Therefore, this package does not provide such "contextual" instances.
Instead, concrete instances are provided. For example, all pairs (excluding self to self) of these integral types are instantiated using fromIntegral:
IntIntegerInt8Int16Int32Int64Word8Word16Word32Word64
Each type can be both source and destination of from. Additional types are instantiated as destination-only:
FloatDouble.