The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

SPVM::Document::Language::Types - Types in SPVM Language

Description

This document describes types in SPVM language.

Types

SPVM language has data types.

See Data type - Wikipedia about data types.

Initial Value

The list of initial values.

Type Name Initial Value
byte 0
short 0
int 0
long 0
float 0 (All bits are 0)
double 0 (All bits are 0)
Object Type undef
Multi-Numeric Type All fields are set to 0 (All bits are 0)

Numeric Type

The numeric type are an integer type and "Floating Point Type".

Numeric Type Order

a numeric type has the type order. The order is "byte", "short", "int", "long", "float", "double" from the smallest.

Integer Type

Integral types are the following four types.

Type Description Size
byte signed 8-bit integer type 1 byte
short signed 16-bit integer type 2 bytes
int signed 32-bit integer type 4 bytes
long signed 64-bit integer type 8 bytes

Note that SPVM has only singed integer types, and doesn't have unsigned integer types.

Integer Type Within int

The integer type within int is an integer type within the int type.

In other words, the integer types within int are the byte type, the short type, and the int type.

byte Type

byte type is an integer type that represents a signed 8-bit integer. This is the same type as int8_t type of the C language.

short Type

short type is an integer type that represents a signed 16-bit integer. This is the same type as int16_t type of the C language.

int Type

int type is is an integer type that represents signed 32-bit integer. This is the same as int32_t type of the C language.

long Type

long type is an integer type that represents a signed 64-bit integer. This is the same type as int64_t type of the C language.

Floating Point Type

Floating Point Type are the following two.

Type Description Size
float Single precision (32bit) floating point type 4 bytes
double Double precision (64bit) floating point type 8 bytes

float Type

The float type is a floating point type that represents a single precision(32bit) floating point. This is the same type as float type of the C language.

double Type

The double type is a floating point type that represents a double precision(64bit) floating point. This is the same type as double type of the C language.

Class Type

The class type is the type that can create the object using a new operator.

  new ClassType;

Basic Object Type

Basic object types are the class type, the array type, the string type, and the any object type.

Object Type

Object types are the basic object types and the array types.

An object type can be assigned to a any object type.

  my $object: object = new Foo;
  my $object: object = "abc";

Numeric Object Type

A numeric object type is the object type that is corresponding to the numeric type.

The list of numeric object types:

Numeric Object Type Corresponding Numeric Type
Byte byte
Short short
Int int
Long long
Float float
Double double

See also the boxing conversion and "Unboxing Conversion".

undef Type

The undef type is the type of undef value.

Undefined Value

An undefined value is represented by undef.

  undef

An undefined value can be assigned to an object type.

In the level of native APIs, undef is defined as (void*)NULL.

  (void*)NULL

An undefined value can be compared by the == operator and the != operator. An undefined value is guaranteed not to be equal to the any created object.

The type of undef is undef type

Examples:

  # Undefine values
  my $string : string = undef;
  
  if (undef) {
    
  }
  
  my $message = "Hello";
  if ($message == undef) {
    
  }

Interface Type

The interface type is a type that is defined using a class keyword and a class attribute interface_t.

  class Stringable : interface_t {
    method to_string : string ();
  }

See also "Interface".

Note that interface types are not class types although they are defined by class keyword.

Any Object Type

Any Object Type is represented by "object". Designed to represent the "void *" Type in C.

  my $object: object;

You can methodstitute the value of "Object Type" for Any Object Type.

  my $object: object = new Foo;
  my $object: object = "abc";
  my $object: object = new Foo [3];

void Type

void Type is a special Type that can only be used in the return type of the method definition and indicates the method has no return value.

  void

Basic Type

The basic types are numeric types, multi-numeric types, the class type, the any object type, and the string type.

Another definition of basic types are the types that is not array types and can become the element of array types.

Array Type

The array type is the type for the array. The array type is composed of the basic type and the dimension such as [], [][].

  # Numeric array
  int[]
  double[]
  
  # String array
  string []
  
  # Class array
  Point[]
  
  # Any object array
  object[]
  
  # 2 dimensional array
  int[][]
  
  # 3 dimensional array
  int[][][]

The array type is an object type.

Compilation Errors:

The maximam value of dimesions is 255. Otherwise a compilation error occurs.

Array

The array is the data structure for multiple values.

There are the following types of array.

  • Numeric Array
  • Object Array
  • Multi-Numeric Array

The numeric array is the array that the type of the element is the numeric type.

The object array is the array that the type of the element is the object type.

The multi-numeric array is the array that the type of the element is the multi-numeric type.

See "Creating Array" to create Array.

Array Access

Array Access is an operator to access the element of Array to get or set the value.

  ARRAY->[INDEX]

See "Getting Array Element" to get the element value of Array.

See "Setting Array Element" to set the element value of Array.

Numeric Array Type

The numeric array type is an array type for the array of the numeric type.

The list of the numeric array.

  • byte[]

  • short[]

  • int[]

  • long[]

  • float[]

  • double[]

Each element are initialized by the "Initial Value" in initial value when the creating array is performed.

byte[] Type

The byte[] type is an array type that the element type is byte.

  byte[]

Object Array Type

Object array types are the array type that the type of the element is an object type.

Examples:

  # Object array types
  my $points : Point[];
  my $points_2dim : Point[][];
  my $stringables : Stringable[];
  my $strings : string[];
  my $objects : object[];

String Array Type

String array types are the array type that the type of the element is the string type.

Examples:

  # String array types
  my $strings : string[];

Class Array Type

Class array types are the array type that the type of the element is the class type.

Examples:

  # Class array types
  my $points : Point[];

Interface Array Type

Interface array types are the array type that the type of the element is the interface type.

Examples:

  # Interface array types
  my $stringables : Stringable[];

Multi-Dimensional Array Type

The multi-dimensional array type is the array type that the type of the element is an array type.

Examples:

  # Multi-dimensional array types
  my $nums_2dim : Int[][];

Multi-Numeric Array Type

A multi-numeric array type is an array type that the basic type is a multi-numeric type.

  • Complex_2d[]
  • Complex_2f[]

The byte size of the element is the total byte size of the fields of the multi-numeric type.

For example, The byte size of the element of Complex_2d is 16 bytes (2 * 8 bytes).

The object of the multi-numeric array type can be created by the new operator.

  my $complex_nums = new Complex_2d[10];

Multi-Numeric Array

The multi-numeric values can be the elements of the array.

  my $zs = new Complex_2d[3];

The elements of the multi-numeric array is continuous multi-numeric values.

  | Complex_2d  | Complex_2d  | Complex_2d  |
  |  re  |  im  |  re  |  im  |  re  |  im  |
Multi-Numeric Array Access

The multi-numeric array access is a syntax to access the element of the multi-numeric array.

  ARRAY->[INDEX]

See "Getting Array Element" to get the element of the array.

See "Setting Array Element" to set the element of the array.

Dereference

The dereference is the operation to get the value from a reference.

A dereference operator perform a dereference.

  # Get the value using a dereference
  my $num2 = $$num_ref;
  
  # Set the value using a dereference
  $$num_ref = 3;
  
  # Get the value of a multi-numeric type using a dereference
  my $z2 = $$z_ref;
  
  # Set the value of a multi-numeric type using a dereference
  $$z_ref = $z2;

