Method

Purpose of this page

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



The _method() method defines the behavior of method which is an interface of the Rice class. A method is an interface that takes parameters and acts on the 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 methods by defining following methods in the fake class.

Concatenate(string): Concatenates internal data and an argument.

Concatenate(fake): Concatenates internal data and internal data of an argument.

Release(): Returns an instance to uninitialized state.


Table of contents:


What is method

First argument: string signature

Second argument: VirtualMachine vm

Third argument: Queue<Rtype> argqueue

Fixed code

Overload

When do not need a method

What is method

A method is an interface that acts on an instance and takes zero or more parameters. Returns a value as a result of the action. Its behavior would be:


fake fakeInstance = new fake("initial data");

string concatData = fakeInstance.Concatenate("+additional data"); // Return value is "initial data+additional data".

string newInternalData = fakeInstance.Value; // Internal data is also "initial data+additional data".


The "Concatenate()" is the method. You can get a return value and changes internal data on an instance.

First argument: string signature

When a method appears, Rice creates a method-signature which is a name in order to call a method.

The method-signature is passed as the first argument of the _method() 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: Queue<Rtype> argqueue

Rice parses a method, stores its arguments in the third argument and passes it to the _method() method.

The third argument contains instances of Rtype-derived class corresponding to the arguments in order of appearance.

Fixed code

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

1:

public override Rtype _method(string signature, VirtualMachine vm, Queue<Rtype> argqueue) {

2:

switch (signature) {

3:

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

4:

InternalData += (Rstring)argqueue.Dequeue(); // Updateds internal data.

5:

return new Rfake(InternalData); // Returns internal data.

6:

}

7:

case "Concatenate(" + Rfake.TYPENAME + ")": { // "Concatenate(fake)"

8:

fake f = (Rfake)argqueue.Dequeue();

9:

InternalData += f.InternalData; // Updateds internal data.

10:

return new Rfake(InternalData); // Returns internal data.

11:

}

12:

case "Release()": { // "Release()"

13:

InternalData = null; // Uninitialized state.

14:

return new Rvoid(); // No return value.

15:

}

16:

default:

17:

return null;

18:

}

19:

}

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 "Concatenate(string)" method is defined in the 3rd to 6th lines.

On line 4, the argument passed to the method is retrieved from the third argument. The class and number of instances stored in the third argument absolutely match to the method-signature. Therefore, you can safely cast the retrieved argument to Rstring class.

As mentioned in the fitter description, the Rstring class can be implicitly cast to a C# string. Therefore, you can concatenate and assign these.

On the 5th line, the result of concatenating is returned as a new instance of the Rfake class.


The "Concatenate(fake)" method is defined in the 7rd to 11th lines.

On line 8, the argument passed to the method is retrieved from the third argument. The class and number of instances stored in the third argument absolutely match to the method-signature. Therefore, you can safely cast the retrieved argument to Rstring class.

We can access the private members of the Rfake class since it is in the definition of the Rfake class. Therefore, you can concatenate and assign the InternalData and the InternalData of the f.

On the 10th line, the result of concatenating is returned as a new instance of the Rfake class.


The "Release()" method is defined in the 12rd to 14th lines.

On the 13th line, null is assigned to the internal data to put the instance in the uninitialized state.

The Release() method does not need to return a value, so it returns an instance of the Rvoid class.


If an instance of the Rtype-derived class returns from a method routine, it indicates that the method corresponding to the first argument exists and the processing was successful.

If the method has no return value, return an instance of the Rvoid class.

Returns a null at "default:". The null indicates a processing failure due to the absence of a corresponding method. Rice throws an exception if null is returned.

Overload

Please notice that methods with the same name are defined in the above code.

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

Rice allows you to define methods with the same name if the arguments are different. This is called method overload.

When do not need a method

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

1:

public override Rtype _method(string signature, VirtualMachine vm, Queue<Rtype> argqueue) {

2:

return null;

3:

}

Copyright © Cooker All rights reserved.