opIndex operators
gedaiu
szabobogdan at yahoo.com
Sat Apr 13 23:57:33 PDT 2013
On Saturday, 13 April 2013 at 11:41:02 UTC, bearophile wrote:
> gedaiu:
>
>> What i want to create, is an array structure like the one from
>> PHP where array levels are not fixed and the sintax to access
>> rhe values is val[][] so returning a reference to a struct
>> that have the same type as the current type is useful.
>>
>> there is a way to do this in D?
>
> Instead of returning float as in my case, you return something
> else that has opIndex and opIndexAssign. This way you can pile
> up the []:
>
>
> import std.stdio;
>
> struct Foo {
> ref Foo opIndex(in size_t i) {
> writeln("opIndex: ", i);
> return this;
> }
>
> void opIndexAssign(in float value, in size_t i) {
> writeln("opIndexAssign: ", value, " ", i);
> }
> }
>
> void main() {
> Foo f;
> f[1] = 2;
> f[3][4] = 5;
> f[6][7][8] = 9;
> }
>
>
> That outputs:
>
> opIndexAssign: 2 1
> opIndex: 3
> opIndexAssign: 5 4
> opIndex: 6
> opIndex: 7
> opIndexAssign: 9 8
>
> Bye,
> bearophile
Value[Value] container;
ref Value opIndex(Value index) {
return container[index];
}
why I get this error?
Error: function base.Value.Value.opIndex (Value index) is not
callable using argument types (string)
Error: cannot implicitly convert expression ("string") of type
string to Value
I have implemented this methods:
this(string)
Value opCast(string val)
Value opAssign(ref const string val)
Thanks!
More information about the Digitalmars-d-learn
mailing list