ivory-stdlib-0.1.0.0: Ivory standard library.

Safe HaskellNone

Ivory.Stdlib.Maybe

Contents

Description

This module provides an interface for a nullable Ivory type.

To define a type like Haskell's Maybe Float, define an Ivory structure type, and make the structure an instance of MaybeType.

 [ivory|
 struct maybe_float
   { mf_valid :: Stored IBool
   ; mf_value :: Stored IFloat
   }
 |]

 instance MaybeType "maybe_float" IFloat where
   maybeValidLabel = mf_valid
   maybeValueLabel = mf_value

With this definition in place, any of the functions in this module will accept a Struct "maybe_float".

These structure types must be defined in an Ivory module as usual, and it is recommended to make them private unless they are necessary as part of the module's public interface.

Synopsis

Interface

class (IvoryStruct sym, IvoryExpr t, IvoryStore t, IvoryInit t) => MaybeType sym t | sym -> t whereSource

Methods

maybeValidLabel :: Label sym (Stored IBool)Source

Return a boolean field indicating whether the value is valid.

maybeValueLabel :: Label sym (Stored t)Source

Return the field containing a value, if it is valid.

Initialization

initJust :: MaybeType sym a => a -> Init (Struct sym)Source

Return an initializer for a maybe type with a valid value.

initNothing :: MaybeType sym a => Init (Struct sym)Source

Return an initializer for a maybe type with no value.

Getting

getMaybe :: MaybeType sym a => ConstRef s1 (Struct sym) -> a -> Ivory eff aSource

Retrieve a maybe's value given a default if it is nothing.

Setting

setJust :: MaybeType sym a => Ref s1 (Struct sym) -> a -> Ivory eff ()Source

Set a maybe value to a valid value.

setNothing :: MaybeType sym a => Ref s1 (Struct sym) -> Ivory eff ()Source

Set a maybe value to an invalid value.

setDefault :: MaybeType sym a => Ref s1 (Struct sym) -> a -> Ivory eff aSource

Set a maybe's value to a default if it is nothing, returning the current value.

setDefault_ :: MaybeType sym a => Ref s1 (Struct sym) -> a -> Ivory eff ()Source

Set a maybe's value to a default value if it is nothing.

Modifying

mapMaybe :: MaybeType sym a => (a -> a) -> Ref s1 (Struct sym) -> Ivory eff ()Source

Modify a maybe value by an expression if it is not nothing.

mapMaybeM :: MaybeType sym a => (a -> Ivory eff a) -> Ref s1 (Struct sym) -> Ivory eff ()Source

Modify a maybe value by an action if it is not nothing.

mapMaybeM_ :: MaybeType sym a => (a -> Ivory eff ()) -> Ref s1 (Struct sym) -> Ivory eff ()Source

Call an action with a maybe value if it is not nothing.

forMaybeM :: MaybeType sym a => Ref s1 (Struct sym) -> (a -> Ivory eff a) -> Ivory eff ()Source

Flipped version of mapMaybeM.

forMaybeM_ :: MaybeType sym a => Ref s1 (Struct sym) -> (a -> Ivory eff ()) -> Ivory eff ()Source

Flipped version of mapMaybeM_.