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

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


On Thu, Mar 31, 2011 at 8:52 AM, Jacob Carlborg <doob at me.com> wrote:
>
> Or, I think this will work as well:
>
> @property ref ConfigSection opDispatch(string sectionName, Args ...)(Args
> args) if (Args.length == 0)
> {
> // getter
> }
>
>
> @property ref ConfigSection opDispatch(string sectionName, Args ...)(Args
> args) if (Args.length == 1)
> {
> // setter
> }
> }
>
> auto foo = new Foo;
>
> foo.bar; // works
> foo.bar = 3; // currently does not work, bug
> foo.bar(3); // works
>
> --
> /Jacob Carlborg
>

That's great! Thanks! If just the assignment worked thought, it would
be perfect..
Btw, do you know which issue # that is? I'd like to read more about
that bug but can't find it on bugzilla.


On Thu, Mar 31, 2011 at 3:50 AM, spir <denis.spir at gmail.com> wrote:
>
> Agreed. And I would really have an answer to your question, since I tried to
> do the same thing. Don't understand why D does not have an 'opMember' or
> 'opDot'. Someone knows?
> This would be one of the first metamethods I would introduce in a language
> (definitely before operator overloading).
>

Yeah, opMember would be great, but it seems that we'll be able to use
opDispatch for that in a way Jacob described once the bugs are ironed
out..
But until then I'll have to live with "ugly" indexing sytnax...


More information about the Digitalmars-d-learn mailing list