Setter

Purpose of this page

The part described on this page is shown below. This is the third part of the template divided into five parts.



The _setter() method defines the behavior of setter which is an interface of the Rice class. Setter is an interface for changing the internal state of an instance.

This is an abstract method declared in the Rtype class definition and must be implemented in an Rtype-derived class.


We explain how to define a setter by defining "Value" setter in the fake class.


Table of contents:


What is setter

First argument: string signature

Second argument: VirtualMachine vm

Third argument: Rtype arg

Fixed code

Overload

When do not need a setter

What is setter

A setter is an interface for changing the internal state of an instance. Its behavior would be:


fake fakeInstance = new fake();

fakeInstance.Value = "Assigned string";


The "Value" is the setter. You can assign a value to the setter that appears to the left of the assignment operator. The assignment changes the internal data of the fakeInstance to "Assigned string".


In Rice, elements that appear on the left side of the assignment operator and can be assigned a value are fields, variables, and setters.

Assignment to fields and variables is a change of the instance itself. Only setters can change the internal state of an instance.

First argument: string signature

When a setter appears on the left side of an assignment statement, Rice evaluates the expression on the right side of the assignment statement to determine the assignment value. Then, creates a setter-signature which is a name in order to call a setter.

The setter-signature is passed as the first argument of the _setter() method.


Second argument: VirtualMachine vm

An instance of the VirtualMachine class is passed as the second argument. This is the virtual machine running the script.

If you need to throw an exception, get it from this argument. You can get an exception in which information such as the source file name and the number of lines where the exception occurred is set.

throw vm.GetRtypeException("Exception message", "Exception name"); // Exception with supplementary information.

Third argument: Rtype arg

A result of evaluating the right-hand side of the assignment statement is stored in the third argument and passed to the _setter() method.

Fixed code

We modify the _setter() method for the fake class.

1:

public override bool _setter(string signature, VirtualMachine vm, Rtype arg) {

2:

switch (signature) {

3:

case "Value(" + Rstring.TYPENAME + ")": { // "Value(string)"

4:

InternalData = (Rstring)arg;

5:

return true;

6:

}

7:

case "Value(" + TYPENAME + ")": { // "Value(fake)"

8:

fake f = (Rfake)arg;

9:

InternalData = f.InternalData;

10:

return true;

11:

}

12:

default:

13:

return false;

14:

}

15:

}

The switch statement is used to branch to the corresponding routine. The value of each case can be created by combining the TYPENAME constant of classes.


The "Value" setter is defined in the 3rd to 6th lines. It modifies the internal data by assigning a string.

As mentioned in the fitter description, the Rstring class can be implicitly cast to a C# string. Therefore, you can directly assign the argument of the Rstring class to InternalData.


The "Value" setter is defined in the 7th to 11th lines. This Value setter modifies the internal data by assigning the internal data of a fake class.

We can access the private members of the Rfake class since it is in the definition of the Rfake class. So we are assigning InternalData directly to InternalData.


Both setters retrieve the assignment value from the third argument. The setter-signature and the third argument absolutely match. Therefore, the retrieved argument can be safely cast.


Please notice that each setter routine returns true and exits. True indicates that the setter corresponding to the first argument exists and the processing was successful.

Returns false at "default:". False indicates a processing failure due to the absence of a corresponding setter. Rice throws an exception if false is returned.

Overload

Please notice that setter names in the above code are the same.

Setter names are the same, but the setter-signatures are different. That is, you can call the appropriate routine with the same setter name.

Rice allows you to define setters with the same name if the assigned values are different. This is called setter overload.

When do not need a setter

If a class does not need a setter, implement the _setter() method as follows.

1:

public override bool _setter(string signature, VirtualMachine vm, Rtype arg) {

2:

return false;

3:

}

Copyright © Cooker All rights reserved.