nenu-lang / conditions
Stable

Conditions

  • An if condition must have type bool; Nenu has no truthy conversion.
  • Use else if for ordered alternatives and else for the final fallback path.
  • Keep each branch responsible for one decision and move reusable calculations outside it.

Basic Syntax

code
1if expense.amount > 0 {2  valid = true3}45if expense.status == ExpenseStatus.Settled {6  label = "Settled"7} else {8  label = "Open"9}

The expression between if and { must resolve to bool. Parentheses around the condition are unnecessary. The braces are required even for one statement.

Ordered Alternatives

code
1if balance > 0 {2  label = "Is owed money"3} else if balance < 0 {4  label = "Owes money"5} else {6  label = "Even"7}

Branches are checked from top to bottom. The first true condition runs, and later alternatives are skipped. Put the narrowest or highest-priority case first.

Combining Conditions

code
1can_settle = amount > 0 and balance < 02needs_attention = amount <= 0 or title == ""34if can_settle and expense.status != ExpenseStatus.Settled {5  ready = true6}
OperatorMeaningOperand type
andBoth conditions are true.bool
orAt least one condition is true.bool
!Invert one condition.bool
Comparison operatorsCompare compatible values.Depends on the compared type.

Ternary Expressions

code
1string label = expense.status == ExpenseStatus.Settled : "Settled" | "Open"2number signed_amount = balance < 0 : -balance | balance

Use condition : then_value | else_value when one boolean decision produces one value. The condition must be bool, and both result expressions must have compatible types. Nenu permits at most two nested ternary levels; use an if statement or helper function for deeper branching.

Room Expense Example

code
1if request.amount <= 0 {2  throw BadRequest("Amount must be greater than zero")3}45if request.title == "" {6  throw BadRequest("Title is required")7}89Expense expense = Expense {10  room_id: room.id11  payer12  title: request.title13  amount: request.amount14  note: ?15  receipt: ?16  status: ExpenseStatus.Open17}1819save(expense)20return Response { expense }

The early branches stop invalid requests before construction or persistence. The successful path stays unindented and easy to read.

Common Mistakes

WrongCorrectReason
if amount { ... }if amount > 0 { ... }Numbers are not truthy.
if note { ... }with note { ... }Optionals require presence proof.
elifelse ifNenu uses two keywords.
if condition thenif condition { ... }There is no then keyword.
condition ? a : bcondition : a | bNenu uses colon and pipe for a ternary expression.

Continue Learning