Dealing with property function and operator overloads

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 3 12:50:44 PDT 2011


On Mon, 03 Oct 2011 14:57:33 -0400, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> Looks like I can use some D tricks for this:
>
> import std.stdio;
>
> struct Point
> {
>     int x, y;
>
>     void opOpAssign(string op)(int rhs)
>     {
>         mixin ("x = x " ~ op ~ " rhs;");
>         mixin ("y = y " ~ op ~ " rhs;");
>     }
> }

Probably slightly off topic, but be very careful with operator overloads  
without using constraints.

For example, I can do some weird things to your struct:

Point p;

p.opOpAssign!"*x; y+="(5);

I suspect operator overloads are going to be a large hole in the interface  
design of many objects, but at least they won't be exploitable once  
compiled.

-Steve


More information about the Digitalmars-d-learn mailing list