Rewrite String Concatenation

Rewrite String Concatenation turns a + chain of three or more parts into the equivalent backtick interpolation. Here "Hello " + name + "!" is three parts, so EK9 refuses it - E11068, explained in the Hovers panel. To play it in the IDE: Help > Lessons > Refactoring…, or ek9 -ide -lesson rewrite-string-concat.

The EK9 IDE as the lesson opens: the code to read and, in the Hovers panel, its error explained

Start from

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

Fails with E11068: string concatenation with 3+ parts - use string interpolation instead

#!ek9
defines module error.e11068.bad

  defines function

    threePartChain()
      -> name as String
      <- result as String: String()
      result: "Hello " + name + "!"

    fourPartChain()
      ->
        first as String
        last as String
      <- result as String: String()

      result: first + " " + last + " here"

    fivePartChainWithDollar()
      ->
        x as Integer
        y as Integer
      <- result as String: String()

      result: "(" + $x + ", " + $y + ")"

Step 1: Where it is

Put the caret on Hello, on line 9.

The EK9 IDE once this is done: Put the caret on Hello, on line 9

Step 2: Rewrite String Concatenation

Right-click it - or choose Refactor > Refactor This… - and choose Localisation > Rewrite to Backtick Interpolation.

The EK9 IDE once this is done: Right-click it - or choose Refactor > Refactor This… - and choose Localisation > Rewrite to Backtick Interpolation

Step 3: Preview and apply

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

The result

That is Rewrite String Concatenation: "Hello " + name + "!" is Hello ${name}!, one allocation instead of three. The four- and five-part chains below it are the same refactoring - try one yourself. It is on the right-click menu wherever the caret is inside a + concatenation; AI assistants use the ek9_rewrite_string_concat tool.

The EK9 IDE when the lesson has finished

Where this lesson comes from