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 »

Hi Guys,

Sorry, I must not have been paying attention at the Conference in Cologne, although I did learn a great deal (LOTS) ;-0) Thanks to all session providers.

Please can you put me right on the correct syntax to use for "begin/end USING", that we can now use like in C#.

I wish to have a StreamReader object which is opened then closed and removed within my IF statement - simple images attached of my so far fail attempts.

Please be aware that my brain is a bit fuddled as I have just spent a few hours coding Async/Await/Task samples. Much watch some cricket !
helpForum_using_01.png
helpForum_using_01.png (10.99 KiB) Viewed 737 times
helpForum_using_02.png
helpForum_using_02.png (10.74 KiB) Viewed 737 times
TIA,
Phil,
Wales, UK (at the moment )
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

USING syntax ? - help please ...

Post by wriedmann »

Hi Phil,

this is my own sample for the using statement:

Code: Select all

using System.IO                              
using System.Text

function Start( ) as void                     
local cFileName as string
local aBytes as byte[]
	                         
cFileName := "c:tempUsing.txt"
System.Console.WriteLine("start working with" + cFileName )
begin using var oFile := FileStream{ cFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite } // as FileStream
  aBytes := Encoding.ASCII.GetBytes( "Hello world!" )
  oFile:Write( aBytes, 0, aBytes:Length )
end using                                                
// file is not only out of scope, but also disposed, i.e. closed
System.Console.WriteLine("end working with" + cFileName )
System.Console.Write( "press a key...." )
System.Console.ReadKey()
	
return
I was not able to use the as FileStream in the using statement, only var worked.

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

USING syntax ? - help please ...

Post by FFF »

Hi Phil,
well, there IS a help file, even offline ;)
From what i read:
BEGIN USING VAR oTest := Test{}
oTest:DoSomething()
END USING
....
Or, maybe i didn't understand the problem

BTW, i didn't find in help, how multiple vars are handled - if one has to nest them, or one "End Using" to unbound them all...

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

USING syntax ? - help please ...

Post by wriedmann »

Hi Karl,
BTW, i didn't find in help, how multiple vars are handled - if one has to nest them, or one "End Using" to unbound them all...
Fabrice said you can nest them, and this is how it works also in C#.

Code: Select all

begin using var v1 := ...
  begin using var v2 := ....
    do something
  end using
end using
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

USING syntax ? - help please ...

Post by Phil Hepburn »

Hi Karl,

Yes, and I was using the on-line help from the web site / forum.

Problem with all new stuff however, is when is/was uploaded or added, etc., etc,.

The on-line details I found were the basis of my attempts, and what I posted to you guys.

Oh! - I see, there are at least two places in the on-line HELP which have different versions of syntax! Or there was a couple of hours ago. Now I see at least two versions of the same (and improved) syntax. Who has been busy this afternoon ? Can't now find the one with 'eos' and the ?s in my last images.

Fingers crossed that this now works.

Thanks Wolfgang for you sample.

Regards,
Phil.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

USING syntax ? - help please ...

Post by Phil Hepburn »

Karl (and all),

This is what I had previously found and tried to use and interpret.
helpForum_using_03.png
helpForum_using_03.png (49.25 KiB) Viewed 737 times
This may be a correct and fuller way to define the syntax BUT, as a standard user guy I find it hard to understand.

HTH,
Phil
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

USING syntax ? - help please ...

Post by wriedmann »

Hi Phil,

the problem with this documentation page is that it was auto-generated from the language definition, and someone from the devteam can explain what every word exactly means.

The word "eos" means "end of statement", I think.

To be sure, you could check also https://docs.xsharp.it where I had added exact the sample after I returned from the conference:

http://docs.xsharp.it/doku.php?id=using

I have to explain it better, I know - there needs to be said the used object must implement the IDisposable interface.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

USING syntax ? - help please ...

Post by Phil Hepburn »

Wolfgang - Thanks, and that explains why I could not USE a 'StringBuilder' class / object ;-0((
helpForum_using_04.png
helpForum_using_04.png (13.21 KiB) Viewed 737 times
That is a shame.

Are there any other restrictions as to what the USING class can or can't be ?

Cheers,
Phil.
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

USING syntax ? - help please ...

Post by wriedmann »

Hi Phil,

since the X# compiler is using the Roslyn compiler, this is true also for C#. So the X# team is not to blame for this:

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
Chris
Posts: 4907
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

USING syntax ? - help please ...

Post by Chris »

Guys,

Well, what BEGIN USING does is to implicitly call Dispose() on the object after it is used, so if the class does not implement IDisposable (which provides the Dispose() method) then it wouldn't make much sense :)

OK, it actually also implicitly uses a TRY...FINALLY block and enforces a limited scope (when exiting it, it can now be collected) for the declared variable(s), which you can also enforce manually if you want, for any local, no matter if it is an IDisposable or not, by using BEGIN..END SCOPE:

Code: Select all

BEGIN SCOPE
  // All vars declared here will be visible only inside this block (scope)
  LOCAL oAnyVar AS MyType
  // ...
END SCOPE
Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply