elsewhen

The elsewhen command gives a when construct another test, tried when the earlier ones were false.

Syntax

elsewhen ( <expression> ) then

expression is as for when. The word then is required, and elsewhen stands alone on its line.

What it does

If no earlier branch of the construct was taken, the preprocessor tests the guard. When it is true, James passes the branch that follows to the parser and drops the later ones. A guard after the branch taken is never examined.

When James applies it

By the preprocessor, when it is read. The text of a branch not taken is dropped. A when is resolved before the phase of the parser matters, so it is legal anywhere.

Rules James adds

  • An elsewhen after an else in the same construct is an error.
  • A guard in a branch not reached is not tested, so an undefined parameter there raises nothing.

Diagnostics

  • E0305: the elsewhen follows an else in the same construct.
  • E0314: there is no open when for it to continue.
  • E0309: the keyword shares its line with other input.
  • E0317: the then is missing.
  • E0203: a guard names a parameter that has no value yet.
  • E0221: a guard is not a well-formed expression, for example one that holds a [ ].

Example

The script tests/corpus/113-elsewhen-selects-branch/input.tg, which make test runs:

para x1 2;
para x2 5;
block
when(%x1.gt.%x2) then
1 3;1 3;1 3;
elsewhen(%x1.lt.%x2) then
1 2;1 2;1 2;
else
1 4;1 4;1 4;
endwhen
1 2 1 2 1 2
endpart

The elsewhen on line 6 is the branch taken, because %x1.lt.%x2 is true. The prep dump shows its 1 2;1 2;1 2; and neither of the other two.

See also

  • when: the construct this belongs to.
  • else: the branch with no test.
  • elseif: the if family’s form.

James 0.3.1.

This site uses Just the Docs, a documentation theme for Jekyll.