Is it possible to handle 'magic' property assignments a'la PHP?
Jacob Carlborg
doob at me.com
Tue Jan 7 23:32:15 PST 2014
On 2014-01-07 21:44, H. S. Teoh wrote:
> If you have a good motivating use case in favor of this addition that
> can be used in a DIP, I'd vote for it.
I'm usually not good at these arguments. I mean, it would be nice to
have but I don't have any strong arguments for it. It's just syntax sugar.
> I like the alias idea, so here's the revised proposal:
>
> 1) Argumentless trailing-delegate syntax:
>
> // Given this declaration:
> void foo(alias dg)();
>
> // We can write this:
> foo {
> // body
> }
>
> // which will get translated into:
> foo!({ /* body */ });
>
> 2) With arguments:
>
> // Given this declaration:
> void foo(alias dg, A...)(A args);
>
> // Or its non-template equivalent:
> void foo(alias dg)(A arg1, B arg2, C arg3, ...);
>
> // We can write this:
> foo(a,b,c,...) {
> // body
> }
>
> // which gets translated into:
> foo!({ /* body */})(a,b,c,...);
>
> 3) With indexing arguments:
>
> // Given this declaration:
> void foo(alias dg, I..., A...)(A args)
> if (is(typeof(dg(I))));
>
> // Or its non-template equivalent:
> void foo(alias dg)(A arg1, B arg2, C arg3, ...) {
> ...
> dg(i, j, k);
> ...
> }
>
> // We can write this:
> foo(i,j,k,... ; a,b,c,...) {
> // body
> }
I would prefer to have the delegate arguments last.
> // which gets translated into:
> foo!((i,j,k,...) { /* body */ })(a,b,c,...);
>
>
> EXAMPLE:
>
> void for_every_other(alias loopBody, R)(R range)
> if (is(typeof(loopBody(ElementType!R.init))))
> {
> while (!range.empty) {
> loopBody(range.front);
> range.popFront();
> if (!range.empty)
> range.popFront();
> }
> }
>
> // Prints:
> // ---
> // 1
> // 3
> // 5
> // ---
> for_every_other (i; [1,2,3,4,5,6]) {
> writeln(i);
> }
If we instead have the delegate argument last UFCS still works:
[1,2,3,4,5,6].for_every_other(i) {
writeln(i);
}
Hmm. Actually, your example is more D like. I don't know which I example
I like best.
I'll see if I can write something down.
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list