Home About Rice Downloads Documents Others Cooker CookerGX
Documents  >  Reference  >  variable
Variables in the Rice

Variables

The data that exist in memory is called as instance. The variables in the Rice is a name that given to the address of an instance. Namely, it is a reference.

Type

The type is a restriction to the data of the instance, and specification of behavior.

Declaration, definition, and assignment

Variables is carried out the restraint of the name and type by the declaration, and is initialized by definition, and correspondence of the name and address is changed by the assignment.

The declaration, difinition, and assignment of variables can carry out at any place in the fitter, setter, getter, and method definition. Declaration of variables at other place in the class definition is a field declaration. Field cannot carry out the declaration and definition at the same time.

The declaration, definition, and assignment of variables cannot carry out outside of the class definition, Namely, the global variables do not exist in the Rice.

Declaration

The statement that is followed a name after a type name is a declaration statement.

For example,


int i;


This is a declaration of a int type variable that is named i. In subsequent code, this variable can hold only the address of the int type instance.

A typename in a declaration statement is almost a simple type name.

For example,


int i;

string s;


However, list, dictionary, queue, and stack of collection types can specify a type name to hold inside.

For example,


list{int} listInt;

stack{string} stackString;


A collection type variable that is declared like above can not hold a element other than specified type.

A type of the elements to hold inside can be nested any number.

For example,


list{dictionary{long}} ldl;

stack{queue{list{bool}}} sqlb;


Definition

A statement that is followed the assignment symbol and expression after a declaration is a definition statement. Expression on the right side of the assignment symbol is necessary to return an instance of the appropriate type.

For example,


int i = 10;//The int-literal returns an instance of the int type. 。


The above is the definition of a variable of int type.

By int type specification, the int type is automatically initialized by declalation. Not necessarily definition is needed. Whether initialization is required depends on the individual type.

Assignment

Assignment to a variable is an address change of the reference.

For example,


i = 10;//It change the reference of the variable to the address of the instance that is returned by the int literal.

j = i;//A reference of the variable j changed to the reference of the variable i. Namely, the variable i and j are referring to the same instance.


Scope

Scope means the source code range that is able to use a variable.

The class, fitter, setter, getter, and method definitions generate scope to the code that are surrounded by them. The if, while, fromto, each, and try statements are also the same.

If scope surrounds other scope, the inside scope can see the outside scope, but the outside scope cannot see the variable that is internal to the inside scope.

For example,


class sample

bool _flag;

open method void sample_method()

bool method_flag = _flag; //♦1

if(true)

bool if_flag = method_flag; //♦2

endif

bool method_flag2 = if_flag; //♦3

endmethod

endclass


♦1 : Referable. _falg is declared in the scope created by the class definition. It is referable from the inside (the scope of the method definition).

♦2 : Referable. method_flag is declared in the scope created by the method definition. It is referable from the inside (the scope of the if statement).

♦3 : Error. if_flag is declared in the scope created by the if statement. It is unreferable from the outside (the scope of the method definition).

Hiding

If scope surrounds the other scope, and the variable of the same name has been declared at the both scope , the variable that is declared in the inside will be used Preferentially at the internal scope. Namely, the variable that is declared at the inside will hide the variable that have same name of the external.

For example,


class sample

open method void sample_method()

bool flag = true;

if(true)

bool flag = false; //♦1

bool flag2 = flag; //♦2

endif

endmethod

endclass


♦1 : Defines a variable of the same name.

♦2 : Inside flag is used. flag2 is false.


The external variable of the same name can be used until you declare the variable of the same name at the inside.


class sample

open method void sample_method()

bool flag = true;

if(true)

bool flag2 = flag; //♦1

bool flag = false; //♦2

flag2 = flag; //♦3

endif

endmethod

endclass


♦1 : Outside flag is used. flag2 is true.

♦2 : It is hidden from here.

♦3 : Inside flag is used. flag2 is false.


Since the definition statements are evaluated from the right side.


class sample

open method void sample_method()

bool flag = true;

if(true)

bool flag = flag; //♦1

endif

endmethod

endclass


♦1 : The inside flag was defined by the outside flag. It is hidden from here.


If the variable has been hidden, a special notation that used to refer to the hidden variable is not exist. Avoiding the variable hiding is the responsibility of the programmer.

Please refer to the "hiding of the identifier" about other hiding.

Reserved variable

The Rice have variables that will be automatically defined in a particular context. This is called a reserved variable. It is a same as normal variables except to be automatically defined.

The following is list of reserved variables


In the while statement.

__count

In the fromto statement.

__count

__index

In the each statement.

__count

__value

__key

In the try statement.

__error


Please note that these variable names are not the reserved word. Except the specific context, these variable names can be declared as a variable. However, it is not recommended.

For more information of the reserved variables, please refer to the manual of each statement.

Next
Previous
Copyright © Rice All rights reserved.