In the referencec of multi-numeric types, the deference can be performed using the arrow operator ->.

  # Get a field of a multi-numeric type using a dereference
  my $x = $z_ref->{re};
  
  # Set a field of a multi-numeric type using a dereference
  $z_ref->{re} = 1;

Any Object Array Type

The any object array type object[] is the type that any object array type can be assigned.

  # Any object array Type
  my $array : object[] = new Point[3];
  my $array : object[] = new object[3];
  my $array : object[] = new Point[][3];

Any Object Array Type is an array type.

You can get the array length using the array length operator.

  my $array : object[] = new Int[3];
  
  # Getting the length of the element of Any Object Array Type
  my $length = @$array;

You can get and set the element using the get array element syntax and the set array element.

  # Getting the element of any object array
  my $num = (Int)$array->[0];
  
  # Setting the element of any object array
  $array->[0] = Int->new(5);

When setting the element of any object array, the element type is checked. If the dimension of the element is not the dimension of the array - 1, an exception is thrown.

Compilation Errors:

If a invalid type is assigned, a compilation error occurs.

string Type

The string type is a type for the "String".

  string

string type can be qualified by "mutable Type Qualifier".

  mutable string

Examples:

  # string type
  my $message : string = "Hello";
  my $message : mutable string = new_string_len 256;

String

SPVM has the string type. A string is created by "String Literal" "String Creating Operator" or "Type Convertion" to the string type.

  # Create a string using a string literal
  my $string = "Hello";
  
  # Create a string using a string creation operator
  my $string = new_string_len 3;
  
  # Create a string using the type cast to the string type
  my $bytes = [(byte)93, 94, 95];
  my $string = (string)$bytes;

The each charcter can be get using ->[].

  # String
  my $string = "Hello";
  my $char0 = $string->[0];
  my $char1 = $string->[1];
  my $char2 = $string->[2];

By default, each character cannnot be set.

  # a compilation error.
  $string_const->[0] = 'd';

If you use mutable type qualifier|/"mutable Type Qualifier", each character can be set.

  my $string_mut = (mutable string)$string;
  $string_mut->[0] = 'd';

The created string is one more last byte that value is \0 on the internal memory. Although this has no meaning from SPVM language, this has meaning from Native APIs.

The length of the string can be got using a string length operator

  # Getting the length of the string
  my $message = "Hello"+
  my $length = length $message;

Multi-Numeric Type

The multi-numeric type is the type to represent a multi-numeric value.

The multi-numeric type can be used as the type of the local variable declaration.

  my $z : Complex_2d;

The value is initialized by the "Initial Value" in initial value.

The multi-numeric type can be used as an argument the type in the method definition.

The multi-numeric type can be used as the return type of the method definition.

  static method add_double_complex : Complex_2d ($z1 : Complex_2d, $z2 : Complex_2d) { ... }

The multi-numeric type can be used as a basic type of the array type .

  my $points = new Complex_2d[5];

The reference can be created for the value of the multi-numeric type.

  my $z : Complex_2d;
  my $z_ref = \$z;

undef cannot be assigned to the multi-numeric type.

Multi-Numeric Value

A multi-numeric value is a value that represents continuous multiple numeric values in memory.

Multi-Numeric Type Definition

A multi-numeric type is defined by the class definition that has the mulnum_t class attribute.

  # Continuous two 64bit floating point
  class Complex_2d : mulnum_t {
    re : double;
    im : double;
  }

The type of a field must be a numeric type.

The types of all fields must be the same types.

The length of the fields must be less than or equal to 255.

The multi-numeric type must end with the following suffix.

  _[FieldsLength][TypeSuffix]

The length of the fields in the suffix must be the same as the length of the fields.

The type suffix in the suffix must correspond to the numeric type that is explained in the multi-numeric type suffix.

See the multi-numeric type field access to get and set the field of the multi-numeric type.

Multi-Numeric Type Suffix

The list of the multi-numeric type suffix.

Numeric Type Type Suffix
byte b
short s
int i
long l
float f
double d

Multi-Numeric Type Field Access

The multi-numeric type field access is an syntax to access the field of the multi-numeric value.

  MULTI_NUMERIC_VALUE->{FIELD_NAME}

See "Getting Multi-Numeric Field" to get the field of the multi-numeric value.

See "Setting Multi-Numeric Field" to set the field of the multi-numeric value.

Reference Type

Reference Type is a Type that can store the address of a variable. Add * after a numeric type or the multi-numeric type You can define it.

  my $num : int;
  my $num_ref : int* = \$num;
  
  my $z : Complex_2d;
  my $z_ref : Complex_2d* = \$z;

Only the address of the Local Variable acquired by "Reference Operator" can be assigned to the value of Reference Type.

Reference Type can be used as Type of argument in the method definition.

Reference Type cannot be used as return value Type in the method definition.

Reference Type cannot be used as the field type in the class definition.

Reference Type cannot be used as the type of Class Variable in the class definition.

See "Reference" for a detailed explanation of Reference.

Compilation Errors:

If only Local Variable Declaration of Reference Type is performed, a compilation error occurs

Reference Type can be used as Type of the local variable declaration. The address of the Local Variable must be stored by the Reference Operator. In case of only Local Variable Declaration, a compilation error occurs

If the Reference Type is used at an Invalid location, a compilation error occurs

Reference

The reference is the address of a local variable on the memory.

Creating Reference

The reference operator creates the reference of a local variable.

A reference is assigned to the "Reference Type" in reference type.

The operand of a reference operator must be the variable of a numeric type or a multi-numeric type.

  # The reference of numeric type
  my $num : int;
  my $num_ref : int* = \$num;
  
  # The reference of multi-numeric type
  my $z : Complex_2d;
  my $z_ref : Complex_2d* = \$z;

The reference type can be used as the types of the arguments of a method.

  # Method Definition
  static method sum : void ($result_ref : int*, $num1 : int, $num2 : int) {
    $$result_ref = $num1 + $num2;
  }
  
  # Method Call
  my $num1 = 1;
  my $num2 = 2;
  my $result_ref = \$result;
  sum($result_ref, $num1, $num2);

Numeric Reference Type

Numeric Reference Type means a numeric type for a reference type. Says.

Multi-Numeric Reference Type

Multi-Numeric Reference Type means a reference type for the multi-numeric type variables. > Means.

Type Qualifier

Type qualifiers qualify the type.

  QUALIFIER TYPE

mutable Type Qualifier

The mutable type qualifier is used to allow to set the character of the string.

  my $string : mutable string;

Examples:

  # Mutable string
  my $message = (mutable string)"abc";
  $message->[0] = 'd';

Type Inference

Omitting the type when the local variable declaration by Type Inference can. Type Inference is always performed by the type on the Right side of Assignment Operator.

  # int
  my $num = 1;
  
  # double
  my $num = 1.0;
  
  # Foo
  my $foo = new Foo;

Type Assignability

The assignability at compile-time is explained.

Compilation Errors:

The assignability is false, a compilation error occurs.

Type Assignability to Numeric

Explains the assignability to the numeric types.

Type Assignability from Numeric to Numeric

If the nemric type order of LEFT_OPERAND is greater than or equal to the nemric type order of RIGHT_OPERAND, the assignability is true.

If the nemric type order of LEFT_OPERAND is greater than the nemric type order of RIGHT_OPERAND, the numeric widening conversion is performed.

