Why Ruby?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Dec 12 10:23:03 PST 2010


On 12/12/10 6:44 AM, Jacob Carlborg wrote:
[snip]
> Conclusion:
>
> D needs a better and nicer looking syntax for passing delegates to
> functions.
>
> Suggestion:
>
> If a function takes a delegate as its last parameter allow the delegate
> literal to be passed outside the parameter list, after the function
> call, and allow to drop the semicolon. Also allow type inference of the
> delegate parameters. Looking like this:
>
> foo(bar) {
> // do something with bar
> }
>
> If the function takes other arguments before the delegate I have two
> suggestions, either just have two parameter lists, each in its own pair
> of parentheses:
>
> foo(3, 'a')(bar) {
> // do something with bar
> }
>
> Or have two parameter lists in one pair of parentheses seperated by a
> semicolon:
>
> foo(3, 'a' ; bar) {
> // do something with bar
> }
>
> I think that the syntax I've used in these examples has previously been
> proposed.

Yah, it's been discussed a couple of times in the past. Both Walter and 
myself are favorable to finding a good, simple lowering that improves 
syntax without adding burden to the person who learns the language.

By the way, lowerings are great. Defining features as lowerings is a 
huge win in language definition, ease of understanding, and correctness 
of implementation. Features that have been implemented through lowering 
have had very few bugs - e.g. scope() and the new operator overloading, 
which was only a week's work.

Getting back to finding a good lowering, consider:

foreach (a, b ; c) stmt

A Ruby syntactic equivalent that clarifies what belongs to the block and 
what belongs to the invoker of the block is:

c.each do |a, b|
   stmt
end

So a and b are passed to the block and each is a method of c (or more 
general, each is a function called taking c as an argument). Going now 
back to D, we can imagine the following lowering:

fun (a, b ; c) stmt

=>

fun(c, (a, b) { stmt })

This could and should be generalized for more parameters, which I'm sure 
is very useful:

fun (a, b ; c, d) stmt

=>

fun(c, d, (a, b) { stmt })

Of course "fun" could be actually "obj.method".

With this we have a compelling syntax that has semantics obtained via 
lowering.


Andrei


More information about the Digitalmars-d mailing list