-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A package exposing a helper function for generating smart constructors. -- @package smartconstructor @version 0.1.0.0 -- | This module exposes a makeSmartCtor function which -- automatically creates smart constructors for custom newtype'd Haskell -- types. See http://wiki.haskell.org/Smart_constructors for a -- more in-depth discussion of smart constructors in Haskell. -- -- Smart constructors are useful for imposing additional checks on -- values; given e.g. -- --
-- import SmartConstructor -- -- newtype Positive = Positive Int -- newtype NonEmptyList a = NonEmptyList [a] -- newtype Interval = Interval (Integer, Integer) ---- -- You can use makeSmartCtor to generate smart constructors as -- follows: -- --
-- -- Defines 'makePositive :: Int -> Maybe Positive' -- makeSmartCtor defaultOptions ''Positive [|(> 0)|] -- -- -- Defines 'makeNonEmptyList :: [a] -> Maybe (NonEmptyList a) -- makeSmartCtor defaultOptions ''NonEmptyList [|not . null|] ---- -- Notice how the third argument defines a predicate; the generated -- functions apply this predicate to the given value: if it yields true, -- the smart constructor call evaluates to a Just value. If the -- predicate yields false, the smart constructor evaluates to -- Nothing. -- -- By default, the name for the smart constructor is derived from the -- type name. A custom name can be specified by modifying the -- ctorName field of the defaultOptions: -- --
-- -- Defines 'createIV :: (Integer, Integer) -> Maybe Interval
-- makeSmartCtor defaultOptions{ ctorName = "createIV" } ''Interval [|uncurry (<=)|]
--
module SmartConstructor
-- | Values of the SmartCtorOptions type can be passed to
-- makeSmartCtor in order to customize the generated constructor
-- functions. At this point, only the name of the function can be
-- changed.
data SmartCtorOptions
SmartCtorOptions :: String -> SmartCtorOptions
-- | The desired name for the smart constructor function. An empty string
-- will make makeSmartCtor derive the function name from the type
-- by prepending make to the type name.
ctorName :: SmartCtorOptions -> String
-- | The default smart constructor generation options; the smart
-- constructor function will be named after the type, e.g.
--
-- -- makeSmartCtor defaultOptions ''Foo [|const True|] ---- -- defines a function makeFoo. defaultOptions :: SmartCtorOptions -- | The makeSmartCtor function creates a smart constructor for the -- given type, using the given predicate. makeSmartCtor :: SmartCtorOptions -> Name -> Q Exp -> Q [Dec]