Type AssignabilityToFromImplicite Type Conversion
TruebytebyteNone
TrueshortshortNone
TrueintintNone
TruelonglongNone
TruefloatfloatNone
TruedoubledoubleNone
TrueshortbyteNumeric Widening Conversion
TrueintbyteNumeric Widening Conversion
TruelongbyteNumeric Widening Conversion
TruefloatbyteNumeric Widening Conversion
TruedoublebyteNumeric Widening Conversion
TrueintshortNumeric Widening Conversion
TruelongshortNumeric Widening Conversion
TruefloatshortNumeric Widening Conversion
TruedoubleshortNumeric Widening Conversion
TruelongintNumeric Widening Conversion
TruefloatintNumeric Widening Conversion
TruedoubleintNumeric Widening Conversion
TruefloatlongNumeric Widening Conversion
TruedoublelongNumeric Widening Conversion
TruedoublefloatNumeric Widening Conversion

Examples:

  # int to int
  my $num : int = 3;
  
  # byte to int
  my $num : int = (byte)5;
  
  # double to double
  my $num : double = 4.5;
  
  # float to double
  my $num : double = 4.5f;

If the nemric type order of LEFT_OPERAND is less than the nemric type order of RIGHT_OPERAND, the assignability is conditional true.

The condition is that RIGHT_OPERAND is a interger literal and the value is between the max and minimal value of the type of LEFT_OPERAND.

If the condition is ture, the numeric narrowing conversion is performed.

Type AssignabilityToFromImplicite Type Conversion
Conditional TruebyteshortNumeric Narrowing Conversion
Conditional TruebyteintNumeric Narrowing Conversion
Conditional TruebytelongNumeric Narrowing Conversion
FalsebytefloatNone
FalsebytedoubleNone
Conditional TrueshortintNumeric Narrowing Conversion
Conditional TrueshortlongNumeric Narrowing Conversion
FalseshortfloatNone
FalseshortdoubleNone
Conditional TrueintlongNumeric Narrowing Conversion
FalseintfloatNone
FalseintdoubleNone
FalselongfloatNone
FalselongdoubleNone
FalsefloatdoubleNone

Examples:

  # int to byte
  my $num : byte = 127;

Type Assignability from NumericObject to Numeric

If the type of LEFT_OPERAND is a numeric type corresponding to the numeric object type of RIGHT_OPERAND and the type of RIGHT_OPERAND is a numeric object type, the assignability is true.

Type AssignabilityToFromImplicite Type Conversion
TruebyteByteUnboxing Conversion
TrueshortShortUnboxing Conversion
TrueintIntUnboxing Conversion
TruelongLongUnboxing Conversion
TruefloatFloatUnboxing Conversion
TruedoubleDoubleUnboxing Conversion

Examples:

  my $int : int = Int->new(3);

  my $double : double = Double->new(3.5);

Type Assignability from Any Object to Numeric

If the type of LEFT_OPERAND is a numeric type and the type of RIGHT_OPERAND is a any object type object, the assignability is true.

The unboxing conversion corresponding to the numeric type is performed.

Type AssignabilityToFromImplicite Type Conversion
TrueNUMERIC_XobjectUnboxing Conversion

Examples:

  my $int : int = (object)Int->new(3);

  my $double : double = (object)Double->new(3.5);

Type Assignability from Others to Numeric

If the type of LEFT_OPERAND is a numeric type and the type of RIGHT_OPERAND is other than the types described above, the assignability is false.

Type Assignability to Multi-Numeric

If the type of LEFT_OPERAND is a multi-numeric type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
TrueMULNUM_XMULNUM_XNone
FalseMULNUM_XOTHERNone

Examples:

  my $z1 : Complex_2d;
  my $z2 : Complex_2d = $z1;

Type Assignability to Referenece

If the type of LEFT_OPERAND is a reference type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
TrueREF_XREF_XNone
FalseREF_XOTHERNone

Examples:

  my $num : int = 5;
  my $num_ref : int* = \num;

Type Assignability to String

If the type of LEFT_OPERAND is the string type without the mutable type qualifier and the type of RIGHT_OPERAND is the string type, the assignability is true.

If the type of LEFT_OPERAND is the string type with the mutable type qualifier and the type of RIGHT_OPERAND is the string type with the mutable type qualifier, the assignability is true.

If the type of LEFT_OPERAND is the string type with the mutable type qualifier and the type of RIGHT_OPERAND is the string type without the mutable type qualifier, the assignability is false.

If the type of LEFT_OPERAND is the string type and the type of RIGHT_OPERAND is a numeric type or the undef type, the assignability is true.

If the type of RIGHT_OPERAND is a numeric type, the numeric-to-string conversion is performed.

Type AssignabilityToFromImplicite Type Conversion
TruestringstringNone
Truestringmutable stringNone
Truemutable stringmutable stringNone
Falsemutable stringstringNone
TruestringstringNone
TruestringNUMERIC_Xnumeric-to-string conversion
TruestringundefNone
FalsestringOTHERNone

Examples:

  my $string : string = "abc";
  my $num_string : string = 3;
  my $string : string = undef;

Type Assignability to NumericObject

If the type of LEFT_OPERAND is a numeric object type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, a numeric type that is corresponding to the numeric object type, or the undef type, the assignability is true.

Otherwise, the assignability is false.

If the type of RIGHT_OPERAND is a numeric type, the boxing conversion is performed.

Type AssignabilityToFromImplicite Type Conversion
TrueNUMERIC_OBJECT_XNUMERIC_OBJECT_XNone
TrueNUMERIC_OBJECT_XNUMERIC_XBoxing Conversion
TrueNUMERIC_OBJECTundefNone
FalseNUMERIC_OBJECTOTHERNone

Examples:

  my $num_object : Int = Int->new(3);
  my $num_object : Int = 3;
  my $num_object : Int = undef;

Type Assignability to Class

If the type of LEFT_OPERAND is a class type and the type of RIGHT_OPERAND is the same type, or the undef type, the assignability is true.

If the type of LEFT_OPERAND is a super class of the type of RIGHT_OPERAND, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
TrueCLASS_XCLASS_XNone
TrueCLASSundefNone
TrueSUPER_CLASS_XCLASS_YNone
FalseCLASSOTHERNone

Examples:

  my $point : Point = Point->new;
  my $point : Point = undef;

Type Assignability to Interface

If the type of LEFT_OPERAND is an interface type and the type of RIGHT_OPERAND is the same type, or the undef type, the assignability is true.

If the type of LEFT_OPERAND is an interface type and the type of RIGHT_OPERAND is a class type and the class has the same interface of LEFT_OPERAND, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
TrueINTERFACE_XINTERFACE_XNone
TrueINTERFACE_XINTERFACE_HAVING_YNone
TrueINTERFACEundefNone
FalseINTERFACEOTHERNone

Examples:

  # Point has Stringable interface
  my $stringable : Stringable = Point->new(1, 2);
  my $stringable : Stringable = undef;

Type Assignability to Any Object

If the type of LEFT_OPERAND is the any object type and the type of RIGHT_OPERAND is an object type, a numeric type or the undef type, the assignability is true.

Otherwise, the assignability is false.

If the type of RIGHT_OPERAND is a numeric type, the boxing conversion is performed.

Type AssignabilityToFromImplicite Type Conversion
TrueobjectOBJECT_YNone
TrueobjectNUMERIC_XBoxing Conversion
TrueobjectundefNone
FalseobjectOTHERNone

Examples:

  my $object : object = Point->new;
  my $num_object : object = 3;
  my $object : object = undef;

