質問

I don't know a really good name for what I'm talking about, but, most programming languages use a syntax for assignment statements something like this:

(variable) (assignment operator) (expression)

And some have some special assignment operators, such as var1 += var2 which is equivalent tovar1 = var1 + var2.

My question is basically, is there any language that would treat += like above, but also have an =+ operator so that var1 =+ var2 would be equivalent to var2 = var2 + var1?

Essentially, are there programming languages with "mirrorable" assignment operators, so that the variable on the right is what gets assignment a new variable (as opposed to the one on the left)?

Edit:

I suppose a much better example of what I'm referring to would be a language in which you can have both (variable) (assignment operator) (expression) and (expression) (assignment operator) (variable) as valid statements.

役に立ちましたか?

解決

Authors of programming languages usually try to keep their language as simple as possible, without any unnecessary redundancy. So having any kind of two-side evaluation is redundant and unnecessary. It will only make the language specification 20 pages longer. I think there is no language with this feature.

By the way, your "grammar" (syntax rules) is not clear. What if someone writes

var x = 3, y = 2;
x = y;

By the way, Pascal has a "direction" of assignment. Actually, you can write your code as

var x = 3, y = 2;
x := y;
y =: x;

and write your own simple preprocessor for that, but again, I think it is just a complication of "clean" language.

他のヒント

What you propose is just a way to make code harder to read. There is no value in having a =+ operator if it's just synthatic sugar for writing your variables in a different order. But since you're into this weird stuff, you would surely love languages like Befunge and Piet, which, in a somewhat twisted way, do support that.

Some COBOL instructions are like this. E.g. ADD B TO A is the equivalent of "A += B" in other languages.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top