Operator Overloading
C3 allows some limited operator overloading for working with containers.
”Element at” operator []
Implementing []
allows a type to use the my_type[<value>]
syntax:
It’s possible to use any type as argument, such as a string:
Only a single [] overload is allowed.
”Element ref” operator &[]
Similar to [], the operator returns a value for &my_type[<value>]
, which may
be retrieved in a different way. If this overload isn’t defined, then &my_type[<value>]
would
be a syntax error.
”Element set” operator []=
The counterpart of [] allows setting an element using my_type[<index>] = <value>
.
”len” operator
Unlike the previous operator overloads, the “len” operator simply enables functionality
which augments the []
-family of operators: you can use the “from end” syntax e.g my_type[^1]
to get the last element assuming the indexing uses integers.
Enabling ‘foreach’
In order to use a type with foreach, e.g. foreach(d : foo)
, at a minimum methods
with overloads for []
(@operator([])
) and len
(@operator(len)
) need to be added.
If &[]
is implemented, foreach by reference is enabled (e.g. foreach(double* &d : foo)
)