Type Assignability to Undefined

If the type of LEFT_OPERAND is the undef type, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
Falseundef TypeOTHERNone

Examples:

  # The assignability is false
  undef = Point->new;

Type Assignability to Numeric Array

If the type of LEFT_OPERAND is a numeric array type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND or the undef type, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
Truebyte[]byte[]None
Trueshort[]short[]None
Trueint[]int[]None
Truelong[]long[]None
Truefloat[]float[]None
Truedouble[]double[]None
TrueNUMERIC[]undefNone
FalseNUMERIC[]OTHERNone

Examples:

  my $nums : int[] = new int[3];
  my $nums : int[] = undef;

Type Assignability to Multi-Numeric Array

If the type of LEFT_OPERAND is a multi-numeric array type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND or the undef type, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
TrueMULNUM_X[]MULNUM_X[]None
TrueMULNUM_X[]undefNone
FalseMULNUM_X[]OTHERNone

Examples:

  my $nums : Complex_2d[] = new Complex_2d[3];
  my $nums : Complex_2d[] = undef;

Type Assignability to String Array

If the type of LEFT_OPERAND is a string array type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND or the undef type, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
Truestring[]string[]None
Truestring[]undefNone
Falsestring[]OTHERNone

Examples:

  my $strings : string[] = ["abc", "def"];
  my $strings : string[] = undef;

Type Assignability to Class Array

If the type of LEFT_OPERAND is a class array type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND or the undef type, the assignability is true.

If the basic type of LEFT_OPERAND is an super class of the type of RIGHT_OPERAND, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
TrueCLASS_X[]CLASS_X[]None
TrueSUPER_CLASS_X[]CLASS_Y[]None
TrueCLASS_X[]undefNone
FalseCLASS_X[]OTHERNone

Examples:

  my $points : Point[] = new Point[3];
  my $points : Point[] = undef;

Type Assignability to Interface Array

If the type of LEFT_OPERAND is an interface array type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND or the undef type, the assignability is true.

If the type of LEFT_OPERAND is an interface array type and the type of RIGHT_OPERAND is a class array type and its basic type can assign to the basic type of LEFT_OPERAND, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
TrueINTERFACE_X[]INTERFACE_X[]None
TrueINTERFACE_X[]undefNone
TrueINTERFACE_X[]INTERFACE_HAVING_Y[]None
FalseINTERFACE_X[]OTHERNone

Examples:

  my $stringables : Stringable[] = new Stringable[3];

  my $stringables : Stringable[] = new Point[3];
  
  my $stringables : Stringable[] = undef;

Type Assignability to Any Object Array

If the type of LEFT_OPERAND is the any object array type object[] and the type of RIGHT_OPERAND is an object array type or the undef type, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
Trueobject[]OBJECT_ARRAY_YNone
Trueobject[]undefNone
Falseobject[]OTHERNone

Examples:

  my $any_objects0 : object[];
  my $any_objects : object[] = $any_objects0;
  
  my $points : Point[];
  my $any_object : object[] = $points;
  
  my $any_object : object[] = undef;
  
  my $points_2dim : Point[][];
  my $any_object : object[] = $points_2dim;
  
  my $stringables : Stringable[];
  my $any_object : object[] = $stringables;
  
  my $strings : string[];
  my $any_object : object[] = $strings;

Type Assignability to Multi-Dimensional Array

If the type of LEFT_OPERAND is a multi-dimensional array type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND or the undef type, the assignability is true.

If the type dimesion of LEFT_OPERAND is equal to the type dimension of RIGHT_OPERAND, and the basic type of LEFT_OPERAND is a super class of the basic type of RIGHT_OPERAND, the assignability is true.

If the type dimesion of LEFT_OPERAND is equal to the type dimension of RIGHT_OPERAND, and the basic type of RIGHT_OPERAND has the basic type of LEFT_OPERAND, the assignability is true.

Otherwise, the assignability is false.

Type AssignabilityToFromImplicite Type Conversion
TrueX[]..[]X[]..[]None
Trueobject[]undefNone
TrueSUPER_CLASS_X[]..[]CLASS_Y[]..[]None
TrueINTERFACE_X[]..[]INTERFACE_HAVING_Y[]..[]None
Falseobject[]OTHERNone

([]..[] means two or more [])

Examples:

  my $points_2dim : Point[][];
  my $muldim_array : Point[][] = $points_2dim;

  my $muldim_array : Point[][] = undef;

  my $strings_2dim : String[][];
  my $muldim_array : Stringable[][] = $strings_2dim;

  {
    my $cb = method : string ($object : object) {
      my $point = (Point)$object;
      return $point->to_string;
    };
    my $muldim_array : Stringer[][] = [[$cb]];
  }

Type Castability

The castability at compile-time is explained.

Compilation Errors:

The castability is false, a compilation error occurs.

Type Castability to Numeric

The castability to the numeric types is explained.

Type Castability from Numeric to Numeric

If the type of LEFT_OPERAND is a numeric type and the type of RIGHT_OPERAND is a numeric type, the castability is true.

If the nemric type order of LEFT_OPERAND is greater than the nemric type order of RIGHT_OPERAND, the numeric widening conversion is performed.

If the nemric type order of LEFT_OPERAND is less than the nemric type order of RIGHT_OPERAND, the numeric narrowing conversion is performed.

If the nemric type order of LEFT_OPERAND is equal to the nemric type order of RIGHT_OPERAND, copying is performed.

Type CastabilityToFromConversion or Type Checking
TruebytebyteNone
TrueshortshortNone
TrueintintNone
TruelonglongNone
TruefloatfloatNone
TruedoubledoubleNone
TrueshortbyteNumeric Widening Conversion
TrueintbyteNumeric Widening Conversion
TruelongbyteNumeric Widening Conversion
TruefloatbyteNumeric Widening Conversion
TruedoublebyteNumeric Widening Conversion
TrueintshortNumeric Widening Conversion
TruelongshortNumeric Widening Conversion
TruefloatshortNumeric Widening Conversion
TruedoubleshortNumeric Widening Conversion
TruelongintNumeric Widening Conversion
TruefloatintNumeric Widening Conversion
TruedoubleintNumeric Widening Conversion
TruefloatlongNumeric Widening Conversion
TruedoublelongNumeric Widening Conversion
TruedoublefloatNumeric Widening Conversion
TruebyteshortNumeric Narrowing Conversion
TruebyteintNumeric Narrowing Conversion
TruebytelongNumeric Narrowing Conversion
TruebytefloatNumeric Narrowing Conversion
TruebytedoubleNumeric Narrowing Conversion
TrueshortintNumeric Narrowing Conversion
TrueshortlongNumeric Narrowing Conversion
TrueshortfloatNumeric Narrowing Conversion
TrueshortdoubleNumeric Narrowing Conversion
TrueintlongNumeric Narrowing Conversion
TrueintfloatNumeric Narrowing Conversion
TrueintdoubleNumeric Narrowing Conversion
TruelongfloatNumeric Narrowing Conversion
TruelongdoubleNumeric Narrowing Conversion
TruefloatdoubleNumeric Narrowing Conversion

Examples:

  # int to int
  my $num = (int)3;
  
  # byte to int
  my $num_byte : byte = 5;
  my $num = (int)5;
  
  # double to double
  my $num = (double)4.5;
  
  # float to double
  my $num = (double)4.5f;
  
  # int to byte
  my $num = (byte)127;

  # double to int
  my $num = (int)2.5;

