Skip to content

Commit 4cd725b

Browse files
committed
feat: adds else if and else conditional clauses
1 parent 043a2ab commit 4cd725b

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

SPEC.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -7146,11 +7146,11 @@ Example output:
71467146

71477147
### Conditional Statement
71487148

7149-
A conditional statement consists of the `if` keyword, followed by a `Boolean` expression and a body of (potentially nested) statements. The conditional body is only evaluated if the conditional expression evaluates to `true`.
7149+
A conditional statement consists of one or more conditional clauses. Each conditional clause is comprised of an expression that evaluates to a `Boolean` and an associated clause body. When a conditional statement is executed, each of the conditional clauses is evaluated sequentially: (a) the expression for that clause is evaluated and, if the returned value is `true`, the body of that clause is executed and the entire conditional statement suspends further execution. The simplest conditional statement contains a single "if" clause, which is the `if` keyword, followed by the clause's boolean expression, and, finally, the clause body of (potentially nested) statements wrapped in curly brackets.
71507150

71517151
After evaluation of the conditional has completed, each declaration or call output in the conditional body is exposed in the enclosing context as an optional declaration. In other words, for a declaration or call output `T <name>` within a conditional body, a declaration `T? <name>` is implicitly available outside of the conditional body. If the expression evaluated to `true`, and thus the body of the conditional was evaluated, then the value of each exposed declaration is the same as its original value inside the conditional body. If the expression evaluated to `false` and thus the body of the conditional was not evaluated, then the value of each exposed declaration is `None`.
71527152

7153-
The scoping rules for conditionals are similar to those for scatters - declarations or call outputs inside a conditional body are accessible within that conditional and any nested statements.
7153+
The scoping rules for conditionals are similar to those for scattersdeclarations or call outputs inside a conditional body are accessible within that conditional and any nested statements.
71547154

71557155
In the example below, `Int j` is accessible anywhere in the conditional body, and `Int? j` is an optional that is accessible outside of the conditional anywhere in `workflow test_conditional`.
71567156

@@ -7231,7 +7231,9 @@ Example output:
72317231
</p>
72327232
</details>
72337233

7234-
WDL has no `else` keyword. To mimic an `if-else` statement, you would simply use two conditionals with inverted boolean expressions. A common idiom is to use `select_first` to select a value from either the `if` or the `if not` body, whichever one is defined.
7234+
After the initial `if` clause, conditional statements may have any number of `else if` clauses and a single, final `else` clause. As described in the paragraph above, `else if` clauses are only evaluated if all prior clauses in the statement have evaluated to `false`. If present, the final `else` clause executes only if all prior clauses evaluated to `false`.
7235+
7236+
When gathering results from conditionals, a common idiom is to use `select_first` to select a value from one of the `if`, `else if`, or `else` bodies—whichever one is defined.
72357237

72367238
<details>
72377239
<summary>
@@ -7246,7 +7248,7 @@ task greet {
72467248
}
72477249
72487250
command <<<
7249-
printf "Good ~{time} buddy!"
7251+
printf "Good ~{time} buddy!"
72507252
>>>
72517253
72527254
output {
@@ -7259,13 +7261,11 @@ workflow if_else {
72597261
Boolean is_morning = false
72607262
}
72617263
7262-
# the body *is not* evaluated since 'b' is false
72637264
if (is_morning) {
7265+
# The body *is not* evaluated since `is_morning` is `false`.
72647266
call greet as morning { time = "morning" }
7265-
}
7266-
7267-
# the body *is* evaluated since !b is true
7268-
if (!is_morning) {
7267+
} else {
7268+
# The body *is* evaluated since the clause above did not trigger.
72697269
call greet as afternoon { time = "afternoon" }
72707270
}
72717271

0 commit comments

Comments
 (0)