E50050: Variable/Constant duplicated

Category: E50xxx Common errors · Reported in phase: SYMBOL_DEFINITION · Classification: DUPLICATE_VARIABLE · All error codes

What went wrong

A variable with this name is already declared in the current scope. Each variable name must be unique within its scope.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 79 (What is the assign-if-unset operator :=? in EK9?)

Operator distinction

  • '<-' (declaration): creates NEW variable with inferred type
  • ':=' (assignment): assigns to EXISTING variable
  • ':=?' (guarded): assigns only if UNSET

How to fix it

  1. If updating existing variable: use ':=' instead of '<-'
  2. If creating new variable: use a different name
  3. Check for typos that accidentally match existing variable

Common causes

  • Using '<-' when ':=' was intended (update vs create)
  • Copy-paste error duplicating declaration
  • Variable name collision with earlier declaration

See also

  • E50050: duplicate variable in scope (this error)
  • E02040: duplicate type/symbol definition

Examples

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

Fails with E50050: Variable/Constant duplicated

#!ek9
defines module error.e50050.bad

  defines function

    demo()
      <- rtn as Integer?
      total <- 42
      total <- 100
      rtn: total

Fixed: Assign the existing variable with :=

#!ek9
defines module error.e50050.good

  defines function

    demo()
      <- rtn as Integer?
      total <- 42
      total := total + 100
      rtn: total

Fixed: Or give the second value its own name

#!ek9
defines module error.e50050.good.new.name

  defines function

    demo()
      <- rtn as Integer?
      first <- 42
      second <- 100
      rtn: first + second

Questions and answers

These Q&A entries show E50050 and its fix: