dotnet-shell / Shell

The C# script compatible shell
https://dotnet-shell.github.io/
MIT License
61 stars 6 forks source link

Using declaration cannot be used as top-level statement or in shell #6

Closed RadxaYuntian closed 2 years ago

RadxaYuntian commented 2 years ago

Expected behavior

IDisposable objects can be declared with using var as top-level statement, or in interactive shell.

Current behavior

C# compiler throws error out. When the same statement is instead compiled with dotnet run, there is no error.

Sample shell output

!yuntian@Yuntian /home/yuntian> using var f = File.Open("/tmp/test", FileMode.OpenOrCreate);
(1,11): error CS1002: ; expected
!yuntian@Yuntian /home/yuntian> if (0 == 0) { using var f = File.Open("/tmp/test", FileMode.OpenOrCreate); }
!yuntian@Yuntian /home/yuntian> var f = File.Open("/tmp/test", FileMode.OpenOrCreate);
!yuntian@Yuntian /home/yuntian> f.Dispose()
!yuntian@Yuntian /home/yuntian>

Reasoning

Although using var is not exactly useful in main() or in interactive shell where variables will be discarded anyway when the process ends, scripts can be reworked into functions and reused elsewhere. Allowing the variable to be properly declared can save debugging effort down the line.

RadxaYuntian commented 2 years ago

Seems to be an inherited issue from dotnet-script.