Type Castability from NumericObject to Numeric

If the type of LEFT_OPERAND is a numeric type corresponding to the numeric object type of RIGHT_OPERAND and the type of RIGHT_OPERAND is a numeric object type, the castability is true.

Type CastabilityToFromConversion or Type Checking
TruebyteByteUnboxing Conversion
TrueshortShortUnboxing Conversion
TrueintIntUnboxing Conversion
TruelongLongUnboxing Conversion
TruefloatFloatUnboxing Conversion
TruedoubleDoubleUnboxing Conversion

Examples:

  my $int = (int)Int->new(3);

  my $double = (double)Double->new(3.5);

Type Castability from Any Object to Numeric

If the type of LEFT_OPERAND is a numeric type and the type of RIGHT_OPERAND is a any object type object, the castability is true.

The unboxing conversion corresponding to the numeric type is performed.

Type CastabilityToFromConversion or Type Checking
TrueNUMERIC_XobjectUnboxing Conversion

Examples:

  my $object : object = Int->new(3);
  my $int = (int)$object;
  
  my $object : object = Double->new(3.5);
  my $double = (double)$object;

Type Castability from Others to Numeric

If the type of LEFT_OPERAND is a numeric type and the type of RIGHT_OPERAND is other than the types described above, the castability is false.

Type Castability to Multi-Numeric

If the type of LEFT_OPERAND is a multi-numeric type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, the castability is true.

Otherwise, the castability is false.

Type CastabilityToFromConversion or Type Checking
TrueMULNUM_XMULNUM_XNone
FalseMULNUM_XOTHERNone

Examples:

  my $z1 : Complex_2d;
  my $z2 = (Complex_2d)$z1;

Type Castability to Referenece

If the type of LEFT_OPERAND is a reference type and the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, the castability is true.

Otherwise, the castability is false.

Type CastabilityToFromConversion or Type Checking
TrueREF_XREF_XNone
FalseREF_XOTHERNone

Examples:

  my $num : int = 5;
  my $num_ref = (int*)\num;

Type Castability to String

If the type of LEFT_OPERAND is the string type and the type of RIGHT_OPERAND is the string type, the castability is true.

If the type of LEFT_OPERAND is the string type with the mutable type qualifier and the type of RIGHT_OPERAND is the string type without the mutable type qualifier, the runtime type checking is performed.

If the type of RIGHT_OPERAND is a numeric type, the numeric-to-string conversion is performed.

If the type of LEFT_OPERAND is the string type and the type of RIGHT_OPERAND is a numeric type, the undef type, or the any object type object, the castability is true.

If the type of RIGHT_OPERAND is a numeric type, the numeric-to-string conversion is performed.

If the type of LEFT_OPERAND is the string type and the type of RIGHT_OPERAND is the any object type object, the castability is true and the runtime type checking is performed.

Type CastabilityToFromConversion or Type Checking
TruestringstringNone
Truestringmutable stringNone
Truemutable stringmutable stringNone
Truemutable stringstringRuntime type checking
TruestringstringNone
TruestringNUMERIC_XNumeric-to-String Conversion
TruestringobjectRuntime type checking
TruestringundefNone
FalsestringOTHERNone

Examples:

  my $string = (string)"abc";
  my $num_string = (string)3;
  my $string : string = undef;

Type Castability to NumericObject

If the type of LEFT_OPERAND is a numeric object type and the types of RIGHT_OPERANDs are the following cases:

If the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, a numeric type that is corresponding to the numeric object type, the any object type object, or the undef type, the castability is true.

The type of RIGHT_OPERAND is other than above, the castability is false.

If the type of RIGHT_OPERAND is a numeric type, the boxing conversion is performed.

If the type of LEFT_OPERAND is the type of RIGHT_OPERAND is the any object type object, the runtime type checking is performed.

Type CastabilityToFromConversion or Type Checking
TrueNUMERIC_OBJECT_XNUMERIC_OBJECT_XNone
TrueNUMERIC_OBJECT_XNUMERIC_XBoxing Conversion
TrueNUMERIC_OBJECTobjectRuntime type checking
TrueNUMERIC_OBJECTundefNone
FalseNUMERIC_OBJECTOTHERNone

Examples:

  my $num_object = (Int)Int->new(3);
  my $num_object = (Int)3;
  my $num_object = (Int)undef;
  
  my $object : object = Int->new(3);
  my $num_object = (Int)$object;

Type Castability to Class

If the type of LEFT_OPERAND is a class type and the types of RIGHT_OPERANDs are the following cases:

If the type of RIGHT_OPERAND is the same type, the any object type object, an interface type or the undef type, the castability is true.

If the type of LEFT_OPERAND is a super class of the type of right operand, the castability is true.

If the type of RIGHT_OPERAND is a super class of the type of left operand, the castability is true.

Otherwise, the castability is false.

If the type of RIGHT_OPERAND is the any object type object or an interface type, the runtime type checking is performed.

Type CastabilityToFromConversion or Type Checking
TrueCLASS_XCLASS_XNone
TrueSUPER_CLASS_XCLASS_YNone
TrueCLASS_XSUPER_CLASS_YRuntime type checking
TrueCLASS_XINTERFACE_YRuntime type checking
TrueCLASS_XobjectRuntime type checking
TrueCLASSundefNone
FalseCLASSOTHERNone

Examples:

  my $point : Point = Point->new;
  
  my $stringable : Stringable;
  my $point = (Point)$stringable;

  my $stringer : Stringer;
  my $point = (Point)$stringer

  my $point = (Point)undef;

Type Castability to Interface

If the type of LEFT_OPERAND is an interface type, and the types of RIGHT_OPERANDs are the following cases:

If the type of RIGHT_OPERAND is the same type, the any object type object , an interface type or the undef type, the castability is true.

If the type of RIGHT_OPERAND is a class type and the class has the interface of LEFT_OPERAND, the castability is true.

Otherwise, the castability is false.

If the type of RIGHT_OPERAND is the any object type object, an interface type, the runtime type checking is performed.

Type CastabilityToFromConversion or Type Checking
TrueINTERFACE_XINTERFACE_XNone
TrueINTERFACE_XINTERFACE_HAVING_YNone
TrueINTERFACE_XINTERFACE_YRuntime type checking
TrueINTERFACE_XobjectRuntime type checking
TrueINTERFACEundefNone
FalseINTERFACEOTHERNone

Examples:

  my $stringable1 : Stringable;
  my $stringable2 = (Stringable)$stringable1;
  
  my $cloneable : Cloneable;
  my $stringable = (Stringable)$cloneable;
  
  my $stringable  = (Stringable)Point->new(1, 2);

  my $object : object  = Point->new(1, 2);
  my $stringable  = (Stringable)Point->new(1, 2);
  
  my $stringable : Stringable = undef;

Type Castability to Any Object

If the type of LEFT_OPERAND is the any object type and the types of RIGHT_OPERANDs are the following cases:

If the type of RIGHT_OPERAND is an object type, a numeric type or the undef type, the castability is true.

Otherwise, the castability is false.

If the type of RIGHT_OPERAND is a numeric type, the boxing conversion is performed.

Type CastabilityToFromConversion or Type Checking
TrueobjectOBJECT_YNone
TrueobjectNUMERIC_XBoxing Conversion
TrueobjectundefNone
FalseobjectOTHERNone

