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.


You can define a numeric class with a large range by a numeric class with a small range.


real rr = 10L; // Define real with long.

real rrr = 10; // Define real with int.

long ll = 10; // Define long with int.


In this case, the class on the left side is newly generated based on the right side and defined.

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.

You can assign a numeric class with a small range to a numeric class with a wide range.


real rr;

rr = 10L; // Assigns the long value to the real.

rr = 10; // Assigns the int value to the real.

long ll;

ll = 10; // Assigns the int value to the long.


In this case, a new instance of the lvalue class is created based on the instance returned by the expression, and it is assigned.

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

Rice have variables that will be automatically defined by the system. These are called reserved variables. It is a same as normal variables except to be automatically defined.

The following is list of reserved variables


while statement

__count

__index

fromto statement

__count

__index

each statement

__count

__index

__key

__value

try statement

__error

keepon statement

__count

__index


These variable names are not reserved words. For details of reserved variables, refer to each manual.

Copyright © Rice All rights reserved.