My new favorite D feature
Steve Horne
stephenwantshornenospam100 at aol.com
Fri Dec 22 18:47:26 PST 2006
On Thu, 21 Dec 2006 23:39:39 -0600, Tim Keating
<holyspamster at gmail.com> wrote:
>In article <emeln6$25q3$1 at digitaldaemon.com>, BCS at pathlink.com says...
>> Bob W wrote:
>> No joke. I think He's saying he likes that feature.
>>
>> Just read it literally
>
>Yup. I complicated the example by using '=' instead of '==', which, as
>has been pointed out, is another way that D helps you avoid shooting
>yourself in the foot.
Yeah, but I'm not quite so keen. I like being able to use assignments
within expressions (in moderation, of course).
Personally, I think there are two sane options for programming
languages...
1 : Allowing assignments in expressions :
- Use := for assignment and == for equality testing
- The = operator should be recognised, but should always give an
error
2 : Disallowing assignments in expressions :
- Use a single = operator
The first option in particular makes a lot of sense if you've spent
most of the last 20 years regularly switching back and forth between C
and Pascal family languages.
Of course you can still confuse := and ==, in theory. But it is pretty
unlikely. Confusion isn't limited to these operators anyway. Consider
operators like +=. Whichever of the two characters you miss, you get
code that's legal but wrong. Finger trouble can always cause confusion
in any language. There comes a point where the programmer has to take
responsibility.
It's just that the = operator is an particular annoyance since it
means different things in Pascal and C family languages, and is
'overloaded' to mean both in languages like Basic, and all of these
different approaches are 'the right way' to a lot of people.
As for case 2, if there's no context where both operators are legal,
there's no point having two operators, so you may as well go the Basic
route and overload a single operator. I'm all for expressing
intentions explicitly, but that doesn't mean pointless pedanticism. We
are happy to have other kinds of overloading in the language, after
all, and decades of experience show that having a single = operator
causes no problems at all in the languages that work that way. It
simply means there's no way to write assignments in expressions,
saving the hassle of banning them.
The trouble is, it breaks my prime directive. Don't start changing
fundamental operators around once a language is in use. The kind of
chaos that would cause...
It was bad enough when Python changed its division operator :-(
--
Remove 'wants' and 'nospam' from e-mail.
More information about the Digitalmars-d
mailing list