Examples:

  my $object : object = Point->new;
  my $num_object : object = 3;
  my $object : object = undef;

Type Castability to Numeric Array

If the type of LEFT_OPERAND is the byte[] type and the type of RIGHT_OPERAND is the string type, the castability is true.

If the type of LEFT_OPERAND is a numeric array type and the types of RIGHT_OPERANDs are the following cases:

If the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, the any object type obejct or the undef type, the castability is true.

Otherwise, the castability is false.

If the type of LEFT_OPERAND is the byte[] type and the type of RIGHT_OPERAND is the string type, "String-to-byte[] Conversion" is performed.

If the type of RIGHT_OPERAND is the any object type obejct, the runtime type checking is performed.

Type CastabilityToFromConversion or Type Checking
Truebyte[]stringString-to-byte[] Conversion
TrueNUMERIC_X[]NUMERIC_X[]None
TrueNUMERIC[]objectRuntime type checking
TrueNUMERIC[]undefNone
FalseNUMERIC[]OTHERNone

Examples:

  my $bytes = (byte[])"abc";
  
  my $nums = (int[])new int[3];
  
  my $object : object = new int[3];
  my $nums = (int[])$object;
  
  my $nums = (int[])undef;

Type Castability to Multi-Numeric Array

If the type of LEFT_OPERAND is a multi-numeric array type and the types of RIGHT_OPERANDs are the following cases:

If the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, the any object type obejct or the undef type, the castability is true.

Otherwise, the castability is false.

If the type of RIGHT_OPERAND is the any object type obejct, the runtime type checking is performed.

Type CastabilityToFromConversion or Type Checking
TrueMULNUM_X[]MULNUM_X[]None
TrueMULNUM_X[]objectRuntime type checking
TrueMULNUM_X[]undefNone
FalseMULNUM_X[]OTHERNone

Examples:

  my $nums = (Complex_2d[])new Complex_2d[3];

  my $object : object = new Complex_2d[3];
  my $nums = (Complex_2d[])$object;

  my $nums = (Complex_2d[])undef;

Type Castability to String Array

If the type of LEFT_OPERAND is a string array type and the types of RIGHT_OPERANDs are the following cases:

If the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, the any object type obejct, the any object array type obejct[] or the undef type, the castability is true.

Otherwise, the castability is false.

If the type of RIGHT_OPERAND is the any object type obejct, or the any object array type obejct[], the runtime type checking is performed.

Type CastabilityToFromConversion or Type Checking
Truestring[]string[]None
Truestring[]objectRuntime type checking
Truestring[]object[]Runtime type checking
Truestring[]undefNone
Falsestring[]OTHERNone

Examples:

  my $strings = (string[])["abc", "def"];

  my $object : object = ["abc", "def"];
  my $strings = (string[])$object;

  my $objects : object[] = ["abc", "def"];
  my $strings = (string[])$object;

  my $strings  = (string[])undef;

Type Castability to Class Array

If the type of LEFT_OPERAND is a class array type and the types of RIGHT_OPERANDs are the following cases:

If the basic type of LEFT_OPERAND is a super class of the basic type of RIGHT_OPERAND, the castability is true.

If the basic type of RIGHT_OPERAND is a super class of the basic type of LEFT_OPERAND, the castability is true.

If the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, the any object type obejct, the any object array type obejct[] or the undef type, the castability is true.

Otherwise, the castability is false.

If the type of RIGHT_OPERAND is the any object type obejct, or the any object array type obejct[], the runtime type checking is performed.

Type CastabilityToFromConversion or Type Checking
TrueCLASS_X[]CLASS_X[]None
TrueSUPER_CLASS_X[]CLASS_Y[]None
TrueCLASS_X[]SUPER_CLASS_Y[]Runtime type checking
TrueCLASS_X[]objectRuntime type checking
TrueCLASS_X[]object[]Runtime type checking
TrueCLASS_X[]undefNone
FalseCLASS_X[]OTHERNone

Examples:

  my $points = (Point[])new Point[3];

  my $object : object = new Point[3];
  my $points = (Point[])$object;

  my $objects : object[] = new Point[3];
  my $points = (Point[])$object;

  my $points = (Point[])undef;

Type Castability to Interface Array

If the type of LEFT_OPERAND is an interface array type and the types of RIGHT_OPERANDs are the following cases:

If the type of RIGHT_OPERAND is a class array type and its basic type has the interface of the basic type of LEFT_OPERAND, the castability is true.

If the type of RIGHT_OPERAND is the same type of LEFT_OPERAND, the castability is true.

If the type of RIGHT_OPERAND is an differnt type of interface array type, the castability is also true.

If the type of RIGHT_OPERAND is the any object type obejct, the any object array type obejct[] or the undef type, the castability is true.

Otherwise, the castability is false.

If the type of RIGHT_OPERAND is an differnt type of interface array type, the runtime type checking is performed.

If the type of RIGHT_OPERAND is the any object type obejct, or the any object array type obejct[], the runtime type checking is performed.

Type CastabilityToFromConversion or Type Checking
TrueINTERFACE_X[]INTERFAECE_HAVING_Y[]None
TrueINTERFACE_X[]INTERFACE_X[]None
TrueINTERFACE_X[]INTERFACE_Y[]Runtime type checking
TrueINTERFACE_X[]objectRuntime type checking
TrueINTERFACE_X[]object[]Runtime type checking
TrueINTERFACE_X[]undefNone
FalseINTERFACE_X[]OTHERNone

Examples:

  my $stringables = (Stringable[])new Stringable[3];

  my $stringables = (Stringable[])new Point[3];
  
  my $stringables = (Stringable[])undef;

Type Castability to Any Object Array

If the type of LEFT_OPERAND is the any object array type object[] and the types of RIGHT_OPERANDs are the following cases:

If the type of RIGHT_OPERAND is an object array type or the undef type, the castability is true.

If the type of RIGHT_OPERAND is an any object type, the castability is true.

Otherwise, the castability is false.

If the type of RIGHT_OPERAND is an any object type, the runtime type checking is performed.

Type CastabilityToFromConversion or Type Checking
Trueobject[]OBJECT_ARRAY_YNone
Trueobject[]undefNone
Trueobject[]objectRuntime type checking
Falseobject[]OTHERNone

Examples:

  my $any_object : object;
  my $any_objects = (object[])$any_object;

  my $any_objects0 : object[];
  my $any_objects = (object[])$any_objects0;

  my $points : Point[];
  my $any_object = (object[])$points;

  my $any_object = (object[])undef;

  my $points_2dim : Point[][];
  my $any_object = (object[])$points_2dim;

  my $stringables : Stringable[];
  my $any_object = (object[])$stringables;
  
  my $strings : string[];
  my $any_object = (object[])$strings;
  

Type Castability to Multi-Dimensional Array

If the type of LEFT_OPERAND is a multi-dimensional array type and and the types of RIGHT_OPERANDs are the following cases:

If the type of RIGHT_OPERAND is the same type of LEFT_OPERAND or the undef type, the castability is true.

If the type of RIGHT_OPERAND is an any object type, the castability is true.

If the type dimesion of LEFT_OPERAND is equal to the type dimension of RIGHT_OPERAND, and the basic type of LEFT_OPERAND is a super class of the basic type of RIGHT_OPERAND, the castability is true.

