Super-dee-duper D features
James Dennett
jdennett at acm.org
Mon Feb 12 20:16:41 PST 2007
[With apologies for following on from my own post:]
James Dennett wrote:
> Walter Bright wrote:
>> kris wrote:
>>> Walter Bright wrote:
>>>> 3) Less code == more productivity, less bugs. I don't mean
>>>> gratuitously less code, I mean less code in the sense that one can
>>>> write directly what one means, rather than a lot of tedious bother.
>>>> For example, if I want to visit each element in an array:
>>>>
>>>> foreach(v; e)
>>>> {...}
>>>>
>>>> is more direct than:
>>>>
>>>> for (size_t i = 0; i < sizeof(e)/sizeof(e[0]); i++)
>>>> { T v = e[i];
>>>> ... }
>>>>
>>> Yep, that's great! One of the reasons I like D so much, along with
>>> array slicing.
>> The C++ version is even *worse* than the C one (for wordiness bother):
>>
>> for (std::vector<T>::const_iterator i = e.begin(); i != e.end(); i++)
>> { T v = *i;
>> ... }
>
> C++ can, of course, also do (with type-safety)
> for (size_t i = 0; i < size(e); ++i)
>
>> I mean I know the reasons for every bit of the syntax there, and in
>> isolation they make sense, but put it all together and it seems to go
>> backwards.
>
> C++, of course, has std::for_each(e.begin(), e.end(), do_x);
> in its library (though that's weaker than it could be because
> of lack of support for convenient anonymous functions/lambdas).
>
> C++0x is very likely to have for(v: e). It's implemented
> in ConceptGCC already. Java already has essentially that,
> as does C#. This really doesn't set D apart (but at least
> D isn't falling behind here).
For completeness (and maybe incidentally illustrating how
easy it is to miss something relevant) I should mention
that the current working paper for C++ also supports type
deduction to allow
for (auto i = e.begin(); i != e.end(); ++i)
which is a big step forward (though in this simple case,
the range form of "for" will still be the better/normal
choice.) Giving "auto" some use in this way will remove
a lot of verbose repetition from C++ code -- but it's
good that D benefits from some of the points learned
during the evolution of C++.
(I hope it can learn from the excessive implicit conversions
in C++ too, and eliminate implicit conversions from character
and boolean types to integers.)
-- James
More information about the Digitalmars-d
mailing list