Dynamic language

Nick Sabalausky a at a.a
Thu Mar 15 22:10:00 PDT 2012


"Adam D. Ruppe" <destructionator at gmail.com> wrote in message 
news:bighgnfjujyisywpceqt at forum.dlang.org...
>
> import std.variant;
> struct Extendable {
>     Variant[string] properties;
>     Variant opDispatch(string name)() {
>        return properties[name];
>     }
>     Variant opDispatch(string name, T)(T t) {
>        properties[name] = t;
>        return properties[name];
>     }
> }
>
> void main() {
>     auto a = Extendable();
>     a.sweet = 10;
>     a.cool = { a.sweet = a.sweet + 1; } // could probably fix ++ too..
>     // fails with std.variant but the language *could* do it
>     a.cool(); // exercise for the reader: maek this work! Gotta add 
> opCall.
> }
>

I'm compelled to make a slight tweak to that:

import std.variant;
struct Extendable
{
    Variant[string] properties;
    Variant opDispatch(string name)() {
       return properties[name];
    }
    Variant opDispatch(string name, T)(T t) {
       properties[name] = t;
       return properties[name];
    }

    // Tweak Start
    void crystal()
    {
        this.wild = true;
        this.sweet = true;
        this.cool = true;
    }
    // Tweak End
}




More information about the Digitalmars-d mailing list