Just had an interesting thought. In languages like C# and Java, I know that when it comes to incrementing and decrementing, you can do a post, or pre-increment/decrement. Like this:
刚做了一个有趣的想法。在C#和Java等语言中,我知道在增量和减量方面,你可以做一个帖子,或者预先增加/减少。像这样:
int a = 5;
Console.WriteLine(a++); // Would print out 5,
// And then increment a to 6
Console.WriteLine(++a); // Would then print out 7 because
// it increments a before printing it out
But, I was wondering if there is any such thing where one might do something like this:
但是,我想知道是否有任何这样的事情,人们可能会做这样的事情:
int a = 5;
Console.WriteLine(a += 5); // Would print out 5,
// And then increment a to 10
Console.WriteLine(a =+ 5); // (Or something along those lines)
// To print out 15 at this point
Just interested and didn't really know where or how to look for the answer, so wondered if anyone on SO would know anything more about it. Thanks!
只是感兴趣,并不知道在哪里或如何寻找答案,所以想知道SO上的任何人是否会更多地了解它。谢谢!
Edit: Added my question from the comments
编辑:从评论中添加了我的问题
Where exactly are a += 5
and a =+
5 defined? I've never seen the second in use. Does it exist at all...? Do they compile to the same thing?
确切地说,+ = 5和a = + 5的定义是什么?我从未见过第二个使用过的。它是否存在......?他们编译成同样的东西吗?
5 个解决方案
#1
In the old days, the C language offered this syntax as a shortcut for adding or subtracting a value from a variable.
在过去,C语言提供了这种语法作为从变量中添加或减去值的快捷方式。
a =+ 5;
b =- 5;
But early on in the life of C, dmr (of blessed memory) and ken deprecated that syntax in favor of
但是在C的生命早期,dmr(有福的记忆)和ken不赞成这种语法
a += 5;
b -= 5;
for precisely the same purpose, because it's far too easy to write b=-5
which means something entirely different from b -= 5
. This "experienced" programmer remembers rewriting a bunch of code to match the new language spec.
出于完全相同的目的,因为写入b = -5太容易了,这意味着与b - = 5完全不同。这位“经验丰富”的程序员记得重写一堆代码以匹配新的语言规范。
So there has never been pre- or post- increment semantics in those constructions like there is in a++
or --b
.
因此,在像++或--b这样的结构中,从未有过预增量或后增量语义。
#2
There is no such operator in any language that I know of, but you can write your own C# function to do the same thing!
在我所知的任何语言中都没有这样的运算符,但你可以编写自己的C#函数来做同样的事情!
static void postAdd<T>(ref T lhs, T rhs) {
T saved = lhs;
lhs += rhs;
return saved;
}
This is not possible in Java because Java does not support pass-by-reference with ref
.
这在Java中是不可能的,因为Java不支持使用ref传递引用。
#3
Console.WriteLine(a+=5);
Is perfectly legal in the langauge. It is a sort of pre-increment operator in that it will increment the variable a
before returning the value to the WriteLine
call.
在语言中完全合法。它是一种预增量运算符,因为它会在将值返回到WriteLine调用之前递增变量a。
=+ isn't a valid operator since it isn't defined by the C# standard. All of the C# operators can be found on this page: https://msdn.microsoft.com/en-us/library/ewkkxkwb.aspx
= +不是有效的运算符,因为它不是由C#标准定义的。所有C#操作符都可以在此页面上找到:https://msdn.microsoft.com/en-us/library/ewkkxkwb.aspx
Its not possible to create your own operators, because the compiler would have to know how to make expression trees or parse the line to generate the IL. The aside to this is that, by a lot of work and especially with the release of Rosyln, you could in theory make your own language (like IronPython) that had those operators.
它不可能创建自己的运算符,因为编译器必须知道如何创建表达式树或解析该行以生成IL。除此之外,通过大量的工作,尤其是Rosyln的发布,理论上你可以创建自己的语言(如IronPython),并拥有这些运算符。
The closest that you can come to your own operator are things like extension methods, for example:
您可以访问自己的运算符的最接近的是扩展方法,例如:
public class Extensions
{
public static int AddMul(this int x, int add, int mull)
{
return x * mull + add;
}
}
which would be used like:
将使用如下:
int x = 4;
int y = x.AddMul(2, 3); //y now equals 14
#4
The following prints the required results but is not exactly beautiful code:
以下打印所需的结果,但不是很漂亮的代码:
int a = 5;
System.out.println(a+=5);
System.out.println((a+=5)-5);
System.out.println(a);
Prints:
10, 10, 15
10,10,15
a+=5
returns the value of a
after the increment. (a+=5)-5
increments a
and returns its value before the increment.
a + = 5返回增量后的a值。 (a + = 5)-5增加a并在增量之前返回其值。
a=+5
just compiles to a=5
. This performs assignment and the unary plus operator. It is akin to doing a=-5
(a
equals negative 5).
a = + 5只编译为a = 5。这将执行赋值和一元加运算符。它类似于a = -5(等于负5)。
System.out.println(+5);
Prints:
5
In C#
the equivalent code generates the same output:
在C#中,等效代码生成相同的输出:
int a = 5;
Console.WriteLine(a+=5);
Console.WriteLine((a+=5)-5);
Console.WriteLine(a);
Console.WriteLine(+5);
Prints:
10, 10, 15, 5
10,10,15,5
#5
No. a += 5
isn't a post increment. It's an increment.
编号a + = 5不是后增量。这是一个增量。
a++
is post-increment. And ++a
is pre-increment.
a ++是后增量。并且++ a是预增量。
#1
In the old days, the C language offered this syntax as a shortcut for adding or subtracting a value from a variable.
在过去,C语言提供了这种语法作为从变量中添加或减去值的快捷方式。
a =+ 5;
b =- 5;
But early on in the life of C, dmr (of blessed memory) and ken deprecated that syntax in favor of
但是在C的生命早期,dmr(有福的记忆)和ken不赞成这种语法
a += 5;
b -= 5;
for precisely the same purpose, because it's far too easy to write b=-5
which means something entirely different from b -= 5
. This "experienced" programmer remembers rewriting a bunch of code to match the new language spec.
出于完全相同的目的,因为写入b = -5太容易了,这意味着与b - = 5完全不同。这位“经验丰富”的程序员记得重写一堆代码以匹配新的语言规范。
So there has never been pre- or post- increment semantics in those constructions like there is in a++
or --b
.
因此,在像++或--b这样的结构中,从未有过预增量或后增量语义。
#2
There is no such operator in any language that I know of, but you can write your own C# function to do the same thing!
在我所知的任何语言中都没有这样的运算符,但你可以编写自己的C#函数来做同样的事情!
static void postAdd<T>(ref T lhs, T rhs) {
T saved = lhs;
lhs += rhs;
return saved;
}
This is not possible in Java because Java does not support pass-by-reference with ref
.
这在Java中是不可能的,因为Java不支持使用ref传递引用。
#3
Console.WriteLine(a+=5);
Is perfectly legal in the langauge. It is a sort of pre-increment operator in that it will increment the variable a
before returning the value to the WriteLine
call.
在语言中完全合法。它是一种预增量运算符,因为它会在将值返回到WriteLine调用之前递增变量a。
=+ isn't a valid operator since it isn't defined by the C# standard. All of the C# operators can be found on this page: https://msdn.microsoft.com/en-us/library/ewkkxkwb.aspx
= +不是有效的运算符,因为它不是由C#标准定义的。所有C#操作符都可以在此页面上找到:https://msdn.microsoft.com/en-us/library/ewkkxkwb.aspx
Its not possible to create your own operators, because the compiler would have to know how to make expression trees or parse the line to generate the IL. The aside to this is that, by a lot of work and especially with the release of Rosyln, you could in theory make your own language (like IronPython) that had those operators.
它不可能创建自己的运算符,因为编译器必须知道如何创建表达式树或解析该行以生成IL。除此之外,通过大量的工作,尤其是Rosyln的发布,理论上你可以创建自己的语言(如IronPython),并拥有这些运算符。
The closest that you can come to your own operator are things like extension methods, for example:
您可以访问自己的运算符的最接近的是扩展方法,例如:
public class Extensions
{
public static int AddMul(this int x, int add, int mull)
{
return x * mull + add;
}
}
which would be used like:
将使用如下:
int x = 4;
int y = x.AddMul(2, 3); //y now equals 14
#4
The following prints the required results but is not exactly beautiful code:
以下打印所需的结果,但不是很漂亮的代码:
int a = 5;
System.out.println(a+=5);
System.out.println((a+=5)-5);
System.out.println(a);
Prints:
10, 10, 15
10,10,15
a+=5
returns the value of a
after the increment. (a+=5)-5
increments a
and returns its value before the increment.
a + = 5返回增量后的a值。 (a + = 5)-5增加a并在增量之前返回其值。
a=+5
just compiles to a=5
. This performs assignment and the unary plus operator. It is akin to doing a=-5
(a
equals negative 5).
a = + 5只编译为a = 5。这将执行赋值和一元加运算符。它类似于a = -5(等于负5)。
System.out.println(+5);
Prints:
5
In C#
the equivalent code generates the same output:
在C#中,等效代码生成相同的输出:
int a = 5;
Console.WriteLine(a+=5);
Console.WriteLine((a+=5)-5);
Console.WriteLine(a);
Console.WriteLine(+5);
Prints:
10, 10, 15, 5
10,10,15,5
#5
No. a += 5
isn't a post increment. It's an increment.
编号a + = 5不是后增量。这是一个增量。
a++
is post-increment. And ++a
is pre-increment.
a ++是后增量。并且++ a是预增量。