Wrap with coalescing

Wrap with coalescing puts a selected expression on the left of one of EK9's coalescing operators, with what to fall back on on the right: ?: defaults an unset value, <? and >? return the lesser or greater of two operands - the set one when only one is. Here displayName takes name with no default when name is unset. To play it in the IDE: Help > Lessons > Refactoring…, or ek9 -ide -lesson wrap-with-coalescing.

The EK9 IDE as the lesson opens

Start from

The code the lesson starts from: copy it into a file of your own and follow the steps.

#!ek9
defines module lesson.greet

  defines program

    Greet()
      stdout <- Stdout()
      name <- String()
      displayName <- name
      stdout.println(`Hello, ${displayName}!`)

//EOF

Step 1: The expression

Select name, on line 9.

The EK9 IDE once this is done: Select name, on line 9

Step 2: Wrap with coalescing

Right-click it - or choose Refactor > Refactor This… - and choose Wrap with coalescing…

The EK9 IDE once this is done: Right-click it - or choose Refactor > Refactor This… - and choose Wrap with coalescing…

Choose ?: - elvis, a default when the left is unset - type "Anonymous" for the right operand, and choose Preview.

The EK9 IDE just before choosing Preview: Choose ?

Step 3: Preview and apply

The preview shows every change the refactoring makes; then the IDE formats each file it changed. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows every change the refactoring makes; then the IDE formats each file it changed

The result

That is Wrap with coalescing: displayName is name ?: "Anonymous" - "Anonymous" only when name is unset, name itself otherwise. <? and >? wrap the same way, returning whichever operand is set, or the lesser/greater when both are. It is on the right-click menu whenever a single-line expression is selected; AI assistants use the ek9_insert_coalescing tool.

The EK9 IDE when the lesson has finished

Where this lesson comes from