Is it possible to handle 'magic' property assignments a'la PHP?

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jan 7 07:58:43 PST 2014


On Tue, Jan 07, 2014 at 03:35:43PM +0100, Jacob Carlborg wrote:
> On 2014-01-07 13:22, Philippe Sigaud wrote:
> 
> >What about:
> >
> >void loop(void delegate() dg);
> >
> >loop({
> >...
> >
> >});
> >
> >Since any block is a void delegate().
> 
> That's what we have now, and that doesn't look like a built-in
> statement ;)
[...]

Y'know, I've always wanted "trailing delegate syntax":

	func(x, y, z; p, q, r) {
		// body
	}

gets translated into:

	func(p, q, r, (x, y, z) => /* body */);

Since we already have UFCS, which translates a leading fragment into the
first argument (x.func(y) --> func(x,y)), it seems perfectly reasonable
to do something with the final argument too, like the above.

This would allow one to implement, for example, foreach_reverse as a
library function instead of a language keyword:

	void foreach_reverse(I, R)(R range, void delegate(I) dg)
	{
		...
		dg(idx);
		...
	}

	// Gets translated to:
	//	foreach_reverse(range, (uint i) => /* body */);
	foreach_reverse (uint i; range) {
		... // body
	}

	// And you can use UFCS too:
	range.foreach_reverse(uint i) {
		... // body
	}

I'm not holding my breath on this one, though. It's a rather big change
and ultimately is just syntactic sugar. Maybe it can go on the list of
features for D3... ;-)


T

-- 
Famous last words: I wonder what will happen if I do *this*...


More information about the Digitalmars-d-learn mailing list