Skip to content
ricardoboss edited this page Oct 19, 2023 · 1 revision

Variable Already Declared Exception

Description

This exception is thrown when a variable is declared more than once in the same scope.

Example:

number a = 1

number a = 2

Remediation

Make sure that each variable is only declared once in each scope or give each variable a different name:

  number a = 1
- number a = 2
+ number b = 2

You can also overwrite the value of a variable by leaving out the type:

  number a = 1
- number a = 2
+ a = 2

Tip

You can use anonymous code blocks to separate different scopes.

This is valid:

number a = 1

println(a) // prints 1

{
  number a = 2

  println(a) // prints 2
}

println(a) // prints 1