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

Category: E11xxx Code quality and IR analysis · Reported in phase: PRE_IR_CHECKS · Classification: PREFER_STRING_INTERPOLATION · All error codes

What went wrong

A string concatenation chain with 3 or more parts was detected. EK9 provides string interpolation (backtick syntax) which is both more readable and more efficient than concatenation with the + operator.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 309 (Why does string concatenation with + cause a compiler err...)
  • ek9 -q 174 (How do I join or concatenate strings in EK9?)
  • ek9 -q 313 (How does EK9 detect code smells?)

Rationale

Each + operator on strings calls the pure _add method, creating a new intermediate String object. A chain of N concatenations creates N-1 temporary objects. String interpolation compiles to a single bespoke STRING_INTERPOLATION IR instruction that backends optimise to one allocation:

JVM:  invokedynamic StringConcatFactory.makeConcatWithConstants()
LLVM: single allocation with pre-calculated length + memcpy

Beyond performance, interpolation is more readable. The template shows the output shape directly, while concatenation requires mentally assembling fragments separated by + operators.

For AI analysis, an interpolated string is one semantic unit. A concatenation chain is N tokens that must be mentally reconstructed into the intended output - expensive inference that interpolation eliminates.

How to fix it

Convert the concatenation chain to string interpolation: '"Hello " + name + "!"' (three parts, three allocations) becomes `Hello ${name}!` (one, with the output's shape visible), and '"(" + $x + ", " + $y + ")"' becomes `(${x}, ${y})`. A value inside ${...} is converted with its own '$' operator, so no '$' is needed there. The [EXAMPLE] section below shows chains of three, four and five parts, then the forms that are fine (an interpolation, a two-part join, a numeric sum).

See also

  • E11064: magic literal in comparison
  • E11065: repeated magic literal

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

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 + ")"

Fixed: Interpolate: one backtick String in place of the chain

#!ek9
defines module error.e11068.good.fix

  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})`

Fixed: Use string interpolation

#!ek9
defines module error.e11068.good

  defines function

    //Two-part concat is allowed (below threshold)
    twoPartConcat()
      -> name as String
      <- result as String: String()
      result: "Hello " + name

    //Interpolation is fine (obviously)
    interpolation()
      -> name as String
      <- result as String: `Hello ${name}!`

    //Numeric addition is fine (not String type)
    numericAddition()
      ->
        a as Integer
        b as Integer
        c as Integer
      <- result as Integer: 0
      result: a + b + c

    //String concat as just two variables
    twoVariables()
      ->
        first as String
        last as String
      <- result as String: String()
      result: first + last

Questions and answers

These Q&A entries show E11068 and its fix:

Found a problem?

If EK9 reported E11068 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.