notcpp-0.2.0.2: Avoiding the C preprocessor via cunning use of Template Haskell

Safe HaskellNone

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.

Synopsis

Documentation

scopeLookup :: String -> Q ExpSource

Produces a spliceable expression which expands to Just val if the given string refers to a value val in scope, or Nothing otherwise.

scopeLookup = fmap liftMaybe . scopeLookup'

scopeLookups :: [String] -> Q ExpSource

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.

scopeLookup' :: String -> Q (Maybe Exp)Source

Produces Just x if the given string names the value x, or Nothing otherwise.

liftMaybe :: Maybe Exp -> ExpSource

Turns Nothing into an expression representing Nothing, and Just x into an expression representing Just applied to the expression in x.

recoverMaybe :: Q a -> Q (Maybe a)Source

Turns a possibly-failing Q action into one returning a Maybe value.

maybeReify :: Name -> Q (Maybe Info)Source

A useful variant of reify that returns Nothing instead of halting compilation when an error occurs (e.g. because the given name was not in scope).

infoToExp :: Info -> Maybe ExpSource

Returns Just (VarE n) if the info relates to a value called n, or Nothing if it relates to a different sort of thing.