equals assignment

renoX renosky at free.fr
Fri Apr 27 14:19:29 PDT 2007


Urza a écrit :
> I think d should add an operator for assignment ":=" instead
> of "==" like Perl 6.
> http://dev.perl.org/perl6/doc/design/apo/A03.html

Uh, could you explain better what's the difference between = and := ?

The explanation in the Perl webpage is .. weird .. (that said being a 
webpage about Perl, I'm not surprised).


> I recently had trouble with == and = and I think D should add a
> Pascal like equals operator. This would be an improvement and allow
> the avoidance of a == d + 1; errors
> 
> ie if (a == d) ...
> ie if (a := d) ...
> 
> :=

A bad idea: = is shorter than :=, less character: less cruft.
The 'correct' way to avoid the == and = confusion is to make if accept 
only booleans: this way if a and d are int 'if (a = d)' is incorrect.

The only remaining issue is that it doesn't totally avoid the problem: 
you can still have = and == confusion when a and d are boolean, an error 
quite hard to find..

Another solution is the gcc hack of making the compiler warn on any = 
inside a 'if' except when there is a (( )), it's a hack but it's a more 
complete solution..


That said, given that D is based on C/C++ syntax (it even reused C's 
ugly syntax for variable declaration instead of using a nice syntax like 
Limbo or Scala do), I doubt that you'll get much success for  the 
replacement of = by :=.

renoX




More information about the Digitalmars-d mailing list