alias to a property as an argument to a mixin template

monarch_dodra monarchdodra at gmail.com
Mon Sep 24 04:40:00 PDT 2012


On Monday, 24 September 2012 at 10:53:47 UTC, comco wrote:
>
> Yes, I don't claim that - the last thing is just nasty. It's 
> just playing with constructs seeing how far can we go. By the 
> way, now I use the reassign function all over the place! I'm 
> also very glad this discussion ends with a (remotely connected) 
> bug report!

Oh. I think I just got what your "reassign" was meant to do. I 
think I see it now. I guess it does work nice actually.

I'd say that unfortunately, your *new* reassign is making the 
assumption that it is possible to grab a reference of the passed 
in objects. It will fail with "write properties", that need to 
evaluate different functions depending on read context and write 
context:

--------
import std.stdio;

void reassign(A...)(ref A a) { //L3
     static if (A.length > 1) {
         a[0] = a[1];
         reassign(a[1 .. $]);
     }
}

struct S
{
     private int i;

     @property int x(){return i;}
     @property int x(int v){return i = v;}
}

void main()
{
     S a = S(1);
     S b = S(2);
     S c = S(3);

     a.x = b.x;
     b.x = c.x;
     c.x = 4;
     writefln("%s %s %s", a.x, b.x, c.x);

     reassign(a.x, b.x, c.x, 5); //L27
     writefln("%s %s %s", a.x, b.x, c.x);
}
--------
main.d(3): Error: template main.reassign(A...) cannot deduce 
template function from argument types !()(int,int,int,int)
--------

You could get away with it with some sort of template string 
mixin:
         mixin ReassignMixin!("a.x", "b.x", "c.x", 5");
         reassign();

The problem with this approach though, is that it only works once 
per scope... and really, it is not much more than a glorified 
macro.


More information about the Digitalmars-d-learn mailing list