关于语法糖 [英] About syntactic sugar

查看:86
本文介绍了关于语法糖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在讨论C#的使用声明时,我和一个人有争执。

在C#规范(15.13)中,那里这是一个如下的解释。


使用(R r1 = new R()){

r1.F();

}


恰好相当于


R r1 = new R();

试试{

r1.F();

}

最后{

if(r1!= null)( (IDisposable)r1).Dispose();

}


我认为using语句只是一个语法糖。

但这家伙并不这么认为。

他对句法糖的定义非常狭窄。

他说做同样的事情并不代表它' '一个语法糖。

他显示如下的例子。


a ++是a = a + 1的语法糖。

a [i]是C语言*(a + i)的语法糖。


我想知道人们的想法。

你愿意吗?打电话使用"声明一个语法糖?

如果是这样,为什么?

如果没有,为什么不呢?


谢谢。


Sam

Hi,

While discussing C#''s using statement, a guy and I had an argument.
In C# spec (15.13), there''s an explanation like the following.

using (R r1 = new R()) {
r1.F();
}

is precisely equivalent to

R r1 = new R();
try {
r1.F();
}
finally {
if (r1 != null) ((IDisposable)r1).Dispose();
}

I think that using statement is just a syntactic sugar.
But the guy doesn''t think so.
He has a very narrow definition of syntactic sugar.
He says that doing the same thing doesn''t mean it''s a syntactic sugar.
He shows examples like the following.

a++ is a syntactic sugar for a = a + 1.
a[i] is a syntactic sugar for *(a + i) in C language.

I want to know what people think.
Would you call "using" statement a syntactic sugar?
If so, why?
If not, why not?

Thanks.

Sam

推荐答案

确定它是语法糖。一般来说,如果你可以用两种不同的方式做同样的事情,其中​​一种(通常是较短的)就是语法糖。


但那么谁在乎呢?我喜欢语法糖。任何我的代码更容易阅读,因此对于下一个看起来很好的东西的程序员而言,维护成本更低。


BTW,a [i]与C中的*(a + i)不同,因此它不是
语法糖(即使它们完成同样的事情)。 />

-

Jeffrey Hornby

Hornby Consulting,Inc。


" Sam香港及QUOT;写道:
Sure it''s syntactic sugar. Generally if you can do the exact same thing two
different ways, one of them (usualy the shorter) is syntactic sugar.

But then who cares? I like syntactic sugar. Anything that my code easier
to read and therefore cheaper to maintain for the next programmer who looks
at it is a good thing.

BTW, a[i] is not the same thing as *(a+i) in C, therefore it is not
syntactic sugar (even though they accomplish the same thing).

--
Jeffrey Hornby
Hornby Consulting, Inc.

"Sam Kong" wrote:




在讨论C#的使用声明时,我和一个人有争执。 br />
在C#规范(15.13)中,有如下的解释。


使用(R r1 = new R()){

r1.F();

}


恰好相当于


R r1 = new R();

尝试{

r1.F();

}

finally {

如果(r1!= null)((IDisposable)r1).Dispose();

}


我认为使用声明只是一个语法糖。

但这家伙并不这么认为。

他对语法糖的定义非常狭窄。

他说做同样的事情并不代表它是一种语法糖。

他展示了如下例子。


a ++是一个a = a + 1的语法糖。

a [i]是C语言中*(a + i)的语法糖。


我想要知道什么人们认为。

你会打电话给使用吗?声明一个语法糖?

如果是这样,为什么?

如果没有,为什么不呢?


谢谢。


Sam

Hi,

While discussing C#''s using statement, a guy and I had an argument.
In C# spec (15.13), there''s an explanation like the following.

using (R r1 = new R()) {
r1.F();
}

is precisely equivalent to

R r1 = new R();
try {
r1.F();
}
finally {
if (r1 != null) ((IDisposable)r1).Dispose();
}

I think that using statement is just a syntactic sugar.
But the guy doesn''t think so.
He has a very narrow definition of syntactic sugar.
He says that doing the same thing doesn''t mean it''s a syntactic sugar.
He shows examples like the following.

a++ is a syntactic sugar for a = a + 1.
a[i] is a syntactic sugar for *(a + i) in C language.

I want to know what people think.
Would you call "using" statement a syntactic sugar?
If so, why?
If not, why not?

