Name
SPVM::Document::Language::Class - Class Definition in SPVM Language
Description
This document describes class definition in SPVM language.
Class
A class defines its class type, its class variables, its fields and its methods.
The object can be created from a class using new operator.
Class Definition
The class
keyword defines a class. A class has a class block.
# Class definition
class CLASS_NAME {
}
The class name must follow the naming rule of the class name.
Examples:
# Class definition
class Point {
}
Class attributes are written after :
.
class CLASS_NAME : CLASS_ATTRIBUTE {
}
class CLASS_NAME : CLASS_ATTRIBUTE1 CLASS_ATTRIBUTE2 CLASS_ATTRIBUTE3 {
}
Compilation Errors:
If more than one class is defined in a class file, a compilation error occurs.
Examples:
# Class attributes
class Point : public {
}
class Point : public pointer {
}
Version Declaration
The version
keyword declares the version string of a module.
version VERSION_STRING;
The operand VERSION_STRING is a version string.
A version string is the string type.
It is composed of numbers 0-9
, .
.
The following checks are performed.
The version string is saved to the version information of the module.
Compilation Errors:
If the version string has already been declared, a compilation error occurs.
A character in a version string must be a number or .
. Otherwise a compilation error occurs.
The number of .
in a version string must be less than or equal to 1. Otherwise a compilation error occurs.
A version string must begin with a number. Otherwise a compilation error occurs.
A version string must end with a number. Otherwise a compilation error occurs.
The number of .
in a version string must be less than or equal to 1. Otherwise a compilation error occurs.
The length of characters after .
in a version string must be divisible by 3. Otherwise a compilation error occurs.
A version number must be able to be parsed by the strtod
C function. Otherwise a compilation error occurs.
Examples:
class Foo {
version "1";
}
class Foo {
version "20080903";
}
class Foo {
version "1.001";
}
class Foo {
version "10.001003";
}
Version String
The version string is the string represented version of a module.
It is declared by the version declaration.
Version Number
The version number is a floating point number created by the following way.
A "Version Declaration" in version string is converted to a floating point number by the strtod
C function.
Class Attribute
The list of class attributes.
Class attributes | Descriptions |
---|---|
public | This class is public. In other classes, this class can be used as I of new operator. |
private | This class is private. In other classes, this class cannot be used as I of new operator. This is default. |
protected | This class is protected. In other classes except for the child classes, this class cannot be used as I of new operator. |
interface_t | This class is an interface type. The class definition is interpreted as an interface definiton. |
mulnum_t | This class is a multi-numeric type. The class definition is interpreted as an multi-numeric type definiton. |
pointer | The class is a pointer class. |
precompile | Perform precompile to all methods in this class, except for getter methods, setter methods, and enumurations. |
Compilation Errors:
Only one of class attributes private
, protected
or public
can be specified. Otherwise a compilation error occurs.
If more than one of interface_t
, mulnum_t
, and pointer
are specified, a compilation error occurs.
Destructor
A class can have a destructor.
method DESTROY : void () {
}
The destructor is the method that is called when the object is destroyed by the garbage collection.
The name of the destructor must be DESTROY
.
A destructor cannnot have its arguments.
The retrun type must be void type.
A destructor must be an instance method.
If an exception occurs in the destructor, the exception is not thrown. Instead, a warnings message is printed to STDERR
.
Compilation Errors:
If the definition of the destructor is invalid, a compilation error occurs.
Examples:
# Destructor
class Foo {
method DESTROY : void () {
print "DESTROY";
}
}
The child class inherits the destructor of the parent class if the destructor of the current class doesn't eixst.
allow Statement
Private methods, private fields, and private class variables cannot be accessed except from the current class.
A private class cannot be OPERAND of the new operator except from the current class.
The allow
statemenet allows the private access from the other classes.
allow CLASS_NAME;
The allow
statemenet must be defined directory under the class definition.
The class that is OPERAND of the allow
statemenet is loaded by the same way as the use statement.
Examples:
# Allowing private access
class Foo {
allow Bar;
}
interface Statement
The interface
statement guarantees the following things.
interface INTERFACE_NAME;
1. If the class has methods that are definied the the interface, each method must have the Method Compatibility of each interface method in the interface definition.
2. The class must have methods that defined as required interface methods in the the interface.
Compilation Errors:
If not, a compilation error occurs.
Examples:
class Point {
interface Stringable;
method to_string : string () {
my $x = $self->x;
my $y = $self->y;
my $string = "($x,$y)";
return $string;
}
}
my $stringable = (Stringable)Point->new(1, 2);
my $string = $stringable->to_string;
Method Compatibility
(TODO)
Anon Class
The anon class is the class that do not has its class name.
class {
}
But internally an anon class has its name, such as eval::anon::0
.
An anon class is also defined by the anon method.
A anon class for an anon method has its unique class name corresponding to the class name, the line number and the position of columns the anon class is defined.
A anon class for an anon method has the same access control as its outer class.
A anon class for an anon method has the same alias names as its outer class.
# Anon class
class {
static method sum : int ($num1 : int, $num2 : int) {
return $num1 + $num2;
}
}
# Anon method
class Foo::Bar {
method sum : void () {
my $anon_method = method : string () {
}
}
}
# The name of the anon class
Foo::Bar::anon::3::23;
Pointer Class
The pointer class is the class that has the class attribute pointer
.
# Pointer Class
class Foo : pointer {
}
The type of a pointer class is the class type.
A object of a pointer class has the pointer to a native address.
Inheritance
A class inherits a class using the extends
keyword.
class CLASS_NAME extends PARENT_CLASS_NAME {
}
The parts of the definitions of the fields of the all super classes are copied to the class.
The copied parts of the definitions are the field name, the type, the access controll.
The the definitions of the interfaces of the all super classes are copied to the class.
The copied order is from the beginning of the super class at the top level to the current class.
The class can call instance methods of the super classes. The searching order is from the current class to the super class at the top level.
Compilation Errors:
The parant class must be a class type. Otherwise a compilation error occurs.
The name of the parant class must be different from the name of the class. Otherwise a compilation error occurs.
The all super classes must be different from its own class. Otherwise a compilation error occurs.
The field that name is the same as the field of the super class cannnot be defined. Otherwise a compilation error occurs.
Examples:
class Point3D extends Point {
has z : rw protected int;
static method new : Point3D ($x : int = 0, $y : int = 0, $z : int = 0) {
my $self = new Point3D;
$self->{x} = $x;
$self->{y} = $y;
$self->{z} = $z;
return $self;
}
method clear : void () {
$self->SUPER::clear;
$self->{z} = 0;
}
method to_string : string () {
my $x = $self->x;
my $y = $self->y;
my $z = $self->z;
my $string = "($x,$y,$z)";
return $string;
}
method clone : object () {
my $self_clone = Point3D->new($self->x, $self->y, $self->z);
return $self_clone;
}
}
Interface
The interface syntax is described.
Interface Definition
An interface is defined using a class definition with a "Class Attribute" in class attribute interface_t
.
class Stringable: interface_t {
required method to_string : string ();
method foo : int ($num : long);
}
An interface can have interface methods. An interface method does not need its method block.
An interface can have required interface methods by using the method attribute required
.
The type of the interface is the interface type.
An interface can have interface statements.
class TestCase::Pointable : interface_t {
interface Stringable;
method x : int ();
method y : int();
method to_string : string ();
}
An interface method can have its method block.
class Stringable: interface_t {
method to_string : string ();
method call_to_string : string () {
return "foo " . $self->to_string;
}
}
This method is only called by the static instance method call.
$self->Stringable::call_to_string;
Compilation Errors:
An interface cannnot have field definitions. If so, an compilation error occurs.
An interface cannnot have class variable definitions. If so, an compilation error occurs.
Duck Typing
The duck typing is supported.
class Stringable: interface_t {
method to_string : string ();
}
class Point {
method to_string : string () {
my $x = $self->x;
my $y = $self->y;
my $string = "($x,$y)";
return $string;
}
}
my $stringable = (Stringable)Point->new(1, 2);
my $string = $stringable->to_string;
The Point class have no interfaces, but A object of the Point class can be assigned to a Stringable interface because the to_string method in the Point class has the method compatibility of the to_string method in the Strigable interface.
Class File Name
A class must be written in the following class file.
Change ::
to /
. Add ".spvm" at the end.
SPVM/Foo.spvm
SPVM/Foo/Bar.spvm
SPVM/Foo/Bar/Baz.spvm
use Statement
The use
statemenet loads a class.
use Foo;
Classes are loaded at compile-time.
The use
statemenet must be defined directly under the class definition.
class Foo {
use Foo;
}
Compilation Errors:
If the class does not exist, a compilation error occurs.
alias Statement
The alias
statemenet creates an alias name for a class name.
# Create alias
alias Foo::Bar as FB;
FB is used as Foo::Bar alias in class method calls.
# This means Foo::Bar->sum(1, 2);
FB->sum(1, 2);
alias
syntax must be defined directly under the class definition.
class Foo {
alias Foo::Bar as FB;
}
You can create an alias at the same time as loading a class by the use
statement.
use Foo::Bar as FB;
require Statement
If the require
statement that loads a class only if it exists in the class path, and if it does not exist, the block does not exist.
It was designed to implement a part of features of "#ifdef" in the C language.
if (require Foo) {
}
if require Statement can be followed by else Statement.
if (require Foo) {
}
else {
}
Note that elsif Statement cannot be followed.
Let's look at an example. if Foo does not exist, no a compilation error occurs and it is assumed that there is no if block
Therefore, "$foo = new Foo;" does not result in a compilation error because it is assumed that there is no if block.
In the other hand, the else block exists, so a warning is issued.
my $foo : object;
if (require Foo) {
$foo = new Foo;
}
else {
warn "Warning: Can't load Foo";
}
Default Loaded Classes
The following classes are loaded by default. These classes are deeply related to the features of SPVM language itself, such as type conversion.
Class Variable
A class variable is a global variable that has the name space.
Class Variable Definition
our
keyword defines a class variable.
our CLASS_VARIABLE_NAME : TYPE;
A Class variable must be defined directly under the class definition.
The type must be a numeric type or an object type.
Class variable attributes can be specified.
our CLASS_VARIABLE_NAME : ATTRIBUTE TYPE;
our CLASS_VARIABLE_NAME : ATTRIBUTE1 ATTRIBUTE2 ATTRIBUTE3 TYPE;
Compilation Errors:
The class variable mame must follow the rule defined in the class variable name, and must not contain ::
. Otherwise a compilation error occurs.
If a class name with the same name is defined, a compilation error occurs.
Examples:
class Foo {
our $NUM1 : byte;
our $NUM2 : short;
our $NUM3 : int;
our $NUM4 : long;
our $NUM5 : float;
our $NUM6 : double;
our $NUM_PUBLIC : public int;
our $NUM_RO : ro int;
our $NUM_WO : wo int;
our $NUM_RW : rw int;
}
Class Variable Attribute
The list of class variable attributes.
Class Variable Attributes | Descriptions |
---|---|
public | The class variable is public. The class variable can be accessed from other classes. |
private | The class variable is private. The class variable cannnot be accessed from other classes. This is default setting. |
protected | The class variable is protected. The class variable cannnot be accessed from other classes except for the child classes. |
ro | The class variable has its getter method. |
wo | The class variable has its setter method. |
rw | The class variable has its getter method and setter method. |
Compilation Errors:
Only one of class variable attributes private
, protected
or public
can be specified. Otherwise a compilation error occurs.
If more than one of ro
, wo
, and rw
are specified, a compilation error occurs
Class Variable Accessor
A class variable method is a method that gets and sets a class variable.
Class Variable Getter Method
A class variable getter method is a method to perform the getting class variable.
It has no arguments. The return type is the same as the type of the class variable except that the type of the field is the byte type or the short type.
If the type of the class variable is the byte type or the short type, the return type is the int type.
It is defined by the ro
or rw
class variable attributes.
It is a method that name is the same as the class variable name removing $
. For example, if the class variable name is $FOO, its getter method name is FOO
.
Examples:
# Class variable getter method
class Foo {
our $NUM : ro int;
static method main : void {
my $num = Foo->NUM;
}
}
Class Variable Setter Method
A class variable setter method is a method to perform the setting class variable.
The return type is the void type.
It has an argument that type is the same as the type of the class variableexcept that the type of the field is the byte type or the short type.
If the type of the class variable is the byte type or the short type, the argument type is the int type.
It is defined by the wo
or rw
class variable attributes.
It is a method that name is the same as the class variable name removing $
and adding SET_
to the beginning. For example, if the class variable name is $FOO, its setter method name is SET_FOO
.
Examples:
# Class variable setter method
class Foo {
our $NUM : wo int;
static method main : void {
Foo->SET_NUM(3);
}
}
Class Variable Initial Value
Each class variable is initialized with the "Initial Value" in initial value just after the program starts.
This initial value can be changed by using the INIT block.
# Change the initial value of the class variable using INIT block.
class Foo {
our $VAR : int;
INIT {
$VAR = 3;
}
}
Class Variable Access
The class variable access is an operator to set or get a class variable.
See the getting class varialbe and the setting class varialbe.
Field
Fields are the data that an object has.
Field Definition
The has
keyword defines a field.
# The field definition
has FIELD_NAME : OPT_ATTRIBUTES TYPE;
# An examples
has name : string;
has age : protected int;
has max : protected rw int
The field is defined directly under the class block.
class MyClass {
has name : string;
}
Field attributes can be specified.
Compilation Errors:
The field definition needs the type. The type must be a numeric type or an object type. Otherwise a compilation error occurs.
The field names must follows the rule of the field name. Otherwise a compilation error occurs.
Field names cannot be duplicated. If so, a compilation error occurs.
Field Attribute
The list of field attributes.
Attributes | Descriptions |
---|---|
private | This field is private. This field cannnot be accessed from other class. This is default setting. |
protected | This field is protected. This field cannnot be accessed from other class except for the child classes. |
public | This field is public. This field can be accessed from other class. |
ro |
This field has its getter method. The getter method name is the same as the field name. For example, If the field names is foo , The getter method name is C.
|
wo |
This field has its setter method. The setter method name is the same as field names adding set_ to top. For example, If the field names is foo , The setter method name is set_foo .
|
rw | This field has its getter method and its setter method. |
A field getter method is an instance method. It has no arguments. The return type of a field getter method is the same as its field type, except for the byte
and short
type.
If the type of the field is the byte
or short
type, The return type of a field getter method is the int
type.
A field setter method is an instance method. It has an argument. The type of the argument is the same as the field type. The return type is the void type.
If the type of the field is the byte
or short
type, The argument type of a field setter method is the int
type.
Compilation Errors:
Only one of field attributes private
, protected
or public
can be specified. Otherwise a compilation error occurs.
If more than one of ro
, wo
, and rw
are specified at the same time, a compilation error occurs
Examples:
class Foo {
has num1 : byte;
has num2 : short;
has num3 : int;
has num4 : long;
has num5 : float;
has num6 : double;
has num_public : public int;
has num_ro : ro int;
has num_wo : wo int;
has num_rw : rw int;
}
Field Access
The field access is an operator to get or set the field.
INVOCANT->{FIELD_NAME}
The field access has three different syntax.
Compilation Errors:
If the invocant is different from the following three field access, a compilation error occurs.
If the field name does not found, a compilation error occurs
Field Access of the class
The field access of the class.
my $point = new Point;
$point->{x} = 1;
my $x = $point->{x};
See "Getting Field" to get the field of the class.
See "Setting Field" to set the field of the class.
Field Access of thethe multi-numeric type
The field access of the multi-numeric type.
my $z : Complex_2d;
$z->{re} = 1;
my $re = $z->{re};
See "Getting Multi-Numeric Field" to get the field of the multi-numeric type.
See "Setting Multi-Numeric Field" to set the field of multi-numeric type.
Field Access of the Multi-Numeric Reference via Derefernce
The field access of the multi-numeric reference via derefernce.
my $z : Complex_2d;
my $z_ref = \$z;
$z_ref->{re} = 1;
my $re = $z_ref->{re};
See "Getting Multi-Numeric Field via Dereference" to get the field of the multi-numeric reference via dereference.
See "Setting Multi-Numeric Field via Dereference" to set the field of the multi-numeric reference via dereference.
Method
a.
Method Definition
The method
keyword defines a class method or an instance method.
# Static method
static method METHOD_NAME : RETURN_TYPE (ARG_NAME1 : ARG_TYPE1, ARG_NAME2 : ARG_TYPE2, ...) {
}
# Instance method
method METHOD_NAME : RETURN_TYPE (ARG_NAME1 : ARG_TYPE1, ARG_NAME2 : ARG_TYPE2, ...) {
}
Methods must be defined directly under the class definition.
Method names must be follow the rule of "Method Name".
The argument names must be follow the rule of "Local Variable Name".
The minimal length of arguments is 0. The max length of arguments is 255.
Defined methods can be called using "Method Call" syntax.
A method can have method attributes.
ATTRIBUTES static method METHOD_NAME : RETURN_TYPE (ARG_NAME1 : ARG_TYPE1, ARG_NAME2 : ARG_TYPE2, ...) {
}
A method has "Method Block" except for the case that the method has the native
method attributes.
Compilation Errors:
The types of the arguments must be a numeric type, the multi-numeric type, an object type, or a reference type. Otherwise a compilation error occurs.
The type of the return value must be the void type, a numeric type, the multi-numeric type or an object type. Otherwise a compilation error occurs.
Optional Argument
The optional argument is the syntax to specify optional arguments.
static method METHOD_NAME : RETURN_TYPE (ARG_NAME1 : ARG_TYPE1, ARG_NAME2 : ARG_TYPE2 = DEFAULT_VALUE) {
}
# Deprecated
static method METHOD_NAME : RETURN_TYPE (ARG_NAME1 : ARG_TYPE1, ARG_NAME2 = DEFAULT_VALUE : ARG_TYPE2) {
}
Examples:
static method substr ($string : string, $offset : int, $length : int = -1) {
# ...
}
my $string = "abc";
my $offset = 1;
my $substr = &substr($string, $offset);
# This is the same as the following code
my $string = "abc";
my $offset = 1;
my $length = -1;
my $substr = &substr($string, $offset, $length);
Class Method
A class method is defined with the static
keyword.
static method sum : int ($num1 : int, $num2 : int) {
# ...
}
A class method can be called from the class name.
# Call a class method
my $total = Foo->sum(1, 2);
If the class method is belong to the current class, a class method can be called using & syntax.
# Call a class method using C<&>
my $total = &sum(1, 2);
Instance Method
An instance method is defined without the static
keyword.
method add_chunk : void ($chunk : string) {
# ...
}
An instance method can be called from the object.
# Call an instance method
my $point = Point->new;
$point->set_x(3);
Method Attributes
Method attributes are attributes used in a method definition.
Attributes | Descriptions |
---|---|
private | This method is private. This method can not be accessed from other classes. |
protected | This method is protected. This method can not be accessed from other classes except for the child classes. |
public | This method is public. This method can be accessed from other classes. This is default setting. |
precompile | This method is a precompile method. |
native | This method is a native method. |
required | This method is required. |
Native Method
A native method is the method that is written by native languages such as the C language, C++
.
A native method is defined by the native
method attribute.
native sum : int ($num1 : int, $num2 : int);
A native method doesn't have its method block.
About the way to write native methods, please see SPVM Native Class and SPVM Native API.
Precompiled Method
If the class has the precompile
class attribute, the methods of the class are precompiled.
The source code of each precompiled method is translated to C source code and is compiled to the machine code such as MyMath.o
.
And it is linked to a shared library such as MyMath.so
on Linux/Unix, MyMath.dll
on Windows, or MyMath.dylib
on Mac.
And each function in the shared library is bind to the SPVM method.
Precompiled methods need the build directory such as ~/.spvm_build
to compile and link them.
Enumeration
The enumeration is the syntx to defines constant values of the int type.
Enumeration Definition
The enum
keyword defines an enumeration. An enumeration has definitions of constant values.
# Enumeration Definition
enum {
FLAG1,
FLAG2,
FLAG3
}
An enumeration must be defined directly under the class definition.
class Foo {
enum {
FLAG1,
FLAG2,
FLAG3
}
}
The name given to an enumeration value must be a method name.
The first enumeration value is 0. The next enumeration value is incremented by 1, and this is continued in the same way.
In the above example, FLAG1
is 0, FALG2
is 1, and FLAG3
is 2.
The type of an enumeration value is the int type.
,
after the last enumeration value can be allowed.
enum {
FLAG1,
FLAG2,
FLAG3,
}
An enumeration value can be set by =
explicitly.
enum {
FLAG1,
FLAG2 = 4,
FLAG3,
}
In the above example, FLAG1
is 0, FALG2
is 4, and FLAG3
is 5.
An enumeration value is got by the getting enumeration value.
Compilation Errors:
If an enumeration definition is invalid, a compilation error occurs.
Examples:
class Foo {
enum {
FLAG1,
FLAG2,
FLAG3,
}
}
Enumeration Attributes
Attributes can be specified to an enumeration definition.
private enum {
FLAG1,
FLAG2 = 4,
FLAG3,
}
The list of enumeration attributes:
Attributes | Descriptions |
---|---|
private | This enumeration is private. Each value of this enumeration can not be accessed from other classes. |
protected | This enumeration is protected. Each value of this enumeration can not be accessed from other classes except for the child classes. |
public | This enumeration is public. Each value of this enumeration can be accessed from other classes. This is default setting. |
Compilation Errors:
Only one of enumeration attributes private
, protected
or public
can be specified. Otherwise a compilation error occurs.
Getting Enumeration Value
A value of the enumeration can be got using the class method call.
my $flag1 = Foo->FLAG1;
my $flag2 = Foo->FLAG2;
my $flag3 = Foo->FLAG3;
A getting enumeration value is replaced to an interger literal at compilation time.
For this, if an enumeration value is changed after first publication to users, the binary compatibility is not kept.
An enumeration value can be used as an operand of the case statement.
switch ($num) {
case Foo->FLAG1: {
# ...
}
case Foo->FLAG2: {
# ...
}
case Foo->FLAG3: {
# ...
}
default: {
# ...
}
}
Local Variable
Local Variable Declaration
Local Variable is a variable that is declared in "Scope Block". Local Variable has the scope. This is the same as Local Variable in C Language.
The local variable is declared using my "Keyword".
my LOCAL_VARIABLE_NAME : TYPE;
The local variable name must be follow the rule of "Local Variable Name".
the type must be specified. Type must be a numeric type, an object type, the multi-numeric type, or a reference type.
# Local Variable Declaration Examples
my $var : int;
my $var : Point;
my $var : Complex_2d;
my $var : int*;
The local variable is initialized by "Local Variable Initial Value".
# Initialized by 0
my $num : int;
# Initialized by 0
my $num : double;
# Initialized by undef
my $point : Point;
# x is initialized by 0. y is initialized by 0.
my $z : Complex_2d;
The initialization of the local variable can be written at the same time as the local variable declaration.
# Initialized by 1
my $num : int = 1;
# Initialized by 2.5
my $num : double = 2.5;
# Initialized by Point object
my $point : Point = new Point;
The type can be omitted using the type inference,
# Type inference - int
my $num = 1;
# Type inference - double
my $num = 1.0;
The local variable declaration returns the value of the local variable. The return type is the type of the local variable.
my $ppp = my $bar = 4;
if (my $bar = 1) {
}
while (my $bar = 1) {
}
See the scope about the scope of the local variable.
Local Variable Initial Value
The local variable is initialized by the "Initial Value" in initial value.
Local Variable Access
The local variable Access is an operator to access Local Variable to get or set the value.
See "Getting Local Variable" to get Local Variable value.
"Setting Local Variable" to get Local Variable value.
If "Class Variable" with the same name as the Local Variable exists, Program uses the variable as Local Variable, not "Class Variable".
Scope
A scope is the part that is surrounded by a scope block.
# Scope block
{
# Beginning of scope
my $point = Point->new;
# End of scope
}
When a object that is not undef is assigned to a local variable, the reference count is incremented by 1.
At the end of scope, the reference count is decremented by 1. If the reference count becomes 0, the object will be destroyed.
See also garbage collection.
Block
A block is the part enclosed by {
and }
.
Class Block
A class block is a block used in a class definition.
# Class block
class Point {
}
Enumeration Block
An enumeration block is a block used in a enumeration definition.
# Enumeration block
enum {
ONE,
TWO,
}
Scope Block
The scope block has its scope. Zero or more statements are written in a scope block.
Simple Block
The simple block is a scope block.
# Simple block
{
1;
}
The simple block must have at least one statements. Otherwise it is intepreted as the array initialization.
Method Block
The method block is a scope block.
# Method block
static method foo : int () {
}
eval Block
The eval
block is a scope block.
# eval block
eval {
}
if Block
The if
block is a scope block.
# if block
if (CONDITION) {
}
elsif Block
The elsif
block is a scope block.
# elsif block
elsif (CONDITION) {
}
else Block
The else
block is a scope block.
# else block
else {
}
for Block
The for
block is a scope block.
# for Block
for (my $i = 0; $i < 3; $i++) {
}
while Block
The while
block is a scope block.
# while block
while (CONDITION) {
}
switch Block
The switch
block is a scope block.
# switch block
switch (CONDITION) {
}
case Block
The case
block is a scope block.
# case block
switch (CONDITION) {
case CASE_VALUE1: {
# ...
}
}
default Block
The default
block is a scope block.
# default block
switch (CONDITION) {
default: {
# ...
}
}
INIT Block
The INIT
block is a block to be executed just after the program starts.
INIT {
}
The INIT
block must be defined directly under the class definition.
class Foo {
INIT {
}
}
Zero or more statements can be written in a INIT
block.
INIT {
my $foo = 1 + 1;
my $bar;
}
The return statement cannot be written in INIT
block.
If a INIT
block is not defined in a class, a default empty INIT
block is defined.
An INIT
block is editted.
If a parent class exists, the INIT block of the parent class is called at the beginning of the INIT block.
If classes are used by the use statement, the interface statement, and the allow statement, The INIT blocks in the classes are called in order after the above calling.
# Before Editting
class MyClass extends ParentClass {
use Foo;
use Bar;
INIT {
$POINT = Point->new(1, 2);
}
}
# After Editting
class MyClass extends ParentClass {
use Point;
use Fn;
INIT {
ParentClass->INIT;
Point->INIT;
Fn->INIT;
$POINT = Point->new(1, 2);
}
}
An INIT
block is automatically called only once.
The execution order of INIT
blocks is not guaranteed. The INIT blocks in the "Default Loaded Classes" in default loaded class are called before INIT blocks of user defined classes.
Examples:
class Foo {
use Point;
our $NUM : int;
our $STRING : string;
our $POINT : Point;
# INIT block
INIT {
$NUM = 3;
$STRING = "abc";
$POINT = Point->new(1, 2);
}
}
Copyright & License
Copyright (c) 2023 Yuki Kimoto
MIT License