Home News DOC Class DL Others Cooker
Documents  >  Reference  >  Variable
Variables in the Rice

Variables

A group of data that represents the state of a certain class that exists in the memory area is called an instance. The data that specifies the location of the instance in memory is called an address.

A variable in Rice is the name given to the memory area that holds the address of the instance. That is, 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.

The int type is automatically initialized by declalation. Whether initialization is required depends on the individual type.

Assignment

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

For example,


i = 10; // It change the reference of the variable i 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, keepon, each, and try statements are also the same.

If scope surrounds other scope, the inside scope can see variables in the outside scope, but the outside scope cannot see variables in 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 Error.

endmethod

endclass


♦1: The _falg field is declared in the scope created by the class definition. It is referable from the inside.

♦2: The method_flag is declared in the scope created by the method definition. It is referable from the inside.

♦3: The if_flag is declared in the scope created by the if statement. It is unreferable from the outside.

Hiding

If a scope surrounds another 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


while statement

__count

fromto statement

__count

__index

each statement

__count

__value

__key

try statement

__error

keepon statement

__index


These variable names are not the reserved word. For more information of the reserved variables, please refer to the manual of each statements.

Next
Previous
Copyright © Rice All rights reserved.