Hi @Oleg,
Daml is a purely functional language, which among other things means that every code construct is an expression, and every expression returns a value. It’s also a statically typed language, meaning every expression must have a (statically defined) type.
So the if condition then then-expression else else-expression
construct is an expression that returns a value of a given type. From a type perspective:
- The
condition
expression needs to evaluate to a Boolean value.
- The
then-expression
and the else-expression
must have the same type, which will also be the type of the overall if
expression.
So in a = if ... then ... else ...
, a
can have a single, defined type.
The language does not support an if
expression without an else
branch. Given the above constraints, how could it? If you write
a = if False then 4
what is the type of a
? What is the value of a
? That would just not make sense.
Now, in the specific context of a do
block, it’s tempting to think of if
as a statement rather than an expression. But the language does not make a special case here; it’s the same if
construct and it’s just as much of an expression. Within a do
block, a line of the form
if ... then ... else ...
is a shorthand for
_unused_name <- if ... then ... else ...
which is to say we are still expecting a resulting value and we are still capturing it in a local binding, even though that binding is not getting used later on. Thus we still need an else
branch to get a meaningful, well-typed expression.
If you’re not meaning to use the result of your then
branch, you can rewrite your code to
if someCondition2 then do
exercise template1Cid SomeChoiceOnThatTemplate with someParam = someValue
pure ()
else
pure ()
such that both branches return an Action ()
.