If you were to write your own programming language (as I still intend to do someday even if only for the learning experience it provides me), what symbol (or symbols) would you use to represent the assignment operator?
Even though it would looks like BNF or old-school Pascal, I would lean towards using := as the assignment operator. That would leave me free to use = for equality and there wouldn't be the issue of = vs. == tripping up new programmers. Besides, performing assignments is more common than making comparisons, so perhaps assignment should have the more distinct operator. Going one step further, if the language I wrote was not strongly typed, I would use == as the identity operator (as like PHP's ===). := and = to == seems a more logical progression to me than = and == to ===.
I suspect I wouldn't use = as both assignment and comparison as Basic does because of the ambiguity it causes. For example:
I think my second choice would be just : and have something like:
Of course, this all presumes the elements of a statement are written in a certain order. If you were to use a keyword such as set:
Even though it would looks like BNF or old-school Pascal, I would lean towards using := as the assignment operator. That would leave me free to use = for equality and there wouldn't be the issue of = vs. == tripping up new programmers. Besides, performing assignments is more common than making comparisons, so perhaps assignment should have the more distinct operator. Going one step further, if the language I wrote was not strongly typed, I would use == as the identity operator (as like PHP's ===). := and = to == seems a more logical progression to me than = and == to ===.
I suspect I wouldn't use = as both assignment and comparison as Basic does because of the ambiguity it causes. For example:
x = y = 0Does this mean "assign 0 to both x and y", or "assign the boolean comparison whether the value of y is 0 to x?" Statements such as these:
x := y = 0 x := y := 0are then both clear in their meaning.
I think my second choice would be just : and have something like:
x: 2 + 2The lvalue appears as if it were a label, giving the visual representation that x means 2 + 2. Plus, it would be one (of many) syntaxtic differences that would separate my language from the others. I don't know of any languages that currently use : as an assignment operator.
Of course, this all presumes the elements of a statement are written in a certain order. If you were to use a keyword such as set:
x set 2 + 2just appears awkward to me. It would have to be:
set x 2 + 2But if you always require the assignment target on the left-hand side of your operator, then is an explicit assignment operator really required as all? The implied assignment operation of something like:
x 2 + 2is clean and succinct.
This makes visual sense to me.
ReplyDeletex <- 2 + 2
I’d like to use “←” for assignment! ;-)
ReplyDeletex ← 2 + 2
NB my favorite OS doesn't star with “Windows…” but ends with “…Linux” |-D