-
Notifications
You must be signed in to change notification settings - Fork 4.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
Making global namespaces available to an assembly? good idea? useful? what do you think? #14547
Comments
Related: #2044 |
@alrz yeah it's related but very different approach, will see what people think about this. |
That doesn't sound like a good idea to me. AssemblyInfo.cs is currently not special in any way and you want to explicitly name it in the specification? Besides, when compiling using the Roslyn API, there may not even be file names.
C# currently does not have dialects, this does not look like a good reason to start to me. Why would it need to be opt-in or opt-out in the first place? People who don't like it can just not use it, like with all the various features that C# currently has. |
Note: VB supports this through |
You're right, then we can just put these namespaces everywhere we would like and then opt-in to them in other files.
Sure, I've just thrown some ideas to see what the community think. :) @vbcodec I don't think that anything that touches the file system in such a way is a great idea, I don't think I want C# to be like C++/TypeScript, I have enough on my plate dealing with them. @alrz Is it strictly VB feature? |
switch to VB, and go to project's settings where you can set globally available namespaces |
@vbcodec What do you mean by switch to VB? I get the point where VB has this feature but why would it be strictly a VB thing? I'm not a VB programmer and I wouldn't convert projects to VB just to have this feature, that's kinda silly, it's like telling people to convert their projects to another language just because this language has a feature they need or not but they still think that it can be useful to add it to their favorite language. People hate options? or what's the issue? |
I wouldn't tie this information to AssemblyInfo as it isn't in any way special from the compiler point of view. What sounds reasonable to me is that it should:
Which leads me to this design (intentionally similar to
|
I kinda like that approach. If I understand correctly, you can still omit If implemented that way, I'd like to suggest for the IDE experience: Hovering over the |
Exactly
That would be awesome |
I think it would be more interesting to use different approach. Introduce "using batches" concept ( // put at any C# file
namespace MyProject
{
using as DefaultUsings {
System,
System.IO,
System.Xml,
System.Linq,
static Testing.Utilities.Lambda
};
} And then you can just reference all bunched together usings in one line // prefixed with "MyProject" because usings batch is declared inside namespace!
using MyProject.DefaultUsings; Using batches shouldn't be exported in binary DLL. They should be meaningful only for current project source files. |
@Opiumtm hmm this is pretty cool! but I'd go with the following syntax:
I think that the static modifier is redundant because the compiler can infer that it's a static class and do the appropriate thing but that's just an assumption. Anyway I like this because it will allow us to group multiple using statements and import them in other places, however, I see two issues here:
|
Yes, they should merge and throw compilation warning. To fight ambiguity to some degree, batch usings can be declared inside namespace and then referenced as if it's a part of namespace. Things that are legal, but potentially problematic ("usings batch" with the same name inside same namespace declared and merged across different source files is a potential maintainability problem) should throw warning at compilation. So, developer would see it's a legal, but definitely bad practice. |
@Opiumtm Warnings can be left to analyzers. :) |
@eyalsk And I think, recursive using batches (using batch that contains another using batch) should be forbidden as it would introduce complex namespace dependency graph with possible cycles. So, using batches should contain only regular namespaces or static types. |
@Opiumtm Yeah, I think so too. :) |
I agree with having it explicit, and in-language — if But not sure how much benefit named batches would bring over a default set, can't think of many use cases. Something simple like |
I think it should be illegal. Merge would be unnecessary complexity that doesn't add any value. |
@zippec Well, it has some value but I agree with you, I don't think it should be allowed. |
Just brainstorming here, but what about calling this "namespacegroup"? It would communicate what it is, and make it impossible for older compilers to understand (since it would be a new keyword, and as explained below also a syntax error). To be 100% clear, what I had in mind is (at point-of-use): using namespacegroup Foo.Bar.MyNamespaceGroup; It would not increase count of '#' directives (those should be kept on a very short leach), while make it 100% clear for even a casual observer what's going on. Some of the remaining issues would be to define a namespacegroup, specification of such a beast, and how to specify specific types by "whole path". Would f.ex. "global::MyNamespaceGroup.SomeType" be valid after such a fictional using directive? I like the idea, if nothing else for the intellectual challenge, but I also note Here Be Dragons! :-) |
Why do we need to introduce a new keyword? it would be an error anyhow with or without the new keyword and the if you already used the name for a namespace in the same assembly then you can't use it for the namespace group, BTW, it's not really clear whether you support @zippec's idea or @Opiumtm's idea, I mean in the first part of your post it seems like you relate to @Opiumtm's idea but then you mentioned directives and this is another idea so I'm not sure. Anyway, I hope I understood you, probably not? 😄 |
@eyalsk With a standing out keyword, it would not provide any risk of surprises (this is something I consider vitally important). I like the concept of being able to reduce amount of "includes" (a.k.a. "using"), but when it comes to lumping multiple namespace includes into a single include, I think it would be better to be explicit about what is going on - hence the "*group" keyword idea. |
@tamlin-mike I'm not quite sure what surprises you think you may run into? The IDE can help by displaying the using statements it imports, including the file where the group is defined inside a tooltip and finally it can color it differently so people would know it's not a regular using statement. Not to mention that namespacegroup doesn't make sense because it can also contain static classes so maybe 'using group Foo.Bar.MyNamespaceGroup;` but again, I don't think it's really necessary. :) |
@Eylask: The point I was trying to make was that that the keyword would make it stand out (even without a "fancy" IDE - think: looking at the code in Notepad). I willingly admit, I haven't given this the amount of thought it probably deserve, but I'm still quite convinced this feature deserves a keyword of its own (and that it only is used to introduce namespaces - nothing else). Perhaps my view is oversimplifying the potential issues (wouldn't be the first time). |
@tamlin-mike maybe, that's for the design team to decide iff this feature will ever make it.. haha.. :) |
Hey guys,
Many times, mostly in my testing projects I need to repeat myself over and over again and I have a bunch of namespaces that exists in all of my testing files.
Something like this:
Opiumtm's approach:
I've modified the syntax slightly.
Restrictions:
MyProject.SomeUsings
cannot be used again butMyProject2.SomeUsings
should be valid.Zippec's approach:
Both approaches are good enough for me and I like them both, personally I'd go with @Opiumtm suggestion.
Edit:
AssemblyInfo.cs
.The text was updated successfully, but these errors were encountered: