microsoft / qsharp-language

Official repository for design of the quantum programming language Q# and its core libraries
MIT License
235 stars 56 forks source link

File-scoped namespace statements #98

Open cgranade opened 3 years ago

cgranade commented 3 years ago

Suggestion

Allow namespace statements to end with a semicolon instead of a block, indicating that the namespace declaration applies to the entire file.

Considerations

Most Q# source files declare exactly one namespace, such that the extra level of indenting is unnecessary in most common cases. Allowing namespace declarations to be file-scoped would cut down on the amount of boilerplate and nesting, making Q# source files easier to read (esp. for newer users).

A similar discussion has led to the proposal and likely adoption of a similar feature into C# 10 (https://github.com/dotnet/csharplang/blob/main/proposals/csharp-10.0/file-scoped-namespaces.md).

Context

This proposal would not change any Q# semantics, but would provide a useful shorthand for existing features and semantics.

Examples

Example 1:
Using file-scoped namespaces to apply a namespace to all declarations in a file.

namespace Microsoft.Quantum.Foo;

// This function is declared with the fully
// qualified name Microsoft.Quantum.Foo.A.
function A() : String {
    return "aaaaaa";
}

Example 2: Mixing file- and block-scoped namespaces is forbidden.

namespace Microsoft.Quantum.Foo;

namespace Bar { // 💣: can't mix file- and block-scope namespace declarations.

}

Affidavit (please fill out)

Please add ticks by placing a cross in the box:

Please tick all that apply: