0

I have a situation where I defined a couple of module-level instance variables using the 'private' scope identifier. I require to do this because these variables will be used in several functions within the module. Also, some of these variables are 'lists' or 'sets'. I realized that values of these variables persist between repeated calls to a certain function within the module. This is as expected.

I am also creating a test where I call one of the functions repeatedly. I would prefer to have a fresh copy of the instance variables (just like with instance members in Java). I can't seem to do so. If I try to nullify the content of the list/set, I get into trouble as follows:

module foo::bar

private set[DataType_1] data1;

public void nullifyInstanceVars( )
{
    //tried  
    data1={}

} 

//method that gets called repeatedly:
public void repeatCallMe(..)
{
    nullifyInstanceVars( );
    ...
    ..
    //Throws an error like: trying to add an element of type 1 to set[void]
    data1 +=  anElementOfType1 

}

So, I modified the nullifyInstanceVars( ) method to have set[DataType1] data1={ }. It doesn't work because I believe that simply creates a new variable scoped only within the function and really doesn't clear the element!

Any help is appreciated...

apil.tamang
  • 2,545
  • 7
  • 29
  • 40

1 Answers1

1

This really looks like a bug in the Rascal interpreter. I will file a bug report for it.

The work around is to initialize data1 in the declaration as well:

private set[int] data1 = {};

Can you confirm that this solves your problem?

Paul Klint
  • 1,418
  • 7
  • 12
  • Ahhh.. well, I found a work around, and besides my script has become more entangled than I'm comfortable unraveling it to that state. In the meantime, I realized that there were other instance variables where I explicitly initialize as you recommend; and they never reported any errors. Thus, it must be the case as you suggested. Thanks... – apil.tamang Nov 26 '14 at 22:22
  • Also, try instead saying data1 = data1 + anElementOfType1, I've noticed this before as well but it's a bit unpredictable as to when it will happen. – Mark Hills Nov 26 '14 at 22:25