E06230: variables being captured must be named when not just using identifiers

Category: E06xxx Type hierarchy checks · Reported in phase: SYMBOL_DEFINITION · Classification: CAPTURED_VARIABLE_MUST_BE_NAMED · All error codes

What went wrong

When capturing values in dynamic functions/classes, expressions, function calls, and literals MUST have a name. Only simple variable identifiers can be captured without naming.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 115 (How do dynamic classes work in EK9?)

How to fix it

  1. Add name before colon: (name: expression)
  2. Or assign expression to variable first, then capture variable

Rationale

The captured value needs a name so it can be referenced in the function body. Simple variables already have names; expressions/literals don't.

Common causes

  • Capturing function call result without naming it
  • Capturing a literal value without naming it
  • Forgetting that expressions aren't auto-named

See also

  • E06230: unnamed capture of expression/literal (this error)
  • E06240: mixed named and unnamed parameters

Examples

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

Fails with E06230: variables being captured must be named when not just using identifiers

#!ek9
defines module error.e06230.bad

  defines function

    threshold() as pure
      <- rtn as Integer: 42

    discriminator() as abstract
      -> text as String
      <- rtn as Boolean?

  defines program

    UnnamedCaptures()
      //A capture that is not just a variable needs a name the body can use
      longer <- (threshold()) is discriminator as function
        rtn: length text > threshold

      shorter <- (100) is discriminator as function
        rtn: length text < max

Fixed: Name each captured expression: (min: threshold()), (max: 100)

#!ek9
defines module error.e06230.good

  defines function

    threshold() as pure
      <- rtn as Integer: 42

    discriminator() as abstract
      -> text as String
      <- rtn as Boolean?

  defines program

    NamedCaptures()
      stdout <- Stdout()

      longer <- (min: threshold()) is discriminator as function
        rtn: length text > min

      shorter <- (max: 100) is discriminator as function
        rtn: length text < max

      stdout.println(`${longer("a sentence")} ${shorter("a sentence")}`)

Questions and answers

These Q&A entries show E06230 and its fix: