dotnet / csharplang

The official repo for the design of the C# programming language
11.48k stars 1.02k forks source link

Champion "Implicitly scoped using statement" (16.3, Core 3) #1174

Open gafter opened 6 years ago

gafter commented 6 years ago

This is for some form of using statement, where the statements executed while the resource is held are the following statements in the same block that contains the using statement; the resource is freed at the end of the block.

See https://github.com/dotnet/csharplang/issues/114 and https://github.com/dotnet/csharplang/issues/114#issuecomment-349773994

LDM history:

Joe4evr commented 5 years ago

using X = Y is already valid syntax in C# that means something else entirely (it introduces a namespace or a type alias).

But it's not legal to use inside of a method scope. In fact, the compiler doesn't even consider it being a namespace/type alias when used in a method scope, so there's no ambiguity here.

DaZombieKiller commented 5 years ago

using X = Y is already valid syntax in C# that means something else entirely (it introduces a namespace or a type alias).

But it's not legal to use inside of a method scope. In fact, the compiler doesn't even consider it being a namespace/type alias when used in a method scope, so there's no ambiguity here.

I'm curious as to how that would interact with the scripting form of C#, where this doesn't necessarily apply.

wanton7 commented 5 years ago

This is great feature, but is there anything similar planned for namespaces?

Most if not all .cs files will have this wrapping namespace in them.

namespace MyNameSpace
{
    public class MyClass
    {
    }
}

It seems very redundant because most of the files will have single namespace just wrapping everything. It would be great to able to do this this instead

namespace MyNameSpace;

public class MyClass
{
}
Joe4evr commented 5 years ago

@wanton7 #137.

JesperTreetop commented 5 years ago

The documentation seems to imply that this (multi-declaration implicitly typed variables) is possible:

using var f2 = new FileStream("..."), f3 = new FileStream("...");

But it doesn't look to be possible with SharpLab. Is it supposed to be possible, and wouldn't it then clash with disallowing multi-declaration var generally?

I'd file this as a bug on the documentation but the proposal says the same thing.

YairHalberstadt commented 5 years ago

@JesperTreetop You can never use var with multiple declarations. You always have to specify the type explicitly.

munael commented 5 years ago

Should #114 just mention this issue instead of both being put on the project board?