Global variable in a module not accessible after the module being imported

StackOverflow https://stackoverflow.com/questions/20118214

  •  03-08-2022
  •  | 
  •  

Pergunta

I found that in the Rascal console, after I import a module which contains global variable definitions, these global variables are not accessible. Only global function names are accessible. For example, in the following code, after I import Foo in the console, I can call x() to get 1, but dereference x gives me an unbound variable error. Why?

module Foo

int x = 1;

int x() = 1;
Foi útil?

Solução

Globals are default private in Rascal, to prevent you from easily doing such nastiness :-)

module Foo

public int x = 1;

int x() = 1;

This is a bit inconsistent, since the function is public by default but not when you consider that we will eventually remove globals from the language :-)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top