Top-level members #9802
Replies: 9 comments 23 replies
-
|
I'm still surprised it took so long to get to this considering all the work done for Will definitely help with language adoption and may be useful in some enterprise contexts too. |
Beta Was this translation helpful? Give feedback.
-
|
This is a loooong-requested feature, and I am surprised (but very happy!) to see it being considered since I was sure the discussion was closed as "Likely never" the last time |
Beta Was this translation helpful? Give feedback.
-
|
Copying in the link to the markdown document: https://github.com/jjonescz/csharplang/blob/tlm/proposals/top-level-members.md |
Beta Was this translation helpful? Give feedback.
-
|
I think this is a great idea, but I have a few concerns with the proposal: I don't think fields (or auto properties) are a good idea (certainly not for the first release; once out in the wild maybe my concerns will be assuaged). Global mutable state that floats around outside of a class is bad juju, in my opinion; it's fine for methods (which will become 'functions') but allowing state will just encourage people to start using files as file-scoped classes. Regarding mixing type declarations and top-level members in the same file: it feels like this should be allowed but there's a question about how it gets lowered; namely are the declared types nested inside the implicit type? I'd say they shouldn't be, otherwise adding a member to a file would cause the declared types to "move", perhaps disappear entirely (if their visibility is left to the default). |
Beta Was this translation helpful? Give feedback.
-
|
How does this exist differently than the existing top-level members that are considered locals to an implicit constructor of the Program class? |
Beta Was this translation helpful? Give feedback.
-
|
I'm copying a comment I made on the PR because perhaps that wasn't the best place to put it: (With respect to what happens if you mix top-level members and types in the same file). My mental model of how this should work goes like this: Rather than thinking "If this file contains top-level members then lift the contents of the file into a generated class" I think "For each declaration in this file, if it's a member, then lift it into a generated class". So namespace N;
void M1() {}
class C1 {}
void M2() {}
class C2 {}
void M3() {}becomes namespace N;
partial static class TopLevel
{
void M1() {}
}
class C1 {}
partial static class TopLevel
{
void M2() {}
}
class C2 {}
partial static class TopLevel
{
void M3() {}
}rather than namespace N;
static class TopLevel
{
void M1() {}
class C1 {}
void M2() {}
class C2 {}
void M3() {}
} |
Beta Was this translation helpful? Give feedback.
-
|
I confused about this proposal. Sure, C++ and Rust support them, but the LDT’s consistent argument to this feature through the years is “ I’m not against it. Just wondering “what changed?” |
Beta Was this translation helpful? Give feedback.
-
|
Are there examples of code that would benefit from this so much? I fail to see how this is better than "global using static ...". I think if user specifically wants members of some static class to be discoverable in ecope of namespace then a simple attribute similar to F#'s AutoOpen would suffice. |
Beta Was this translation helpful? Give feedback.
-
|
If the main point is to make C# more beginner-friendly, then the idea is to reduce code overhead? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: #9719 (markdown)
Champion issue: #9803
Beta Was this translation helpful? Give feedback.
All reactions