On 2010-12-14 19:33, Stephan Soller wrote:

I think it's a matter of consistency. In Ruby blocks are used all the
time for pretty much everything. In D this isn't the case because
usually templates are used for stuff where blocks are used in Ruby (e.g.
map, group and find in std.algorithm).

I think that the templates that take a string as a predicate is just an
ugly hack because D has a too verbose delegate syntax.


I absolutely agree with that. However I don't have a better idea how to
write it. Delegate syntax is already fairly compact in D.

For example code as this isn't very uncommon in Ruby:

[1, 2, 3, 4, 5].select{|e| e > 3}.collect{|e| e*e}

Of course this is a simplified example. Usually the collection would
contain some objects and the blocks would filter for a method call (like
"e.even?"). However I built a small D1 struct that implements a select
and collect function. With the unified function call synatax for array
with code should actually work:

[1, 2, 3, 4, 5].select((int e){ return e > 3; })
.collect((int e){ return e*e; });

It's already fairly compact. The main overhead is the parameter type and
the return statement. I don't believe this can be reduced any more
without breaking consistency of the language.

Probably not, but, for example, Scala allows very compact delegate literals:

Array(1, 2, 3, 4, 5).select(_ > 3).collect(_ * _)

Or more verbose:

Array(1, 2, 3, 4, 5).select((x) => x > 3).collect((x, y) => x * y)

I'm not 100% sure I that the syntax is correct.

Delegates are way more verbose in other languages like PHP and
JavaScript (more or less the same syntax in both languages). Despite
that it's no problem to use it and in case of jQuery it's used very
often. I think the main reason why delegates are not used like that in D
is performance. I'm really not sure about it but I suspect that
delegates are less effective than code directly generated by a template
like map!(). I don't know how efficient templates can integrate
delegates so I just suspect that this is a performance problem.

Happy programming
Stephan Soller

PHP has a very verbose delegate syntax with explicit closures, one of the many reasons I don't use PHP. JavaScript has quite similar syntax as D but the "function" keyword is required, I try to use CoffeeScript (compiles to javascript), which has a lot nicer delegate syntax, as often I can.

D(dmd) needs to be able to inline delegates.

--
/Jacob Carlborg

Reply via email to