New in C#4

bearophile bearophileHUGS at lycos.com
Wed Oct 29 13:59:19 PDT 2008


Jarrett Billingsley:

> Er, what?  They are exactly the same size..

In standard Python coding style usually in function calls with named arguments you don't put a space after the equal:
foo(x=10)

But I have just seen that after a comma people (like me) usually put a space:
foo(x: 10)

So the second line is a char longer. But I agree it's a not unimportant detail.


> Besides, = is already an expression in D.  f(x = 5) has a well-defined
> meaning already.

The following D code gives a syntax error:

import std.stdio: writefln;
int f(int x) { return x + 10; }
void main() {
    writefln( f(x=5) );
}

While this works, and assigns x to 5 in the main:

import std.stdio: writefln;
int f(int x) { return x + 10; }
void main() {
    int x;
    writefln( f(x=5) ); // Output: 15
    writefln(x); // Output: 5
}

In Python x = 5 is never an expression (this precisely to avoid the typical bug of C code of using = instead of ==).
And the named arguments don't change the value of the variable in the calling frame:

x = 10
f(x=5)
print x # Output: 10

Overall I agree than the syntax with the colon is probably better for D.
(While a syntax that I don't like much is the .. instead of : to define an array slice).

Bye,
bearophile



More information about the Digitalmars-d mailing list