IIndexable INTERFACE

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.

All Members


TypeNameSignature
methodatpublic var at(var)
methodatpublic void at(var, var)
methodsizepublic int size()

Methods


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

  • index An index at which the internal value is to be retrieved.

Returns

  • The value retrieved.

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

  • index An index at which the given value is to be set.
  • value The value to set.

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

  • The size of the indexed scope. Typically a non-negative value, but this is not required.