- Overview
- Tutorials
- Getting started
- Get started with Canton and the JSON Ledger API
- Get Started with Canton, the JSON Ledger API, and TypeScript
- Get started with Canton Network App Dev Quickstart
- Get started with smart contract development
- Basic contracts
- Test templates using Daml scripts
- Build the Daml Archive (.dar) file
- Data types
- Transform contracts using choices
- Add constraints to a contract
- Parties and authority
- Compose choices
- Handle exceptions
- Work with dependencies
- Functional programming 101
- The Daml standard library
- Test Daml contracts
- Next steps
- Application development
- Getting started
- Development how-tos
- Component how-tos
- Explanations
- References
- Application development
- Smart contract development
- Daml language cheat sheet
- Daml language reference
- Daml standard library
- DA.Action.State.Class
- DA.Action.State
- DA.Action
- DA.Assert
- DA.Bifunctor
- DA.Crypto.Text
- DA.Date
- DA.Either
- DA.Exception
- DA.Fail
- DA.Foldable
- DA.Functor
- DA.Internal.Interface.AnyView.Types
- DA.Internal.Interface.AnyView
- DA.List.BuiltinOrder
- DA.List.Total
- DA.List
- DA.Logic
- DA.Map
- DA.Math
- DA.Monoid
- DA.NonEmpty.Types
- DA.NonEmpty
- DA.Numeric
- DA.Optional
- DA.Record
- DA.Semigroup
- DA.Set
- DA.Stack
- DA.Text
- DA.TextMap
- DA.Time
- DA.Traversable
- DA.Tuple
- DA.Validation
- GHC.Show.Text
- GHC.Tuple.Check
- Prelude
- Smart contract upgrading reference
- Glossary of concepts
DA.Optional¶
The Optional
type encapsulates an optional value. A value of type
Optional a
either contains a value of type a
(represented as Some a
),
or it is empty (represented as None
). Using Optional
is a good way
to deal with errors or exceptional cases without resorting to
drastic measures such as error.
The Optional type is also an action. It is a simple kind of error
action, where all errors are represented by None
. A richer
error action can be built using the Either
type.
Functions¶
- fromSome
: Optional a -> a
The
fromSome
function extracts the element out of aSome
and throws an error if its argument isNone
.Note that in most cases you should prefer using
fromSomeNote
to get a better error on failures.
- fromSomeNote
-
Like
fromSome
but with a custom error message.
- catOptionals
: [Optional a] -> [a]
The
catOptionals
function takes a list ofOptionals
and returns a list of all theSome
values.
- listToOptional
: [a] -> Optional a
The
listToOptional
function returnsNone
on an empty list orSome
a where a is the first element of the list.
- optionalToList
: Optional a -> [a]
The
optionalToList
function returns an empty list when givenNone
or a singleton list when not givenNone
.
- fromOptional
: a -> Optional a -> a
The
fromOptional
function takes a default value and aOptional
value. If theOptional
isNone
, it returns the default values otherwise, it returns the value contained in theOptional
.
- mapOptional
: (a -> Optional b) -> [a] -> [b]
The
mapOptional
function is a version ofmap
which can throw out elements. In particular, the functional argument returns something of typeOptional b
. If this isNone
, no element is added on to the result list. If it isSome b
, thenb
is included in the result list.
- whenSome
: Applicative m => Optional a -> (a -> m ()) -> m ()
Perform some operation on
Some
, given the field inside theSome
.
- findOptional
: (a -> Optional b) -> [a] -> Optional b
The
findOptional
returns the value of the predicate at the first element where it returnsSome
.findOptional
is similar tofind
but it allows you to return a value from the predicate. This is useful both as a more type safe version if the predicate corresponds to a pattern match and for performance to avoid duplicating work performed in the predicate.