If the type dimesion of LEFT_OPERAND is equal to the type dimension of RIGHT_OPERAND, and the basic type of RIGHT_OPERAND is a super class of the basic type of LEFT_OPERAND, the castability is true.

If the basic type of the type of LEFT_OPERAND is an interface type and the basic type of the type of RIGHT_OPERAND is a class type and the dimension of the type of RIGHT_OPERAND is the same as the dimension of the type left oerand and the basic type of the type of RIGHT_OPERAND has the interface of the basic type of the type of LEFT_OPERAND , the castability is true.

Otherwise, the castability is false.

Type CastabilityToFromConversion or Type Checking
TrueANY_X[]..[]ANY_X[]..[]None
TrueANY_X[]..[]objectRuntime type checking
TrueANY_X[]..[]object[]Runtime type checking
TrueANY_X[]..[]undefNone
TrueSUPER_CLASS_X[]..[]CLASS_Y[]..[]None
TrueCLASS_X[]..[]SUPER_CLASS_Y[]..[]Runtime type checking
TrueINTERFACE_X[]..[]INTERFACE_HAVING_Y[]..[]None
Falseobject[]OTHERNone

([]..[] means two or more [])

Examples:

  my $points_2dim : Point[][];
  my $muldim_array : Point[][] = $points_2dim;
  
  my $muldim_array : Point[][] = undef;
  
  my $strings_2dim : String[][];
  my $muldim_array : Stringable[][] = $strings_2dim;
  
  {
    my $cb = method : string ($object : object) {
      my $point = (Point)$object;
      return $point->to_string;
    };
    my $muldim_array : Stringer[][] = [[$cb]];
  }

Type Conversion

Type conversion is explained.

Explicite Type Conversion

The explicite type conversion is the type conversion performed by a type cast expicitely.

Examples:

  # The explicte type conversion from long to int 
  my $num = (int)123L;
  
  # The explicte type conversion from byte[] to string
  my $num = (string)new byte[3];
  
  # The explicte type conversion from string to byte[]
  my $num = (byte[])"Hello";

Implicite Type Conversion

The implicite type conversion is the type conversion performed implicitly when a value is assigned using assignment operator, pass an argument to a method using a method call, or set a return value using the return statement.

See "Type Assignability" if you know when implicite type conversion is performed.

Examples:

  # The implicite type conversion from int to double 
  my $num : double = 5;
  
  # The implicite type conversion from double to Double
  my $num_object : Double = 5.1;
  
  # The implicite type conversion from Double to double
  my $num : double = Double->new(5.1);
  
  # The implicite type conversion from int to string
  my $string : string = 4;

Integer Promotional Conversion

The integer promotional conversion is a type conversion to convert an integer type within int to the int type using the numeric widening conversion.

Numeric Widening Conversion

The numeric widening conversion is a type conversion from a small-order numeric type to a large-order numeric type.

See also numeric types order abount the order of numeric type.

The return value of a converion are same as the return value of the type cast of the C language.

  (TYPE)OPERAND

byte to short:

  int8_t from = VALUE;
  int16_t to = (int16_t)from;

byte to int:

  int8_t from = VALUE;
  int32_t to = (int32_t)from;

byte to long:

  int8_t from = VALUE;
  int64_t to = (int64_t)from;

byte to float:

  int8_t from = VALUE;
  float to = (float)from;

byte to double:

  int8_t from = VALUE;
  double to = (double)from;

short to int:

  int16_t from = VALUE;
  int32_t to = (int32_t)from;

short to long:

  int16_t from = VALUE;
  int64_t to = (int64_t)from;

short to float:

  int16_t from = VALUE;
  float to = (float)from;

short to double:

  int16_t from = VALUE;
  double to = (double)from;

int to long:

  int32_t from = VALUE;
  int64_t to = (int64_t)from;

int to float:

  int32_t from = VALUE;
  float to = (float)from;

int to double:

  int32_t from = VALUE;
  double to = (double)from;

long to float:

  int64_t from = VALUE;
  float to = (float)from;

long to double:

  int64_t from = VALUE;
  double to = (double)from;

The numeric widening conversion is performed in some of the type casts, the index of the array access, the length of the creating array, OPERAND of the unary plus operator, OPERAND of the unary minus operator, and the left and right operands of the shift operators.

Numeric Narrowing Conversion

The numeric narrowing conversion is a conversion from a wide numeric type to a narrow numeric type.

See also numeric types order abount the order of numeric type.

The return value of a converion are same as the return value of the type cast of the C language.

  (TYPE)OPERAND

double to float:

  double from = value;
  float to = (float)from;

double to long:

  double from = value;
  int64_t to = (int64_t)from;

double to int:

  double from = value;
  int32_t to = (int32_t)from;

double to short:

  double from = value;
  int16_t to = (int16_t)from;

double to byte:

  double from = value;
  int8_t to = (int8_t)from;

float to long:

  float from = value;
  int64_t to = (int64_t)from;

float to int:

  float from = value;
  int32_t to = (int32_t)from;

float to short:

  float from = value;
  int16_t to = (int16_t)from;

float to byte:

  float from = value;
  int8_t to = (int8_t)from;

long to int:

  int64_t from = value;
  int32_t to = (int32_)from;

long to short:

  int64_t from = value;
  int16_t to = (int16_t)from;

long to byte:

  int64_t from = value;
  int8_t to = (int8_t)from;

int to short:

  int32_t from = value;
  int16_t to = (int16_t)from;

int to byte:

  int32_t from = value;
  int16_t to = (int16_t)from;

short to byte:

  int16_t from = value;
  int8_t to = (int8_t)from;

The numeric narrowing conversion is performed in some of the type casts.

Binary Numeric Conversion

The binary numeric conversion is a type conversion to upgrade the type of LEFT_OPERAND or RIGHT_OPERAND of the binary operator that operands are numeric types.

The following rules apply in order.

1. If LEFT_OPERAND or RIGHT_OPERAND is the double type, OPERAND of the small type is converted to the big type using the numeric widening conversion.

2. If LEFT_OPERAND or RIGHT_OPERAND is the float type, OPERAND of the small type is converted to the big type using the numeric widening conversion.

3. If LEFT_OPERAND or RIGHT_OPERAND is the long type, OPERAND of the small type is converted to the big type using the numeric widening conversion.

4, Otherwise, both LEFT_OPERAND and RIGHT_OPERAND are converted to the int type using the numeric widening conversion.

Numeric-to-String Conversion

The numeric-to-string conversion is a type conversion from a numeric type to the string type.

  # The numeric-to-string conversion
  my $byte = (byte)1;
  my $short = (short)2;
  my $int = 3;
  my $long = 4L;
  my $float = 2.5f;
  my $double = 3.3;
  
  # The string is 1.
  my $string_byte = (string)$byte;
  
  # The string is 2.
  my $string_short = (string)$short;

  # The string is 3.
  my $string_int = (string)$int;

  # The string is 4.
  my $string_long = (string)$long;
  
  # The string is "2.5"
  my $string_float = (string)$float;
  
  # The string is "3.3"
  my $string_double = (string)$double;

String-to-byte Conversion

The String-to-byte conversion is a type conversion from the string Type to "byte Type".

  # The String-to-byte conversion
  my $string : string = "Hello";
  my $num : byte = (byte)$string;

If the string is undef, returns 0.

If not, the string is coverted to a number by the strtoll function in the C language.

The number is greater than INT8_MAX, the number is set to INT8_MAX.

The number is less than INT8_MIN, the number is set to INT8_MIN.

