Compare commits

...

12 commits

Author SHA1 Message Date
CyrusNajmabadi 44c5009151
Update namespaces.md 2021-02-16 12:22:13 -08:00
CyrusNajmabadi e13251ed65
Update spec/namespaces.md 2020-10-27 14:01:15 -04:00
CyrusNajmabadi 2940595a94
Update spec/namespaces.md 2020-10-27 14:00:27 -04:00
CyrusNajmabadi 67e19ac51f
Update namespaces.md 2020-10-27 10:59:35 -07:00
CyrusNajmabadi 3fe82297b5
Update spec/namespaces.md 2020-10-27 03:12:18 -04:00
CyrusNajmabadi f8550ef836
Update spec/namespaces.md 2020-10-27 03:10:59 -04:00
CyrusNajmabadi 5fd8498724
Update spec/namespaces.md 2020-10-27 03:09:28 -04:00
CyrusNajmabadi aab7c2bef3
Update namespaces.md 2020-10-27 00:08:47 -07:00
CyrusNajmabadi 5c26a69c8e
Update namespaces.md 2020-10-27 00:06:52 -07:00
CyrusNajmabadi 15e5536305
Update namespaces.md 2020-10-27 00:02:34 -07:00
CyrusNajmabadi f4e41ad808
Update namespaces.md 2020-10-27 00:00:32 -07:00
CyrusNajmabadi 810b2abd20
Update namespaces.md 2020-10-26 18:05:08 -07:00

View file

@ -6,11 +6,16 @@ Using directives ([Using directives](namespaces.md#using-directives)) are provid
## Compilation units
A *compilation_unit* defines the overall structure of a source file. A compilation unit consists of zero or more *using_directive*s followed by zero or more *global_attributes* followed by zero or more *namespace_member_declaration*s.
A *compilation_unit* defines the overall structure of a source file. A compilation unit consists of zero or more *using_directive*s followed by zero or more *global_attributes* followed by a *compilation_unit_body*. A *compilation_unit_body* can either be a *single_line_namespace* or zero or more *namespace_member_declaration*s.
```antlr
compilation_unit
: extern_alias_directive* using_directive* global_attributes? namespace_member_declaration*
: extern_alias_directive* using_directive* global_attributes? compilation_unit_body
;
compilation_unit_body
: namespace_member_declaration*
| single_line_namespace
;
```
@ -34,14 +39,21 @@ class B {}
The two compilation units contribute to the single global namespace, in this case declaring two classes with the fully qualified names `A` and `B`. Because the two compilation units contribute to the same declaration space, it would have been an error if each contained a declaration of a member with the same name.
A *single_line_namespace* will contribute members corresponding to the *namespace_declaration* it is semantically equivalent to. See ([Namespace Declarations](#namespace-declarations)) for more details.
## Namespace declarations
A *namespace_declaration* consists of the keyword `namespace`, followed by a namespace name and body, optionally followed by a semicolon.
A *single_line_namespace_declaration* consists of the keyword `namespace`, followed by a namespace name, a semicolon and an optional list of directives and type declarations.
```antlr
namespace_declaration
: 'namespace' qualified_identifier namespace_body ';'?
;
single_line_namespace_declaration
: 'namespace' qualified_identifier ';' extern_alias_directive* using_directive* type_declaration*
;
qualified_identifier
: identifier ('.' identifier)*
@ -95,6 +107,35 @@ namespace N1.N2
```
the two namespace declarations above contribute to the same declaration space, in this case declaring two classes with the fully qualified names `N1.N2.A` and `N1.N2.B`. Because the two declarations contribute to the same declaration space, it would have been an error if each contained a declaration of a member with the same name.
A *single_line_namespace_declaration* permits a namespace declaration to be written without the `{ ... }` block. For example:
```csharp
namespace Name;
extern alias A;
using B;
class C
{
}
```
is semantically equivalent to
```csharp
namespace Name
{
extern alias A;
using B;
class C
{
}
}
```
Specifically, a *single_line_namespace_declaration* is treated the same as a *namespace_declaration* at the same location in the *compilation_unit* with the same *qualified_identifier*. The *extern_alias_directive*s, *using_directive*s and *type_declaration*s of that *single_line_namespace_declaration* act as if they were declared in the same order inside the *namespace_body* of that *namespace_declaration*.
A source file cannot contain both a *single_line_namespace_declaration* and a *namespace_declaration*. A source file cannot contain multiple *single_line_namespace_declaration*s. A *compilation_unit* cannot contain both a *single_line_namespace_declaration* and any *statement* at the top level.
## Extern aliases
An *extern_alias_directive* introduces an identifier that serves as an alias for a namespace. The specification of the aliased namespace is external to the source code of the program and applies also to nested namespaces of the aliased namespace.