Questions about syntax decisions

F. Almeida cpp.tutoring at gmail.com
Wed May 19 00:55:35 PDT 2010


Dear all,

I once took a curious look at D in the past (circa 2004), and also
read the DM newsgroups at the time that Stroustrup frequently posted
here.

Now, in 2010, I've been giving D 2.0 a second chance and I'm happy
with it. Strings, dynamic arrays and hash tables couldn't be easier
to use and the option of metaprogramming presents way more
possibilities than C++.

However, I have a few questions about syntax.

1) Operator overloading

First of all, I find the operator overloading weird. This is because
I'm used to C++'s syntax "operator+" and "operator+=", for example,
instead of "opAdd" and "opAddAssign". While I understand that it
gives plenty of room and freedom for introducing new operators in
the future, it nonetheless feels less "natural", at the lack of a
better word. C++ has a reserved keyword "operator" for this, why not
using a modified syntax to this end in D? Why not using, for
example, "operator(+)", "operator(+=)", or "operator(cast(real))"?
This would be consistent with the simplified function template
syntax, and make operator declarations easier to read (I find that
reading the symbol itself is more intuitive when reading the source
code).

2) Operators that change context when unary or binary

I noticed that for example, "~this" denotes a class destructor
(thank you for including GC AND making it optional, btw), meaning
that the "~" operator is a negation when used as a unary operator.
However, it becomes a binary concatenation operator when used with
arrays/strings, which is completely unrelated to the other use.

The same applies to templates: "!" is a logical negation operator
when used as an unary operator, but it denotes a template when
introduced in a function declaration, for example.

This can be confusing for people learning the language, especially
if it's a first or second programming language. There are a number
of symbols that are not used at all and could just as easily be
used. Why not use "@"? The fact that D does not use the C macro
preprocessor means that "#" is available, for example.

Thank you for taking the time to read my questions.

Regards,
F. Almeida


More information about the Digitalmars-d-learn mailing list