Thanks.

Sam


其他东西都是糖。框架是胜过win32的糖。

Win32比其他东西糖。我会用使用通过手册

方法。


-

William Stacey [MVP]


Sam Kong < sa ******** @ gmail.com写信息

新闻:11 ********************* @ m7g2000cwm.googlegrou ps.com ...

|

|

|在讨论C#的使用声明时,我和一个人有争执。

|在C#规范(15.13)中,有如下的解释。

|

|使用(R r1 = new R()){

| r1.F();

| }

|

|恰好相当于

|

| R r1 = new R();

|试试{

| r1.F();

| }

|终于{

| if(r1!= null)((IDisposable)r1)。Dispose();

| }

|

|我认为使用语句只是一种语法糖。

|但这家伙并不这么认为。

|他对句法糖的定义非常狭窄。

|他说做同样的事情并不代表它是一种语法糖。

|他展示了如下例子。

|

| a ++是a = a + 1的语法糖。

| a [i]是C语言中*(a + i)的语法糖。

|

|我想知道人们的想法。

|你会打电话给使用吗?声明一个句法糖?

|如果是这样,为什么?

|如果没有,为什么不呢?

|

|谢谢。

|

| Sam

|
Everything is sugar over something else. The framework is sugar over win32.
Win32 is sugar over something else. I would use "using" over the manual
method.

--
William Stacey [MVP]

"Sam Kong" <sa********@gmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
| Hi,
|
| While discussing C#''s using statement, a guy and I had an argument.
| In C# spec (15.13), there''s an explanation like the following.
|
| using (R r1 = new R()) {
| r1.F();
| }
|
| is precisely equivalent to
|
| R r1 = new R();
| try {
| r1.F();
| }
| finally {
| if (r1 != null) ((IDisposable)r1).Dispose();
| }
|
| I think that using statement is just a syntactic sugar.
| But the guy doesn''t think so.
| He has a very narrow definition of syntactic sugar.
| He says that doing the same thing doesn''t mean it''s a syntactic sugar.
| He shows examples like the following.
|
| a++ is a syntactic sugar for a = a + 1.
| a[i] is a syntactic sugar for *(a + i) in C language.
|
| I want to know what people think.
| Would you call "using" statement a syntactic sugar?
| If so, why?
| If not, why not?
|
| Thanks.
|
| Sam
|


Sam,


这就像他们来的一样甜蜜。您可以通过在生成的IL上查看

来判断它的糖分。


Brian


Sam Kong写道:
Sam,

That''s about as sweet as they come. You can tell it''s sugar by looking
at the IL generated.

Brian

Sam Kong wrote:




在讨论C#的使用声明时,我和一个人有争执。 br />
在C#规范(15.13)中,有如下的解释。


使用(R r1 = new R()){

r1.F();

}


恰好相当于


R r1 = new R();

尝试{

r1.F();

}

finally {

如果(r1!= null)((IDisposable)r1).Dispose();

}


我认为使用声明只是一个语法糖。

但这家伙并不这么认为。

他对语法糖的定义非常狭窄。

他说做同样的事情并不代表它是一种语法糖。

他展示了如下例子。


a ++是一个句法糖为a = a + 1.

a [i]是C语言*(a + i)的语法糖。


我想知道人们的想法。

你会打电话给使用吗?声明一个语法糖?

如果是这样,为什么?

如果没有,为什么不呢?


谢谢。


Sam
Hi,

While discussing C#''s using statement, a guy and I had an argument.
In C# spec (15.13), there''s an explanation like the following.

using (R r1 = new R()) {
r1.F();
}

is precisely equivalent to

R r1 = new R();
try {
r1.F();
}
finally {
if (r1 != null) ((IDisposable)r1).Dispose();
}

I think that using statement is just a syntactic sugar.
But the guy doesn''t think so.
He has a very narrow definition of syntactic sugar.
He says that doing the same thing doesn''t mean it''s a syntactic sugar.
He shows examples like the following.

a++ is a syntactic sugar for a = a + 1.
a[i] is a syntactic sugar for *(a + i) in C language.

I want to know what people think.
Would you call "using" statement a syntactic sugar?
If so, why?
If not, why not?

Thanks.

Sam


这篇关于关于语法糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