- 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
Reference: Expressions¶
This page gives reference information for Daml expressions that are not updates.
Definitions¶
Use assignment to bind values or functions at the top level of a Daml file or in a contract template body.
Values¶
For example:
pi = 3.1415926535
The fact that pi
has type Decimal
is inferred from the value. To explicitly annotate the type, mention it after a colon following the variable name:
pi : Decimal = 3.1415926535
Functions¶
You can define functions. Here’s an example: a function for computing the surface area of a tube:
tubeSurfaceArea : Decimal -> Decimal -> Decimal
tubeSurfaceArea r h =
2.0 * pi * r * h
Here you see:
the name of the function
the function’s type signature
Decimal -> Decimal -> Decimal
This means it takes two Decimals and returns another Decimal.
the definition
= 2.0 * pi * r * h
(which uses the previously definedpi
)
Arithmetic Operators¶
Operator |
Works for |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
The result of the modulo operation has the same sign as the dividend:
7 / 3
and(-7) / (-3)
evaluate to2
(-7) / 3
and7 / (-3)
evaluate to-2
7 % 3
and7 % (-3)
evaluate to1
(-7) % 3
and(-7) % (-3)
evaluate to-1
To write infix expressions in prefix form, wrap the operators in parentheses. For example, (+) 1 2
is another way of writing 1 + 2
.
Comparison Operators¶
Operator |
Works for |
---|---|
|
|
|
|
Logical Operators¶
The logical operators in Daml are:
not
for negation, e.g.,not True == False
&&
for conjunction, wherea && b == and a b
||
for disjunction, wherea || b == or a b
for Bool
variables a
and b
.
If-then-else¶
You can use conditional if-then-else expressions, for example:
if owner == scroogeMcDuck then "sell" else "buy"
Let¶
To bind values or functions to be in scope beneath the expression, use the block keyword let
:
doubled =
-- let binds values or functions to be in scope beneath the expression
let
double (x : Int) = 2 * x
up = 5
in double up
You can also use let
inside do
blocks:
blah = script
do
let
x = 1
y = 2
-- x and y are in scope for all subsequent expressions of the do block,
-- so can be used in expression1 and expression2.
expression1
expression2