Setter chaining

DigitalDesigns DigitalDesigns at gmail.com
Wed May 30 21:57:21 UTC 2018


The above idea can be emulated in code, abiet ugly and useless:

https://dpaste.dzfl.pl/bd118bc1910c




import std.stdio;

struct CT(A,B)
{
     A v;
	B t;
	alias v this;

     B opUnary(string s)() if (s == "~")
     {
         return t;
     }
	
	A opUnary(string s)() if (s == "*")
     {
         return v;
     }
	
}


class C
{
	int q;
	CT!(int, typeof(this)) foo() { q = 3; CT!(int, typeof(this)) v; 
v.t = this; v.v = q; return v; }
	CT!(int, typeof(this)) bar(int y) { q = q + y; CT!(int, 
typeof(this)) v; v.t = this; v.v = q; return v; }
}


void main()
{
	C c = new C();
	
	auto x = *((~c.foo()).bar(6));
	
	
	
	writeln(x);
}

With a language implementation, all one would need is a symbol, 
using #,

everything would simplify to


class C
{
	int q;
	int foo() { q = 3; return q; }
	int bar(int y) { q = q + y; return q;}
}

auto x = c.#foo().bar(6);






More information about the Digitalmars-d-learn mailing list