Skip to content

Conversation

jjonescz
Copy link
Member

Markdown

Just an outline for now if anyone has any comments before I start filling in details.

cc @jaredpar as the LDM champion for this feature

@jjonescz jjonescz requested a review from a team as a code owner September 26, 2025 14:53

- Some members can be declared directly in a namespace (file-scoped or block-scoped).
- Allowed kinds currently are: methods, operators, extension blocks, and fields.
- Existing declarations like classes still work the same, there shouldn't be any ambiguity.
Copy link
Member

@jcouv jcouv Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top-level statements can co-exist with type declarations, but must come first. Is there a similar rule for top-level members?

Would such types be in the namespace or nested in the TopLevel type? From the spec it should be in the namespace (since types are not allowed as top-level members), but that's surprising. So it may be better to just disallow them...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question, I will add it to the list. But I don't find the behavior surprising, consider that you have an existing code like

namespace N;
class C;

and you decide to add a top-level member, for example

namespace N;
int M() => 42;
class C;

that could be disallowed but if it's allowed it seems natural that the class remains directly in the namespace as it was before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's allowed it seems natural that the class remains directly in the namespace as it was before.

That's arguable, but not obvious. What if I sandwich the type?

namespace N;
int M() => 42;
class C;
int M2() => 42;

It doesn't seem obvious why M() and M2() are in N.TopLevel, but C is directly in N

- Some members can be declared directly in a namespace (file-scoped or block-scoped).
- Allowed kinds currently are: methods, operators, extension blocks, and fields.
- Existing declarations like classes still work the same, there shouldn't be any ambiguity.
- There is no ambiguity with top-level statements because those are not allowed inside namespaces.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit unfortunate that top-level members only be allowed inside namespaces. It seems irregular. But it does help make things unambiguous... Probably worth discussing more

- Cannot be addressed from C#, but has speakable name `TopLevel` so it is callable from other languages.
This means that custom types named `TopLevel` become disallowed in a namespace where top-level members are used.
- Usage:
- `using NS;` implies `using static NS.TopLevel;`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I already have a namespace "N" with a type named "TopLevel" in metadata, will using N; gain the new behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, good question. Perhaps there should be some attribute akin to [Extension].


## Open questions

TODO: What parts of the design are still undecided?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rules for name conflicts should probably be spelled out.

For example, it seem as if the following declarations would be in conflict, due to how they are represented in the language

namespace NS;
int Foo;
class Foo { }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants