- 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
- Using the JSON Ledger API
- 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
- Overview: Template Structure
- Reference: Templates
- Reference: Choices
- Reference: Updates
- Reference: Data Types
- Reference: Built-in Functions
- Reference: Expressions
- Reference: Functions
- Reference: Daml File Structure
- Reference: Daml Packages
- Reference: Contract Keys
- Reference: Interfaces
- Reference: Exceptions (Deprecated)
- 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
- Daml Script
- 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
fromSomefunction extracts the element out of aSomeand throws an error if its argument isNone.Note that in most cases you should prefer using
fromSomeNoteto get a better error on failures.
- fromSomeNote
-
Like
fromSomebut with a custom error message.
- catOptionals
: [Optional a] -> [a]
The
catOptionalsfunction takes a list ofOptionalsand returns a list of all theSomevalues.
- listToOptional
: [a] -> Optional a
The
listToOptionalfunction returnsNoneon an empty list orSomea where a is the first element of the list.
- optionalToList
: Optional a -> [a]
The
optionalToListfunction returns an empty list when givenNoneor a singleton list when not givenNone.
- fromOptional
: a -> Optional a -> a
The
fromOptionalfunction takes a default value and aOptionalvalue. If theOptionalisNone, it returns the default values otherwise, it returns the value contained in theOptional.
- mapOptional
: (a -> Optional b) -> [a] -> [b]
The
mapOptionalfunction is a version ofmapwhich 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, thenbis included in the result list.
- whenSome
: Applicative m => Optional a -> (a -> m ()) -> m ()
Perform some operation on
Some, given the field inside theSome.
- whenSome_
: Applicative m => Optional a -> (a -> m b) -> m ()
Perform some operation on
Someand discard its result, given the field inside theSome.
- whenNone
: Applicative m => Optional a -> m a -> m a
Perform some operation on
None. Otherwise returns content ofSomepured to Applicative.
- whenNone_
: Applicative m => Optional a -> m b -> m ()
Perform some operation on
None. Do nothing forSome. Convenient for discardingSomecontent.
- findOptional
: (a -> Optional b) -> [a] -> Optional b
The
findOptionalreturns the value of the predicate at the first element where it returnsSome.findOptionalis similar tofindbut 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.