On Thu, Jan 09, 2014 at 09:49:17AM +0100, Jacob Carlborg wrote: > On 2014-01-08 19:04, H. S. Teoh wrote: [...] > >In fact, we can already almost get the desired syntax in the current > >language: > > > > /* Current D already supports this: */ > > range.my_foreach!((i,j) { > > /* body */ > > }); > > Almost ;) > > >which isn't that much different from the proposed syntactic sugar: > > > > range.my_foreach(i,j) { > > /* body */ > > } > > > >We're just saving on the '!', ';', and an extra pair of parentheses. > > It quickly get clumsy when you need to pass regular arguments to the > function: > > void foo (alias dg) (int a, int b); > > foo!((i, j) { > // body > })(1, 2); > > Not pretty.
True. So this should be one of the motivating use cases for our DIP. > >I guess the only real advantage is that we get to imitate built-in > >foreach syntax. E.g., if we use the form with arguments but no > >indexing arguments, we can pretend to be an if-statement: > > > > // (Whatever "dynamic if" means...) > > void dynamic_if(alias dg)(bool cond) > > if (is(typeof(dg()))) > > { > > // Haha, we're just wrapping the built-in 'if' cuz we > > // can. > > if (cond) dg(); > > } > > > > int x; > > dynamic_if (x==0) { > > writeln("Boo yah!"); > > } > > > >Or if we use the argumentless form to implement custom block constructs: > > BTW, what do you think about not needing braces if the delegate body > only contains a single statement, like with regular statements: > > dynamic_if (x==0) > writeln("foo"); > > With the alias syntax, I'm wondering if the compiler will have any > problem with that you can pass almost anything to an alias parameter > and not just a delegate. [...] I'm afraid that this might become ambiguous, for example: int* gun(...) {...} func (x==0) *gun(y); Does the second statement mean `func!(() => *gun(y))(x==0)`, or does it mean `func(x==0) * gun(y)`? While it's not hard to disambiguate this semantically, it means it's impossible to parse before you analyze it, which is probably a bad idea. T -- Nobody is perfect. I am Nobody. -- pepoluan, GKC forum