from: Typeclasses for type conversion mappings

[ apache, library, typeclass ] [ Propose Tags ] [ Report a vulnerability ]

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.Maybe b. 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.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Flags

Manual Flags

NameDescriptionDefault
codegen

Enable code generation test

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 1.0.0.0, 1.0.0.1
Change log CHANGELOG.md
Dependencies base (>=4.7 && <5) [details]
License Apache-2.0
Copyright Copyright (C) 2024-2025 XT
Author XT
Maintainer git@xtendo.org
Category Typeclass
Home page https://codeberg.org/xt/from
Bug tracker https://codeberg.org/xt/from/issues
Source repo head: git clone https://codeberg.org/xt/from
Uploaded by xt at 2025-10-24T12:41:06Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 5 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2025-10-24 [all 1 reports]

Readme for from-1.0.0.1

[back to package description]

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 b and
  • instance (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:

  • Int
  • Integer
  • Int8
  • Int16
  • Int32
  • Int64
  • Word8
  • Word16
  • Word32
  • Word64

Each type can be both source and destination of from. Additional types are instantiated as destination-only:

  • Float
  • Double