www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - equals assignment

reply Urza <tawngarth magic.com> writes:
I think d should add an operator for assignment ":=" instead
of "==" like Perl 6.
http://dev.perl.org/perl6/doc/design/apo/A03.html

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) ...

:=
Apr 27 2007
parent reply renoX <renosky free.fr> writes:
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
Apr 27 2007
parent reply Michiel Helvensteijn <nomail please.com> writes:
renoX wrote:

 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..

When should an assignment be used as part of an expression anyway? It's only a way to make code more compact and less readable. Maybe an assignment just shouldn't return anything. I think the compiler should still be able to implicitly cast int to bool, though. -- Michiel
Apr 27 2007
parent Dan <murpsoft hotmail.com> writes:
Heehee... today I retired from my job.
On thursday I'm going to a tropical island.  That said...


I think the real solution is to just use the right operator, and recognize the
difference between = and ==, just like you know to put a semicolon at the end. 
It's trivial.

Worry about metaprogramming or 'in' operator for normal arrays, proveability or
something.  : p

Michiel Helvensteijn Wrote:
 renoX wrote:
 
 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..

When should an assignment be used as part of an expression anyway? It's only a way to make code more compact and less readable. Maybe an assignment just shouldn't return anything. I think the compiler should still be able to implicitly cast int to bool, though. -- Michiel

Apr 27 2007