Using opDispatch as *magic* getter/setter. Possible or not?

Aleksandar Ružičić ruzicic.aleksandar at gmail.com
Thu Mar 31 07:58:44 PDT 2011


On Thu, Mar 31, 2011 at 1:30 PM, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
>
> or you can change the template parameters in the opDispatch setter:
>
> @property ref ConfigSection opDispatch(string sectionName, T : string)(T
> arg)
>
> -Steve
>

Thanks, that's much more readable I now have these templates:

// getter
@property ref ConfigEntry opDispatch(string entryName)() pure {
    if (!(entryName in entries)) {
        entries[entryName] = new ConfigEntry(config);
    }
    return entries[entryName];
}

// setter
@property void opDispatch(string entryName, T : string)(in T value) {

    if (!(entryName in entries)) {
        entries[entryName] = new ConfigEntry(config);
    }

    entries[entryName] = value;
}

And that compiles just fine (I dunno if it runs ok since I'm currently
on windows and I've been messing with my setup so optlink can't stop
complaining.. sigh..)

but due to bug not allowing foo.bar = "abc"; I've also added opIndex
and opIndexAssign (apparently you cannot only have opIndexAssign
defined) as temporary "backup" solution..


More information about the Digitalmars-d-learn mailing list