Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d2: What are semantics of opDot?

Tags:

d

I've met several mentions about opDot method, that allows to overload member access aka dot operator, but official documentation for it is missing. It's surely not dropped out, as std.typecons.Unique makes use of it.

Does anybody know, how opDot can be used, and why there is no documentation about it?

like image 213
toriningen Avatar asked Mar 26 '12 21:03

toriningen


1 Answers

opDot has been scheduled for deprecation. That's why it isn't documented. Don't use it. Use alias this instead. You can use it to alias a particular type or function to a type so that it can act like that type. e.g.

struct S
{
    int value;
    alias value this;
}

will make it so that a variable of type S will implicitly convert to int using S's value field. You can also alias functions that way:

struct S
{
    int get()
    {
        return 7;
    }

    alias get this;
}

though that can be more limiting, since dmd does not currently support having multiple alias thises for a type (it should eventually though). In this case, you can then implicitly cast S to a an int, but not the reverse. Regarldess, alias this is intended for implementing implicit conversions.

If alias this isn't quite what you want, another possibility is opDispatch. It allows you to transform what's on the right-hand side of the dot into other stuff (e.g. turn all calls to foo into bar). But, between those two, you should be able to do pretty much anything you were thinking of doing with opDot (and a lot more besides).

like image 131
Jonathan M Davis Avatar answered Nov 02 '22 20:11

Jonathan M Davis