2021-04-10 20:56:15 +03:00
|
|
|
{-# LANGUAGE ExplicitForAll #-}
|
|
|
|
|
|
|
|
module Witch.Lift where
|
|
|
|
|
|
|
|
import qualified Data.Typeable as Typeable
|
|
|
|
import qualified Language.Haskell.TH.Syntax as TH
|
2021-05-11 02:31:18 +03:00
|
|
|
import qualified Witch.TryFrom as TryFrom
|
2021-04-11 18:11:58 +03:00
|
|
|
import qualified Witch.Utility as Utility
|
2021-04-10 20:56:15 +03:00
|
|
|
|
2021-04-18 21:01:05 +03:00
|
|
|
-- | This is like 'Utility.unsafeFrom' except that it works at compile time
|
|
|
|
-- rather than runtime.
|
|
|
|
--
|
|
|
|
-- > -- Avoid this:
|
2021-05-11 04:56:28 +03:00
|
|
|
-- > unsafeFrom @s "some literal"
|
2021-04-18 21:01:05 +03:00
|
|
|
-- >
|
|
|
|
-- > -- Prefer this:
|
2021-05-11 04:56:28 +03:00
|
|
|
-- > $$(liftedFrom @s "some literal")
|
2022-09-08 16:55:50 +03:00
|
|
|
liftedFrom ::
|
|
|
|
forall source target m.
|
|
|
|
( TryFrom.TryFrom source target,
|
|
|
|
TH.Lift target,
|
|
|
|
Show source,
|
|
|
|
Typeable.Typeable source,
|
|
|
|
Typeable.Typeable target,
|
|
|
|
TH.Quote m
|
|
|
|
) =>
|
|
|
|
source ->
|
|
|
|
TH.Code m target
|
2021-05-11 04:04:51 +03:00
|
|
|
liftedFrom = TH.liftTyped . Utility.unsafeFrom
|
2021-04-10 20:56:15 +03:00
|
|
|
|
2021-04-18 21:01:05 +03:00
|
|
|
-- | This is like 'Utility.unsafeInto' except that it works at compile time
|
|
|
|
-- rather than runtime.
|
|
|
|
--
|
|
|
|
-- > -- Avoid this:
|
|
|
|
-- > unsafeInto @t "some literal"
|
|
|
|
-- >
|
|
|
|
-- > -- Prefer this:
|
2021-04-18 22:44:05 +03:00
|
|
|
-- > $$(liftedInto @t "some literal")
|
2022-09-08 16:55:50 +03:00
|
|
|
liftedInto ::
|
|
|
|
forall target source m.
|
|
|
|
( TryFrom.TryFrom source target,
|
|
|
|
TH.Lift target,
|
|
|
|
Show source,
|
|
|
|
Typeable.Typeable source,
|
|
|
|
Typeable.Typeable target,
|
|
|
|
TH.Quote m
|
|
|
|
) =>
|
|
|
|
source ->
|
|
|
|
TH.Code m target
|
2021-05-11 04:04:51 +03:00
|
|
|
liftedInto = liftedFrom
|