headroom-0.4.1.0: License Header Manager
Copyright(c) 2019-2021 Vaclav Svejcar
LicenseBSD-3-Clause
Maintainervaclav.svejcar@gmail.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Headroom.Data.Coerce

Description

This module provides some extra functionality extending the Data.Coerce module.

Synopsis

Documentation

coerce :: forall (k :: RuntimeRep) (a :: TYPE k) (b :: TYPE k). Coercible a b => a -> b #

The function coerce allows you to safely convert between values of types that have the same representation with no run-time overhead. In the simplest case you can use it instead of a newtype constructor, to go from the newtype's concrete type to the abstract type. But it also works in more complicated settings, e.g. converting a list of newtypes to a list of concrete types.

This function is runtime-representation polymorphic, but the RuntimeRep type argument is marked as Inferred, meaning that it is not available for visible type application. This means the typechecker will accept coerce @Int @Age 42.

inner Source #

Arguments

:: Coercible a b 
=> (b -> b)

function to modify coerced value

-> a

value to modify

-> a

modified value

Allows to map the coercible value. This might be useful for example to change the value within newtype, without manually unwrapping and wrapping the value.

>>> import qualified RIO.Text as T
>>> newtype Foo = Foo Text deriving (Eq, Show)
>>> inner T.toUpper (Foo "hello")
Foo "HELLO"