Champion issue: (TODO)
Allow some members (methods, operators, extension blocks, and fields) to be declared in namespaces and make them available when the corresponding namespace is imported.
// util.cs
namespace MyApp;
void Print(string s) => Console.WriteLine(s);
string Capitalize(this string input) =>
input.Length == 0 ? input : char.ToUpper(input[0]) + input[1..];// app.cs
#!/usr/bin/env dotnet
using MyApp;
Print($"Hello, {args[0].Capitalize()}!");// Fields are useful:
namespace MyUtils;
string? cache;
string GetValue() => cache ??= Compute();// Simplifies extensions:
namespace System.Linq;
extension<T>(IEnumerable<T> e)
{
public IEnumerable<T> AsEnumerable() => e;
}TODO: Why are we doing this? What use cases does it support? What is the expected outcome?
- Avoid boilerplate utility static classes.
- Evolve top-level statements from C# 9.
TODO: This is the bulk of the proposal. Explain the design in enough detail for somebody familiar with the language to understand, and for somebody familiar with the compiler to implement, and include examples of how the feature is used. This section can start out light before the prototyping phase but should get into specifics and corner-cases as the feature is iteratively designed and implemented.
- 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.
- It is as if the members were in an "implicit"
staticclass whose accessibility is eitherinternal(by default) orpublic(if any member is alsopublic). For top-level members, this means:- The
staticmodifier is disallowed (the members are implicitly static). - The default accessibility is
internal.publicandprivateis also allowed.protectedandfileis disallowed. - Overloading is supported.
externandpartialare supported.- XML doc comments work.
- The
- Metadata:
- A type synthesized per namespace and file. That means
privatemembers are only visible in the file. - Cannot be addressed from C#, but has speakable name
TopLevelso it is callable from other languages. This means that custom types namedTopLevelbecome disallowed in a namespace where top-level members are used.
- A type synthesized per namespace and file. That means
- Usage:
using NS;impliesusing static NS.TopLevel;.- Lookup for
NS.Method()can findNS.TopLevel.Method(). - Nothing really changes for extensions.
- Entry points:
- Top-level
Mainmethods can be entry points. - Top-level statements are generated into
Program.Main(speakable function). This is a breaking change (previously the main method was unspeakable). - Simplify the logic: TLS entry-points are normal candidates.
This is a breaking change (previously they were not considered to be candidates and for example
-maincould not be used to point to them).
- Top-level
TODO: Why should we not do this?
- Polluting namespaces with loosely organized helpers.
- Requires tooling updates to properly surface and organize top-level methods in IntelliSense, refactorings, etc.
- Entry point resolution breaking changes.
TODO: What other designs have been considered? What is the impact of not doing this?
- Support
argskeyword in top-level members (just like it can be accessed in top-level statements). But we haveSystem.Environment.GetCommandLineArgs(). - Allow capturing variables from top-level statements inside non-
statictop-level members. Could be used to refactor a single-file program into multi-file program just by extracting functions to separate files. But it would mean that a method's implementation (top-level statements) can influence what other methods see (which variables are available in top-level members). - Allow declaring top-level members outside namespaces as well.
- Would introduce ambiguities with top-level statements.
- Could be brought to scope via
extern alias.- To avoid needing to specify those in project files (e.g., so file-based apps also work),
there could be a syntax for that like
extern alias Util = Util.dll.
- To avoid needing to specify those in project files (e.g., so file-based apps also work),
there could be a syntax for that like
TODO: What parts of the design are still undecided?
- Which member kinds? Methods, fields, properties, indexers, events, constructors, operators.
- Allow
fileorprivateor both? What shouldprivatereally mean? Visible to file, namespace, or something else? - Name for the speakable static class (currently
TopLevel)? Should it be speakable at all? - Should we simplify the TLS entry point logic? Should it be a breaking change?
- Should we require the
staticmodifier (and keep our doors open if we want to introduce some non-statictop-level members in the future)?