Proposal: Add a `cond` expression
Basics
- I have done a basic search through the issue tracker to find similar or related issues.
- I have made myself familiar with the available features of Nushell for the particular area this enhancement request touches.
Related problem
Nushell supports conditional expressions with if and chained else if clauses.
For several independent boolean tests, repeated if/else wording can make the branches harder to scan.
A cond expression could make it clear at a glance that the code is choosing among a sequence of conditions.
When the branches instead compare one value against alternatives, match is already a natural fit.
For example, an access decision might combine an account's role, a resource's visibility, and the state of the account. These predicates depend on different values:
if $account.is_locked {
"deny: account locked"
} else if $account.role == "admin" and $account.is_active {
"allow"
} else if $resource.is_public and $account.is_active {
"allow"
} else {
"deny"
}Nu's pattern-matching guide presents match as a more ergonomic alternative to long if/else if chains.
Guards let match branches test conditions involving multiple values. Matching against true and using a guard for each condition is one (dirty) way to express such a chain:
match true {
_ if $account.is_locked => "deny",
_ if $account.role == "admin" and $account.is_active => "allow",
_ if $resource.is_public and $account.is_active => "allow",
_ => "deny",
}if remains concise for one condition and an optional fallback. match remains the natural choice when comparing one value against patterns. cond would serve cases in which several independent boolean predicates select among results.
Describe the solution you'd like
Proposed syntax
To keep the syntax clear and consistent, it should follow the same principles as the match construct, but without an input value or guard prefix (that is, without true or _ if, respectively, as shown in the example above).
cond {
$account.is_locked => "deny",
$account.role == "admin" and $account.is_active => "allow",
$resource.is_public and $account.is_active => "allow",
else => "deny",
}The dedicated else branch (see fallback conventions in other languages in the Notes section) makes the fallback explicit and must be the final branch.
Proposed behavior
cond is an expression. Like match, it checks branches in source order, but uses boolean conditions instead of patterns:
- Evaluate conditions in source order. The first condition that evaluates to
trueselects its branch. - Do not evaluate later conditions or branch results after a branch matches.
- Require each condition to evaluate to a boolean. If a condition evaluates to a non-boolean value, raise an error, following
ifandmatchbehavior. - Evaluate only the selected branch's result expression and return its value. A branch result can be a single expression or a block; a block returns the value of its last expression.
- Allow an optional final fallback branch written as
else. - If no condition matches and there is no
elsebranch, returnnull, asifandmatchdo when no branch matches.
Describe alternatives you've considered
Fallback conventions in other languages
Choosing else as fallback keyword is a matter a preference.
Here is the solutions we can found in other languagues:
- Common Lisp
condconventionally usestas the final test;tis always true. - Elixir
condusestrueas the final condition and describes it as equivalent toelse. Elixir'scondaccepts truthy values in general, while this proposal requires boolean conditions, as Nushell'sifdoes. - Clojure
condcommonly uses:else, a keyword that is always truthy, as its final test. - Racket
condprovides a dedicatedelseclause, which must be last.
Additional context and details
No response
Source: nushell/nushell