Indexer (programming)
In-game article clicks load inline without leaving the challenge.
In object-oriented programming, an indexer allows instances of a particular class or struct to be indexed just like arrays. It is a form of operator overloading.
Implementations
C++
In C++ one can emulate indexing by overloading the [] operator. The expression a[b...] translates to a call to the user-defined function operator[] as (a).operator[](b...). Here is an example,
C#
Indexers are implemented through the get and set accessors for the operator[]. They are similar to properties, but differ by not being static, and the fact that indexers' accessors take parameters. The get and set accessors are called as methods using the parameter list of the indexer declaration, but the set accessor still has the implicit value parameter.
Example 1
Example 2
Here is a C# example of the usage of an indexer in a class:
Usage example:
In this example, the indexer is used to get the value at the nth position, and then to get the position in the list referenced by its value. The output of the code is:
PHP
In PHP indexing can be implemented via the predefined ArrayAccess interface,
Python
In Python one implements indexing by overloading the __getitem__ and __setitem__ methods,
Rust
Rust provides the std::ops::Index trait.
Smalltalk
In Smalltalk one can emulate indexing by (e.g.) defining the get: and set:value: instance methods. For example, in GNU Smalltalk,