Why Ruby?

Stephan Soller stephan.soller at helionweb.de
Tue Dec 14 10:33:23 PST 2010


>>
>> 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.

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


More information about the Digitalmars-d mailing list