Sparse Aggregate Assignment/Initialization (RAII)

Justin Whear via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 7 14:49:22 PDT 2014


On Mon, 07 Jul 2014 21:34:05 +0000, Nordlöw wrote:

> However using this function through UFCS
> 
>      auto cx = new C().set!"x"(11);
> 
> fails as
> 
> algorithm_ex.d(1257,17): Error: template algorithm_ex.set cannot deduce
> function from argument types !("x")(C, int), candidates are:
> algorithm_ex.d(1242,7):        algorithm_ex.set(string member, T,
> U)(ref T a, in U value) if (isAggregateType!T && hasMember!(T,
> member))
> 
> Instead I have to use
> 
>      auto c = new C(); set!"x"(c, 11);
> 
> which is not as elegant.
> 
> Why doesn't UCFS work here and is there a solution using DMD git master?

You need to use `auto ref` to have this work with both classes and 
structs.  A working version of your code here: auto dx = D().set!"x"(11);
	assert(dx.x == 11);

On Mon, 07 Jul 2014 21:34:05 +0000, Nordlöw wrote:
> Further Is there a cleverer way to do this without resorting to mixins?

__traits(getMember, ...) is useful here, see this version of your code: 
http://dpaste.dzfl.pl/75e03fbec020


More information about the Digitalmars-d-learn mailing list