Safe Haskell | None |
---|---|
Language | Haskell98 |
NotCPP.ScopeLookup
Description
This module exports scopeLookup
, which will find a variable or
value constructor for you and present it for your use. E.g. at some
point in the history of the acid-state package, openAcidState
was
renamed openLocalState
; for compatibility with both, you could
use:
openState :: IO (AcidState st) openState = case $(scopeLookup "openLocalState") of Just open -> open defaultState Nothing -> case $(scopeLookup "openAcidState") of Just open -> open defaultState Nothing -> error "openState: runtime name resolution has its drawbacks :/"
Or, for this specific case, you can use scopeLookups
:
openState :: IO (AcidState st) openState = open defaultState where open = $(scopeLookups ["openLocalState","openAcidState"])
Now if neither of the names are found then TH will throw a compile-time error.
Documentation
scopeLookup :: String -> Q Exp Source
Produces a spliceable expression which expands to
if
the given string refers to a value Just
valval
in scope, or Nothing
otherwise.
scopeLookup =fmap
liftMaybe
.scopeLookup'
scopeLookups :: [String] -> Q Exp Source
Finds the first string in the list that names a value, and produces a spliceable expression of that value, or reports a compile error if it fails.
recoverMaybe :: Q a -> Q (Maybe a) Source