-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: File scoped namespaces #137
Comments
Well, one reason is that namespaces can be nested. thereby creating additional scopes. For example, your code using System;
namespace Company.Project
{
public class Product
{
...
}
} could also be written as using System;
namespace Company
{
namespace Project
{
public class Product
{
}
}
} In both cases the full type name, including the namespace, becomes Also, |
Assuming the proposal does not remove the ability to nest namespaces (which would break all existing C# code), this could improve indentation for the vast majority of code. #114 has similar goals. |
@bondsbw So you're saying that if this proposal were implemented we'd have 3 different ways to write the same scoped namespace? using System;
namespace Company.Project
{
public class Product
{
...
}
} or using System;
namespace Company
{
namespace Project
{
public class Product
{
}
}
} or namespace Company.Project
using System;
public class Product
{
...
} Would this new variation of the For example, if I wrote namespace Company
using System;
namespace Project
public class Product
{
...
} Does this compile I also noticed that the namespace Company.Project
using System;
public class Product
{
...
} would be equivalent to namespace Company.Project
{
using System;
public class Product
{
...
}
} |
Weird idea of using multiple namespaces in one file which should define one entity (class). I don't remember any sample where I need to have several namespaces in one file or nesting them which produces mass of indentation as result hard to read such code. |
@bomzj I never said that multiple top-level namespaces in one file was a good idea, only that we can do it now and I've seen code files that take advantage of that. Having multiple nested namespaces in one file is also something we can do now and has completely unambiguous nesting semantics. |
Yes. Proposals to break backward compatibility are DOA except in the most minor cases. It would be a huge break to remove the block syntax. |
My vote would be: allow a file to allow up to 1 file level namespace directive per file and that it must come before any container namespace directives or type declarations. Differentiate between a "file level namespace directive" and a "container namespace directive" by the fact that a file level one would not have a body and must have a semi:
Such a namespace directive would act as if it was a container namespace containing the rest of the file. |
@bondsbw Yes, proposals which break back compatibly are DOA. I still don't see how this change provides any major benefit other than not having a level of indenting, which is fairly inconsequential. @bbarry So you're saying that we would have a new |
No you could have in examples: ex1: (valid C# today) using System;
namespace Company.Project
{
using System.Collections;
public class Product { }
} or ex2: (valid C# today) using System;
namespace Company
{
using System.Collections;
namespace Project
{
public class Product { }
}
} or ex3: (proposed, equal semantics to ex1) using System;
namespace Company.Project;
using System.Collections;
public class Product { } or ex3: (proposed, equal semantics to ex2) using System;
namespace Company;
using System.Collections;
namespace Project
{
public class Product { }
} but not: ex5 (admittedly an arbitrary example, but the rational would be that if you are using both a container namespace and a file namespace it is because you have more than one container in your file or you require the semantics of ex2): using System;
namespace Company;
namespace Project;
using System.Collections;
public class Product { } and not: ex6 (again arbitrary; here I am explicitly denying this as in scope in this proposal to consider it potentially for the future): using System;
namespace Company
{
namespace Project;
using System.Collections;
public class Product { }
} and not: ex7 (avoiding a pit of failure due to overly complex files): using System;
namespace Whatever {};
namespace Company.Project;
using System.Collections;
public class Product { } and not: ex8 (again avoiding complexity): using System;
delegate void MyFunc(string s); //or any other type decl
namespace Company.Project;
using System.Collections;
public class Product { } |
I was hoping this would make it over from /roslyn. Putting the namespace in the class name appeals to me the most since I have never, ever defined more than one namespace per file. It would be great if that worked for partial classes too:
However, the @scottdorman About ambiguity: just like the |
I agree, the vast majority of the time we only have one namespace per file and only have one class per file. However, that's not always the case and it's certainly not enforced by the compiler and/or IDE. (I've worked in languages that enforced this, and it got to be a real pain. That's not to say there may be good ways to actually do this where it doesn't get in your way more often than not, though.) The ambiguity about what the scope actually is comes about if multiple I'm not a big fan of having the namespace be part of the class name at all. First, it requires changing what constitutes a valid class identifier, but more importantly I think it introduces a lot of redundancy as far as namespaces go. Would the Given @bbarry's earlier examples, I still find this using System;
namespace Company
{
using System.Collections;
namespace Project
{
public class Product { }
}
} to be considerably more readable than using System;
namespace Company;
using System.Collections;
namespace Project
{
public class Product { }
} Especially since, at least to me, all this is really doing is letting me skip two curly braces and some indenting, I don't see the benefits. (I have the same issue, among others, with the implicitly scoped |
I don't see an ambiguity. Same with the using thing: using (a);
// Invisible { for a
// Still using a even though no indent
using (b);
// Invisible { for b
// Still using a and b even though no indent
// Invisible } for b
// Invisible } for a namespace a;
// Invisible { for a
// Namespace here is "a" even though no indent
namespace b;
// Invisible { for b
// Namespace here is "a.b" even though no indent
// Invisible } for b
// Invisible } for a I agree with you that this would not be as flexible for multi-namespace files, but
|
Why not to use 2 types of declaring namespace ? The old approach with nesting where multiple namespaces are need and new one as single line for vast majority. |
@bomzj I suggest opening up a separate proposal for the folder structure idea. |
using System;
namespace Company;
using System.Collections;
namespace Project
{
public class Product { }
} wouldn't (or at least, shouldn't) be allowed under this proposal. By having two namespaces, you defeat the whole point of it. If you need multiple namespaces, then use the old syntax. For those 99.9% of times when only one namespace is used, then the nesting can be reduced down. I'd actually take this proposal one step further and allow the removal of using System;
public class Company.Project.Product;
private readonly bool _someFlag;
public Product() => ...
etc |
I like this idea but only when it's limited specifically so that |
@HaloFour why must be the first directive? Isn't it enough to be the only namespace declaration within that file?
|
@gordanr Probably but why would you put it elsewhere? |
I think it helps the directive to stand out. Otherwise it might get lost after a long list of |
No need to import bad ideas from Java. Also, In Java projects you typically have gazillion of namespaces, precisely because of this structure. No thanks, I don't want a C# mode that enables this messiness. Regarding this proposal, I'd like to keep having the freedom to combine multiple namespaces in 1 file. I use it for related extension classes, where each of the classes is defined in a different namespace (the same namespace as of the class it extends). |
Can anyone see any conflict with partial classes? Also, I am not sure is the proposal's name appropriate. I have no idea how, but probably can be better. |
For all my files that have a single namespace (which is all of them so far, personal and work, including every file I've ever edited or read in an open source project), nothing would make me happier than this: using System;
public class My.Namespace.ClassName
{
} And I do mean nothing. :-) |
This might be good but Visual Studio does not do this automatically, so you have to amend it every time you create class. |
@bomzj First things first. :-) If the language supports it, Visual Studio templates will be able to be created eventually. (Speaking of which, for every console app I ever create, I add |
Vote down. Placing using inside a namespace may result in breaking change for existing code. Except for this the proposal has no benefit but adding one more subtle syntax for already existing feature. We have 8 (or nine?) ways to declare a property now. Isn't it enough? 😕 |
@ig-sinicyn I don't know if it deserves a down vote for those reasons. It's not a breaking change to any existing code. If you edit your code to start using the new syntax in the proposal, that's no more breaking than if you edit your code to put your usings inside. Also, less indent and fewer curly braces to worry about are benefits for sure. |
Given that this proposal requires explicitly using a new syntax which wasn't previously legal this wouldn't result in any breaking changes to existing code. The proposal is indeed minor, but I think reduction of noise indentation is not a bad thing. For the vast majority of source files it's a wasted block of horizontal space, particularly given how rare it actually is to have more than one namespace (nested or otherwise) in a single source file. |
Ok, let me reformulate in the following way:
|
File scoped static classes would be great too like F# modules. namespace Users
{
public static class Create
{
public class Command
{
....
}
public class Handler
{
....
}
}
} With scoped static class module Users.Create;
public class Command
{
....
}
public class Handler
{
....
} |
This thread is huge so apologies if this was already brought up. While I agree with you @jnm2 that this would be a lot cleaner/simpler, it removes the possibility of adding using namespaces inside the namespace, like this: namespace My.Namespace
{
using System;
public class ClassName
{
}
} If the syntax you are proposing was to be allowed, I'd also like to see this being possible: public class My.Namespace.ClassName
{
using System;
public ClassName()
{
...
}
public string SomeProperty { get; set; }
}
} (i.e. namespace references inside the class). |
Another problem with what I wanted is that it's not clear how it could work with nested classes, a prime use case for me. I think this would be best as a shortcut for wrapping with namespace A.B;
class C.D
{ |
As of now, file scoped Namespaces don't work with VS2019 (both preview and non-preview). |
@CleanCodeX FileScopedNamespaces are part of C# 10. So you'll need to use a tool that supports that version of the language. All our IDE support went into the 17.0.x line (i.e. VS2022). |
It seems, people (including me) are a bit confused by the fact that some features are a feature of .NET and some of the C# compiler. |
My confusion is based on that: |
There were some preview features of 10 that went into VS2019. But that's all. |
Will FSN find their way into VS2019 as well? |
No. VS2019 is effectively locked down now, with only highly critical fixes being made there (think "bad crashes affecting millions"). Shipping preview features in it will not happen. |
This is correct. 'global usings' are part of C# 10.
VS2019 has 'preview' support for 'some' C# 10 features. But it doesn't have 'final' support for 'all' c# 10 features. 'preview' allows people to test things out and gives us a chance to make thigns available earlier to get feedback. But not all features will make it into preview in a particular VS release. It will depend on when in the schedule we implement the feature in preview and how that aligns with particular VS schedules.
This is not something we guarantee. As per above, not all features make it into a preview release for a particular VS build. global-usings made it into a preview build for VS2019, FSN made it into a preview build for VS2022. |
Sorry, I meant will FSN find their way into VS2019 preview as well? |
No. |
Shouldn't this issue be closed? It went live in the last release right? |
The ecma spec has not been updated for this yet, so it is not finished @mungojam . |
@mungojam FYI, that's what the "Implemented needs ECMA spec" label on this issue means, although of course that's easily missed if one doesn't know to look for it 🙂 |
That makes sense. I actually came here because it was listed on the future roadmap of c# changes and showing as open. Should it be removed from there? |
Spec: https://github.com/dotnet/csharplang/blob/main/proposals/csharp-10.0/file-scoped-namespaces.md
How it looks now:
Why do we need to nest our class definition in namespace ? Why not to remove extra nesting ?
Isn't that better ?
LDM history:
The text was updated successfully, but these errors were encountered: