USING syntax ? - help please ...

This forum is meant for questions and discussions about the X# language and tools
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

USING syntax ? - help please ...

Post by Phil Hepburn »

Thanks Chris,

Will give this a go with the StringBuilder I was playing around with earlier.

Cheers,
Phil.
FFF
Posts: 1589
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

USING syntax ? - help please ...

Post by FFF »

Chris,
FWIW, i don't get most of the use(fulness) of all this. If i know and want to get rid of a var at a certain point in my code, i can and should null it, with less keys to type and less visual clutter produced.
If i tend to reuse "names", i should better rethink my naming scheme and/or build smaller entities...
So, what remains?

Karl
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
Chris
Posts: 4953
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

USING syntax ? - help please ...

Post by Chris »

Hi Karl,

It's a matter of personal style, preferences etc. Purists will tell you that you should always use scopes and similar stuff, others will say all this is nonsense, personally I am somewhere in between :)

I'd say to everyone just use the way you prefer, no matter what is the "recommended" way to do something or not, just make sure you know the advantages and disadvantages of each method..

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
robert
Posts: 4555
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

USING syntax ? - help please ...

Post by robert »

Phil,
That notation in the docs is like the regular expression

END USING? garbage? eos

means:

END = mandatory end keyword
USING? = optional USING keyword
garbage? = optionally comments and other tokens until the end of the statement
eos = end of statement (either CRLF or semi colon)

END and USING are in capitals, they are tokens
garbage and eos are in lowercase, they are other parser rules.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

USING syntax ? - help please ...

Post by Phil Hepburn »

Thanks for the explanation Robert.

I am about to spend a few hours on some 'Async/Await/Task' examples and so I will use the USING and scope stuff just to see how it works etc.

I am a bit like Chris, somewhere in between ;-0)

But over the years with .NET I do tend to declare variables much closer to where I need to use them - it feels right to me. And I try to dispose of some (external type) objects immediately after they have been used.

I can see what Microsoft are doing - trying to hide all/much code stuff that gets in the way of a more readable and descriptive business solution. They have done this in many places, some much bigger than others.

Must shoot - I think Alwyn wishes me to help him get going with XSharp, he's on the phone just now.

Best Regards,
Phil.
User avatar
wriedmann
Posts: 3774
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

USING syntax ? - help please ...

Post by wriedmann »

Hi Karl,

if I remember correctly what Fabrice said in his session: the "using" statement is very useful because you cannot forget to close a file - the runtime system does it for you.

And there is another important thing: after the end of a "using" statement the relative variable is collected immediatly (and the Dispose() method is also called).

Of course you can do that also by hand, but then you have also to provide a try-catch block to make sure the file is also closed when an exception occurs.

Personally, I don't like all of these new language features because I think some of them make the code harder to read, but the "using" statement is IMHO a very useful addition, and I'm pretty sure I will start to use it.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1589
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

USING syntax ? - help please ...

Post by FFF »

Wolfgang Riedmann wrote:if I remember correctly what Fabrice said in his session: the "using" statement is very useful because you cannot forget to close a file - the runtime system does it for you.
Interesting. Is there a somewhat more "generic" description, what we can assume the runtime to do - and what not?

TIA
Karl
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3774
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

USING syntax ? - help please ...

Post by wriedmann »

Hi Karl,

my earlier posted link to the MS documentation describes it:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4555
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

USING syntax ? - help please ...

Post by robert »

Wolfgang,
Nesting BEGIN USING is not really necessary.
This works, just like it does in C#

Code: Select all

	BEGIN USING LOCAL oStream := FileStream{"aa", FileMode.Open}, ;
		oStream2 := FileStream{"bb", FileMode.Open} AS FileStream
		? oStream:ReadByte()
		? oStream2:ReadByte()
	END USING             
If you compile this and open it with reflector or ilspy you will see that under the hood indeed 2 nested using statements were generated.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3774
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

USING syntax ? - help please ...

Post by wriedmann »

Hi Robert,

thank you very much! Is is good to hear that this works also.

Personally I prefer the nested using statements because code is easier to read and to understand (IMHO readability is one of the most important advantages of X# over C#).

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply