Home Notices Documents Classes Download Others Rice
Documents  >  Development  >  Dynamic embedded classes  >  Method
Implementation of method

Purpose of this page

This page details the fiveth part of the five part that divided the Rtemplate.cs.



In the part describes here, the _method() method is defined. This is an abstract method declared in the definition of Rtype, and must be implemented in a derived class of Rtype. That is, it must be implemented in an embedded class.

The _method() method is a method to define the behavior of the method which is the interface of the class. Method is parameterized interface that acts on instance. It corresponds the method of the C#.


The first section explains the method that is interface of the class.

What is important in the _metter() method is the method's arguments. We will make a section for each argument and explain.

Finally, we will have a section for implementation example.

What is method

A method is an interface that acts on an instance and takes zero or more parameters.

The behavior of the method is shown using the file class as an example.


file someFile = new file("c:\somewhere....");

file newFile = someFile.CopyTo("c:\anotherlocation...");


The CopyTo() is method. As shown in the example, it passes a path as a parameter to someFile's CopyTo() method and calls it. The someFile is copied to that path. Then, an instance of the file class representing the copied file is returned as a result of the method call. LIke this, an interface that takes parameters and acts on an instance is a method.

As a result of acting on the instance, the state of the calling instance may or may not change. The method may or may not return a value.


Explains the behavior of Rice when the method is called.

When a getter appears, Rice creates a name for calling the method from the method's name and arguments. The name is called method-signature.

In the example above, the method name is "CopyTo" and the class name of the argument is "string", so the "CopyTo(string)" creates as the method-signature. And the _method() method which is passed this method-signature and the argument is called for acting on the someFile.

The first argument : string signature

The created method-signature is passed to the _method() method as the first argument.

We will explain method-signature generation rules in order by increasing the number of arguments. Here, it assumes that the instance is the "someIns" and the method is the "SomeMethod".


No argument

someIns.SomeMethod()

This is when empty parentheses are specified after the method name. In this case, the method-signature is "SomeMethod()".


One argument. When taking one argument of string class.

someIns.SomeMethod("Argument string")

The method-signature is "SomeMethod(string)". If there is only one argument, the class name of the argument will be inserted between parentheses.


Two arguments. When taking arguments of string class and int class.

someIns.SomeMethod("Argument string", 10)

The method-signature is "SomeMethod(string,int)". If there are two arguments, The class names that are separated by commas will place between the parentheses in the order of appearance.


Three or more arguments.

It is the same as when two arguments. The class names that are separated by commas will place between the parentheses in the order of appearance.


Please be careful about the class names of container classes - list, dictionary, stack, queue -. These classes can specify a holded class.

For example, the class name of plain list is the "list", but if the string class is kept, the class name is list{string}. The same is true for other container classes.

Please note that this qualified class name appears in the method-signature.


SomeMethod(list)

SomeMethod(list{string})

The method-signature created in this way is passed to the _method() method as the first argument.

The second argiment : VirtualMachine vm

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

The purpose of this argument is to get an instance of the exception class. If you need to throw an exception, get one from VirtualMachine.

For example.


throw vm.GetRtypeException("Message of exception");


Since it is an exception thrown from the _method() method, it is appropriate to use the RtypeException class that is assuming class related exceptions. So we get that instance with GetRtypeException() and throw it.

This argument is useful if you want to give a detailed message about the exception. In addition, if you throw an exception from near the place where the exception occurs, additional information will be more accurate. The additional information is location information such as the source file name and the number of lines where the exception occurred. If you get an exception from the VirtualMachine class, that information will be automatically set to the exception.

Even if you throw a normal exception as C#, it will be caught immediately and converted to UnknownException class. The additional information is set automatically at this time.

The third argument : Queue<Rtype> argqueue

The arguments passed in method calling are stored in argqueue and passed to the _method() method.

We will explain the argqueue in order by increasing the number of arguments.


No argument

someIns.SomeMethod()

In this case, an empty Queue<Rtype> will be passed. Note that it is not null.


One argument. When taking one argument of string class.

someIns.SomeMethod("Argument string")

The method-signature is "SomeMethod(string)". Therefore, the argqueue holds an instance of Rstring class. In this case, its value is "Argument string".


Two arguments. When taking arguments of string class and int class.

someIns.SomeMethod("Argument string", 10)

The method-signature is "SomeMethod(string,int)". Therefore, the argqueue holds instances of Rstring class and Rint class. In this case, its value is "Argument string" and 10.

Like this, the argqueue holds arguments in the order of appearance.


Three or more arguments.

It is the same as when two arguments. The argqueue holds arguments in the order of appearance.

Example of Implementation

We can do the necessary processing using the fact that the method-signature is stored in the signature and the actual arguments are stored in the argqueue in the order of appearance.

The following is an implementation example of a method without arguments - MethodA() - and a method with one string argument - MethodB(string) -.

1:public override Rtype _method(string signature, VirtualMachine vm, Queue<Rtype> argqueue) {
2:switch (signature) {
3:case "MethodA()": {
4:// Implements necessary code here.
5:// It returns an instance of the Rvoid class. The Rvoid class indicates that there is no return value.
6:return new Rvoid();
7:}
8:case "MethodB(" + Rstring.TYPENAME + ")": {
9:string arg1 = (Rstring)argqueue.Dequeue();
10:// Implements necessary code here.
11:// It returns an instance of the Rstring class. That is, the return value of this method is string.
12:return new Rstring();
13:}
14:default:
15:return null;
16:}
17:}

In this example, the switch statement and the method-signature are used to branch. The value of each case can be created by combining the method name and the TYPENAME constant of classes. When there are two or more arguments, you can make a case value regardless of the number of arguments if you place "," between the argument's class names.

Actual arguments are stored in argqueue in the order of appearance as instances of the Rtype class which is the base class. That is, the order of the arguments is known, so if you take out from argqueue and cast it to an Rtype derived class, you can get the necessary arguments correctly.

In the above example, it has been implicitly converted to string after casting from Rtype to Rstring. Please refer to the manual or check the object browser of Visual Studio for the cast of the embedded class.

The implementation of the actual embedded class performs necessary processing after branching in the case. Please note that it returns a Rtype derived class when processing is complete. A Rtype derived class indicates that the method exists and processing is successful.

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

The "default:" returns null. Null indicates that class do not have a suitable method. An exception will be thrown if null is returned.

Implementation when do not need a method

A class does not need a method in some cases.

In such a case, implement the _method() method as follows.

1:public override Rtype _method(string signature, VirtualMachine vm, Queue<Rtype> argqueue) {
2:return null;
3:}

Next page

It concludes the description of the method.

The next page describes the dynamic registration of classes into applications.

Next
Previous
Copyright © CookerGX All rights reserved.