I'm currently designing a database query language and I came to wonder what should be the best syntax for the comparison operator.
Most modern languages use ==
, but amongst the database languages based on SQL the =
is also often used.
I acknowledge that the =
should have been the comparison operator whereas affectation might have been something like <-
if languages had follow a more mathematic syntax, but I guess languages like C have contribued to make =
for affectation a standard.
However, I'm not looking for a debate on what should be the best operator for affectation or comparison, but to know if using the same operator for two distinct things like MySQL and some other languages do is a practice to banish.
In terms of language design, using =
could be a bit annoying since it is source of grammar ambiguities, since this operator is also used for affectation. MySQL seems to solve this ambiguity by introducing the SET
keyword but in my case I don't see the point of introducing a new keyword where I can simply use different operators for affectation and comparison to remove the ambiguity.
So my question is : is there a real benefit to use =
as comparison operator considering it is already used for affectation ?
if (a = b) { ... }
which meant, "assign the value of b to a and if the result is nonzero then do something". It's a perfectly valid statement, arguably does something useful, and is the source of a lot of bugs when the programmer meantif (a == b)
but accidentally missed the second=
. Please don't bring those days back. – Dan Pichelman Jan 20 '20 at 12:56=
does the affectation not the comparison. The problem resides more in the fact that the language allows to do affectations anywhere as they're considered as expressions rather than instructions. This can be used to write very beautiful code though. – ibi0tux Jan 20 '20 at 17:08=
and:=
in assignments (see SO). Note that<-
is much more ambiguous asa<-2
show (is ita<(-2)
ora<-(2)
). Finally BASIC grammar uses = for both assignment and comparison. Initially there was aLET
but it quickly became optional since the grammar rules avoid ambiguity. – Christophe Jan 20 '20 at 19:06if ((a = b))
. – Deduplicator Jan 20 '20 at 22:18