And returns the number.

String-to-short Conversion

The String-to-short conversion is a type conversion from the string Type to "short Type".

  # The String-to-short conversion
  my $string : string = "Hello";
  my $num : short = (short)$string;

If the string is undef, returns 0.

If not, the string is coverted to a number by the strtoll function in the C language.

The number is greater than INT16_MAX, the number is set to INT16_MAX.

The number is less than INT16_MIN, the number is set to INT16_MIN.

And returns the number.

String-to-int Conversion

The String-to-int conversion is a type conversion from the string Type to "int Type".

  # The String-to-int conversion
  my $string : string = "Hello";
  my $num : int = (int)$string;

If the string is undef, returns 0.

If not, the string is coverted to a number by the strtoll function in the C language.

The number is greater than INT32_MAX, the number is set to INT32_MAX.

The number is less than INT32_MIN, the number is set to INT32_MIN.

And returns the number.

String-to-long Conversion

The String-to-long conversion is a type conversion from the string Type to "long Type".

  # The String-to-long conversion
  my $string : string = "Hello";
  my $num : long = (long)$string;

If the string is undef, returns 0.

If not, the string is coverted to a number by the strtoll function in the C language.

And returns the number.

String-to-float Conversion

The String-to-float conversion is a type conversion from the string Type to "float Type".

  # The String-to-float conversion
  my $string : string = "Hello";
  my $float : float = (float)$string;

If the string is undef, returns 0.

If not, the string is coverted to a number by the strtof function in the C language.

And returns the number.

String-to-double Conversion

The String-to-double conversion is a type conversion from the string Type to "double Type".

  # The String-to-double conversion
  my $string : string = "Hello";
  my $num : double = (double)$string;

If the string is undef, returns 0.

If not, the string is coverted to a number by the strtod function in the C language.

And returns the number.

String-to-byte[] Conversion

The String-to-byte[] conversion is a type conversion from the string Type to "byte[] Type".

  # The String-to-byte[] conversion
  my $string : string = "Hello";
  my $bytes : byte[] = (byte[])$string;

A new byte[] object is created and all characters in the string are copied to the elements of byte[] object.

byte[]-to-string Conversion

The byte[]-to-string conversion is a type conversion from the byte[] type to the string Type.

  # byte[]-to-string conversion
  my $bytes : byte[] = new byte[3];
  $bytes->[0] = 'a';
  $bytes->[1] = 'b';
  $bytes->[2] = 'c';
  my $string : string = (string)$bytes;

A new string is created and all elements in the byte[] object are copied to the characters of the string.

Boxing Conversion

The boxing conversion is a type coversion to convert the value of numeric type to the corresponding numeric object type.

Unboxing Conversion

The unboxing conversion is a type coversion to convert the value of the numeric object type to the value of the corresponding numeric type.

Boolean Conversion

The boolean conversion converts an operand to an int value that indicates a boolean value.

This conversion is performed on the follwoing operands.

The operand of the if statement:

  if (CONDITION) {
  
  }

The operand of the unless statement:

  unless (CONDITION) {
  
  }

The second operand of the for statement:

  for (INITIALIZEATION;CONDITION;NEXT_VALUE;) {
  
  }

The operand of the while statement:

  while (CONDITION) {
  
  }

The left and right operand of the logical AND operator:

  CONDITION && CONDITION

The left and right operand of the logical OR operator:

  CONDITION || CONDITION

The operand of the logical NOT operator:

  !CONDITION

The boolean conversion returns the following value corresponding to the type of the condional operand.

If the type is the int type, return the value.

If the type is the undef, returns 0.

If the type is the value returned by the TRUE method of Bool, returns 1.

If the type is the value returned by the FALSE method of Bool, returns 0.

If the type is an integer type within int, the integer promotional conversion is performed on OPERAND.

And the following operation in the C language is performed on OPERAND .

  !!OPERAND

Compilation Errors:

The type of OPERAND of the boolean conversion must be a numeric type, an object type or an reference type or the undef type. Otherwise a compilation error occurs.

Examples:

  if (1) {
    # ok
  }
  
  if (0) {
    # not ok
  }
  
  if (1.5) {
    # ok
  }
  
  if (0.0) {
    # not ok
  }
  
  if (true) {
    # ok
  }
  
  if (Bool->TRUE) {
    # ok
  }
  
  if (false) {
    # not ok
  }
  
  if (Bool->FALSE) {
    # not ok
  }
  
  my $object = SPVM::Int->new(1);
  
  if ($object) {
    # ok
  }
  
  $object = undef;
  if ($object) {
    # not ok
  }
  
  my $value = 1;
  my $ref = \$value;
  
  if ($ref) {
    # ok
  }
  
  if (undef) {
    # not ok
  }

Runtime Type Assignability

The runtime type cheking is the type cheking that is performed at runtime.

The type cast operators that operand is an object type performe the runtime type checking by the rule of the runtime type assignability.

The runtime type assignability is the assignability at runtime.

The isa operator checks the "Runtime Type Assignability" in runtime type assignability

The runtime type assignability is false, an exception is thrown.

If the type of the distribution is an object type and the type of the source is undef, the runtime type assignability is true.

If the type of the distribution is the same as the type of the source, the runtime type assignability is true.

If the type of the distribution is the any object type object and the type of the source is an object type, the runtime type assignability is true.

If the type of the distribution is the any object array type object[] and the type of the source is an object array type, the runtime type assignability is true.

If the type of distribution is an class type, an class array type, an class multi-dimensional array type and the dimention of the type of the distribution is the same as the dimention of the type of the source and the basic type of distribution is a super class of the basic type of the source, the runtime type assignability is true.

If the type of distribution is an interface type, an interface array type, an interface multi-dimensional array type and the dimention of the type of the distribution is the same as the dimention of the type of the source and the basic type of distribution has the interface of the basic type of the source, the runtime type assignability is true.

Runtime Type AssignabilityToFrom
TrueOBJECT_Xundef
TrueOBJECT_XOBJECT_X
TrueobjectOBJECT_Y
Trueobject[]OBJECT_ARRAY_Y
TrueSUPER_CLASS_XCLASS_Y
TrueSUPER_CLASS_X[]CLASS_Y[]
TrueSUPER_CLASS_X[]..[]CLASS_Y[]..[]
TrueINTERFACE_XINTERFACE_HAVING_Y
TrueINTERFACE_X[]INTERFACE_HAVING_Y[]
TrueINTERFACE_X[]..[]INTERFACE_HAVING_Y[]..[]
FalseOBJECT_XOTHER

([]..[] means two or more [])

Type Comment

The type comment syntax is supported. The type comment can be written after of keyword.

  TYPE of TYPE
  TYPE of TYPE1|TYPE2
  TYPE of TYPE1|TYPE2|TYPE3

The type comment can be used the type of the field decralation, the class variable definition, the local variable declaration, and the return value and the types of arguments of the method definition.

  has points : List of Point;
  
  our $POINTS : List of Point;
  
  my $points : List of Point;
  
  static method foo : List of Point ($arg : List of Point) { ... }
  
  my $replace : object of string|Regex::Replacer;

Type comments have no meanings at runtime.

Compilation Errors:

If the type specified as the type comment is not found, a compilation error occurs.

Type Width

The type width is the required length of the runtime stack for the type.

If the type is a multi-numeric type, it is the length of the fields, owhterwise it is 1.

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License