473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

About syntactic sugar

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

Sep 27 '06 #1
13 2485
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:
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

Sep 27 '06 #2
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
|
Sep 27 '06 #3
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:
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
Sep 28 '06 #4
Maybe its just me but there seems to also be a significant scope
difference between the 2 as well.

Sep 28 '06 #5
Sam,

I don't agree, with you, your part is in my idea syntatic sugar because
there is a much easier to write method the other guy did did and therefore
it is better to maintain.

The sugar in your method is that it looks more precise, while that is not
true.

Just my thought,

Cor

"Sam Kong" <sa********@gmail.comschreef in bericht
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

Sep 28 '06 #6
Sam Kong <sa********@gmail.comwrote:

<snip>
I want to know what people think.
Would you call "using" statement a syntactic sugar?
If so, why?
If not, why not?
Yes, I'd say it's just syntactic sugar, for precisely the reasons
you've given. I don't regard "syntactic sugar" as a bad thing though :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 28 '06 #7
Steven,

It's not just you. I always thought it strange as well especially
since the specification uses the word "precisely". But, then again, I
don't think scope was the focus of the explanation in that section.

Brian

Steven Nagy wrote:
Maybe its just me but there seems to also be a significant scope
difference between the 2 as well.
Sep 28 '06 #8
Sure. But I guess what I want to point out is that its not JUST sugar
in this particular argument. There are some scope issues that seriously
differentiate the 2 examples.

I'm not very clued up on the specification though. Perhaps I should
read it some time soon.

Sep 28 '06 #9
Here is an interesting read ~related.
http://www.bluebytesoftware.com/blog...c-7847d98f1641

--
William Stacey [MVP]

"Steven Nagy" <le*********@hotmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
| Sure. But I guess what I want to point out is that its not JUST sugar
| in this particular argument. There are some scope issues that seriously
| differentiate the 2 examples.
|
| I'm not very clued up on the specification though. Perhaps I should
| read it some time soon.
|
Sep 28 '06 #10
Yes, there is a scope difference.

Another difference (IIRC) is that the using statement stores away a
reference to the object, so that it can be disposed even if you destroy
the declared reference to it.

If you do this:

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

the object will still be disposed properly.

Steven Nagy wrote:
Maybe its just me but there seems to also be a significant scope
difference between the 2 as well.
Sep 28 '06 #11
IIRC, the "}" in the using just explicitly calls dispose() so there really
is not much magic going on or object storage (the object already exists in
the method scope). using is a nice little pattern that is very helpful.

--
William Stacey [MVP]

"Göran Andersson" <gu***@guffa.comwrote in message
news:u3**************@TK2MSFTNGP06.phx.gbl...
| Yes, there is a scope difference.
|
| Another difference (IIRC) is that the using statement stores away a
| reference to the object, so that it can be disposed even if you destroy
| the declared reference to it.
|
| If you do this:
|
| using (R r1 = new R()) {
| r1.F();
| r1 = null;
| }
|
| the object will still be disposed properly.
|
| Steven Nagy wrote:
| Maybe its just me but there seems to also be a significant scope
| difference between the 2 as well.
| >
Sep 28 '06 #12

Steven Nagy wrote:
Sure. But I guess what I want to point out is that its not JUST sugar
in this particular argument. There are some scope issues that seriously
differentiate the 2 examples.
I guess what I'm saying is that the specification either used wording
that was too strong or didn't accurately represent the expansion.
Obviously, the specification's description and the compiler's behavior
aren't in complete agreement. Now if it said that it was precisely
like the following then I'd be more comfortable with it.

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

So despite what the specification says I think it is nothing more than
a syntactical convenience. A good one I might add :)
I'm not very clued up on the specification though. Perhaps I should
read it some time soon.
I can't say I'm that familiar with it either. I recommend reading the
ECMA version as opposed to the MSDN version. It's a little more
verbose.

Sep 29 '06 #13
Sam Kong wrote:
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.
By the by, the O'Reilly C# Language Pocket Reference (ISBN: 059600429X)
has a very similar assertion on page 30-31. They don't discuss scope
either.

My opinion is that the try...finally is more immediately understandable,
but that's probably due to my experience with Java and C++.

Undoubtedly, the using() {} is very nice and concise when you have some
experience in C#. To me it well represents the paradigm of managed
code. It is like the other gentleman's example of arrays in C. I could
easily see someone doing *(a+i) in C, but it would really seem out of
place to me in C++ instead of a[i]. I'm tempted to do a benchmark/asm
comparison session with that tho ;)

~Jason

--
Sep 29 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christopher T King | last post by:
Okay, so this is really two requests in one, and they're both kinda outlandish, but I'm gonna post them nonetheless: I've always thought xrange() to be ugly; it looks to be a lot of typing just...
5
by: F Jamitzky | last post by:
It is rather easy to define functions in python that mimic the special ruby syntactic sugar like: 5.times { print "Hello World!" } or .each { |food| eat food } In python these fragments...
12
by: Bob Stearns | last post by:
This is probably the wrong forum for this, but I thought it might start some discussion. The INSERT statement, in its current form, has problems being formatted so a human reader can follow it....
13
by: Neil Zanella | last post by:
Hello, It seems to me that C# properties are nothing more than syntactic sugar for getters and setters. I wonder whether others hold a different point of view. Basically, what more do they have...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
4
by: Bas | last post by:
Hi group, just out of curiosity, is there a list of all the syntactic sugar that is used in python? If there isn't such a list, could it be put on a wiki somewhere? The bit of sugar that I do...
34
by: glomde | last post by:
i I would like to extend python so that you could create hiercical tree structures (XML, HTML etc) easier and that resulting code would be more readable than how you write today with packages like...
28
by: ziman137 | last post by:
Hello all, I have a question and am seeking for some advice. I am currently working to implement an algorithmic library. Because the performance is the most important factor in later...
11
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.