Skip to main content

UndeclaredVariable

rascal-0.34.0

Synopsis

Rascal can not find the definition of the name (identifier) used in an Expressions or Patterns.

Description

A variable can only be used when it has been declared and initialized. This error is generated when this is not the case.

The error message is a bit of a misnomer, because it is produced when any identifier can not be found. That could be a name of a Function, of a Constructors, or a Pattern Variable, or a global Variable or a local Variable which can not be found in the current scope.

You can help Rascal find the name by:

  • Adding a new declaration of a Function, Constructors, a Variable or a Pattern Variable that has the given name.
  • Import or Extend a module that contains a definition of the given name.
  • Using a different name, or correcting a spelling error, such that Rascal can find the name in the current scope, or an imported or extended scope.

Examples

Here is an example where an undeclared variables occurs in list splicing:

rascal>[1, *x, 3]
|prompt:///|(5,1,<1,5>,<1,6>): Undeclared variable: x
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/UndeclaredVariable|
ok

The remedy is here:

rascal>x = [5];
list[int]: [5]
rascal>[1, *x, 3]
list[int]: [1,5,3]

Benefits

  • Knowing this error statically means this can never happen at run-time if you do not get the error.
  • Variable references are never null in Rascal. In fact null is not a concept in Rascal at all.

Pitfalls

  • Not only hard-to-find variable names produce UndeclaredVariable, also missing Functions and Constructors do.