The interface to add the capability of accessing to stored values by an index.
A user-defined object may fully or partially function as a container with vector semantics. Julian supports access to such objects using the built-in indexer syntax. A user may write code like below to access a customized indexable object: class Vector : IIndexable {
private Object[] _objs;
Vector(int size){
_objs = new Object[size];
}
var at(var index) {
return _objs[index];
}
void at(var index, var value) {
_objs[index] = value;
}
int size() {
return _objs.length;
}
}
Vector vector = new vector(3);
for (int i = 0; i < obj.size(); i++) {
vector[i] = new Object(); // engine calls vector.at(var index, var value)
var ele = vector[i]; // engine calls vector.at(var index)
}
The index doesn't have to be an integer. The user may very well leverage this interface to implement an associative array or dictionary.
Type | Name | Signature |
---|---|---|
method | at | public var at(var) |
method | at | public void at(var, var) |
method | size | public int size() |
public var at(var index)
Get the value at specified index.
This method is the backup logic for get-by-indexer syntax on arbitrary objects, as shown in the example of interface summary.
Parameters
Returns
Throws
public void at(var index, var value)
Set the value at specified index.
This method is the backup logic for set-by-indexer syntax on arbitrary objects, as shown in the example of interface summary.
Parameters
Throws
public int size()
Get the size of the indexed scope. Assuming thread-safety measure is in place and an ordinary numeral indexing is applied, a user should expect to successfully call the indexer with an integer between 0 and size - 1, inclusive.
Implementing this method is optional, as the engine would not pro-actively call it. It's a good practice to have it implemented so that the object can be iterated in the classic for-by-index loop.
Returns