Typed arrays
Typed arrays hold a unique type of objets. They are very more efficient than
common arrays because they are indexed and not associative and hold
directly 32 bits or 64 bits objects or pointers rather than dynamic
variables. A typed array of integer is 100 times faster than a standard one,
according to the test included in the distribution.
(This is for binary executables, there is no difference in a Php target.)
Typed arrays are dynamic also, you are not limited by the size, and
boundary overflow is controled.
Currently, three types of typed array exists, array of int, of text and of
objects.
Constructor and literal
The constructor of a typed array has the form:
type(...elements...)
The number of element is between 0 and n.
When there is one element, there is no difference between a constructor
of typed array and the constructor of the scalar:
int(1)
text("text")
This is not a problem at assignment as we can create an array from a scalar,
but inside an expression this is ambiguous.
Inside an expression we must use a literal array instead:
type{...elements...}
The curly braces mean for "array of".
Array of int
The syntax is:
int[] i = int(x, y, ....)
Once it is declared, it is used as a common mixed array, but only
integer numbers may be added to the array.
If you use a single argument: int(10), for example, this can be assigned
either to a variable or to an array of int. At declaration the left side
of the assignement determines the type of constructor at right side. In
an expression a such constructor is a scalar. You must use a literal.
Examples:
int[] x = int(5) int[] x = int(1,2) int[] x = y + int{5}
Array of texts
The declaration is:
text[] t = text(a, b, "demo", ...)
Same rules apply to text array.
Array of real, natural, number
The declarations are:
real[] r = real(1.0, 2.0, etc...) natural[] n = natural(1, 2, etc...) number[] n = number(1, 2.0, ...)
Array of objects
The declaration is:
class someclass
...
/class
someclass object1
someclass object2
object[] = object(object1, object2, ...)
Methods
Virtual methods of typed arrays are identical to that of mixed arrays.
See above.
Assignment and conversion
- You can assign a typed array to a mixed array:
Ex:
int[] i = int(... array a = i
- You can't, of course, assign a mixed array to a typed array since the
values may have different types, unless you push the elements one by one.
A literal array with prefix is considered to be a mixed array. Thus you
can't assign it to a typed array.
Ex: (1,2,3) ... this is a mixed array even with integer items.
int[] i = (1,2,3) ... NOT VALID you must write instead:
int[] i = int(1,2,3) ... valid.
- You can also create a mixed array from a typed one:
array a = array(i)
- You can't create a mixed array with a constructor of typed array:
array a = int(1,2) ... NOT VALID
array a = array(int(1,2)) ... NOT VALID
In both cases, this is useless.
Using typed array with dyn
You can assign a typed array to a dyn.
int[] i = int(1,2)
dyn d = dyn(i)
Two new dyn methods are provided to use a such dyn:
arrayType() return the type of the typed array assigned to a dyn.
toList(int = 0) return a typed array assigned to a dyn.
The parameter is the type (see below) and is required only
if the dyn hold a classical array we want return as
a typed one.
Types currently recognized are:
$TYPE_INT array of integers
$TYPE_TEXT
$TYPE_REAL
$TYPE_NATURAL
$TYPE_NUMBER
$TYPE_OBJECT
Example of use:
d = dyn(int(1,2))
if d.arrayType()
= $TYPE_INT:
int[] i = d.toList()
= $TYPE_TEXT:
text[] t = d.toList()
/if
Random assignment
For compatibility with PHP, adding an element to an array outside the
boundary, has same effect than pushing it on top of the array.
For example:
int[] i = nil
i[10] = 10
this is equivalent to:
i.push(10)
Pushing values is the right way to fill an array in fact.
If you want really putting element at fixed position, you have to fill the
array with some values...
for int k in 0 .. 10 let i.push(0)
you can now put an element at the 10 indice.
Exemple | int
x = 20 int y = 50 print (x * 10) + (y / 5) |
Affiche: | > |
Exercises |
None |