New in C#4
Denis Koroskin
2korden at gmail.com
Wed Oct 29 13:03:13 PDT 2008
On Wed, 29 Oct 2008 17:37:25 +0300, bearophile <bearophileHUGS at lycos.com>
wrote:
> Thanks to Reddit I have found a nice short document that lists some of
> the differences of C#4:
> https://docs.google.com/View?docid=dcj4xk6_17ffc7nmgv
>
> They have added a dynamic invocation, useful if you want to implement a
> dynamic language (like IronPython, IronRuby, etc) on top of the dotnet.
> Object-C shows this is doable in a very C-like language, and the Boo
> language shows that in a statically typed language it can be useful to
> use a Duck type once in a while to reduce the "static type pressure".
> More info on this last concept in the Boo site.
>
> Something that I like a lot is the named arguments, that I hope to see
> in D and Delight someday. They are used often in Python, and they help
> increase the readability of the code, sometimes even reducing mistakes.
> They have used colons:
> foo(x: 1, z: 3)
> While Python uses equal signs:
> foo(x=1, z=3)
> I think they are about equally readable.
> (I think there's a problem with named arguments, that you can solve in
> some ways, for example with an extra hidden bitfield argument that
> encodes what arguments are given and what not).
>
> Bye,
> bearophile
I'm waiting for named arguments, too! They make could more readable and
self-documenting:
graphicsDevice->updateSettings(true, true, false, true); // wtf??
Compare to the following:
graphicsDevice->updateSettings(fullScreen: true, verticalSync: true,
enableBloom: false, fullScreenAA: true);
I once had the following Color class:
class Color
{
this (byte alpha, byte red, byte green, byte blue) { ... }
}
and used it as follows:
Color c = new Color(255, 0, 255, 0); // opaque green color
After refactoring, alpha became last argument in ctor:
this(byte red, byte green, byte blue, byte alpha) { ... }
Note that this change didn't raise any compilation error, nothing that
would notify my of possible error.
Needless to say that I didn't enjoy searching all the code that used Color
to reorder parameters (although some artifact were nice :)
It would save me quite some time if I initialized all my instances as
follows:
Color c = new Color(alpha: 255, red: 0, green: 255, blue: 0);
Now compiler could raise an error if parameters order was changed. Even
better - compiler could reorder parameters for me automatically if given
that all of them are specified and no params ommited (unlike Python). This
makes code more robust to refactoring.
More information about the Digitalmars-d
mailing list