Re: Strange behavior of "=" as assignment operator - Mailing list pgsql-general

From David Johnston
Subject Re: Strange behavior of "=" as assignment operator
Date
Msg-id 1370080343052-5757645.post@n5.nabble.com
Whole thread Raw
In response to Re: Strange behavior of "=" as assignment operator  (Chris Travers <chris.travers@gmail.com>)
Responses Re: Strange behavior of "=" as assignment operator  (Chris Travers <chris.travers@gmail.com>)
Re: Strange behavior of "=" as assignment operator  (Pavel Stehule <pavel.stehule@gmail.com>)
List pgsql-general
Chris Travers-5 wrote
> However = as assignment is particularly odd to me for two reasons.  First
> it is not ambiguous but it leads to difficult to read constructs, like
> this:
>
>    out_var = in_left = in_right;

Agreed but the genie is already out of the bottle and I am OK with something
at the "lint/check_function" level trying to enforce good coding habits as
opposed to changing the language to prohibit what has now been allowed for
many years.

Also, "out_var := in_left = in_right;" is not THAT much more readable than
the former and in either case seeing the variable "out_var" begin the
statement is likely a much bigger indicator that "out_var is being assigned
to" than is the presence of the "=".  Since we do not allow multiple
assignment everything to the right of the variable has to be an expression
(i.e. we are NOT assigning the value of in_right to in_left and out_var).


> Secondly it is way too easy for a beginner to accidentally use = as an
> assignment operator.  This can lead to odd, difficult to understand, bugs.
>  If it is going to be legacy behavior at least we should consider raising
> a
> warning.

Should read:

"... accidentally use = as assignment operator - when they intended to use
it as an equality operator."

While I admit this is possible, using a statement when you meant to use an
expression, it seems to me it would be an unusual occurrence mainly because
if I need an equality expression the first thing I am going to type will be
the "IF/DO/CASE" context syntax in which the expression is evaluated.  I
type "var = ..." if I am thinking assignment and I type "IF var = ..." if I
am thinking equality.

If they are using it for assignment intentionally then this is not a
problem.

If they want to perform an assignment within an expression such would not
work because the assignment would look like an equality comparison but doing
this is likely not something an SQL programmer would attempt generally and
is IMO not something where prevention is worth the cost of breaking forward
compatibility.

A comment was made that "GET DIAGNOSTICS var = item;" is standard defined.
Is the use of ":=" for assignment also standard defined?  If so its not that
inconsistent standards surprise me but...anyway.

David J.



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Strange-behavior-of-as-assignment-operator-tp5757205p5757645.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


pgsql-general by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: Strange behavior of "=" as assignment operator
Next
From: Chris Travers
Date:
Subject: Re: Strange behavior of "=" as assignment operator