dlr bug
[mcs.git] / docs / ecma334 / 16.3.1.xml
blob9d821aeb730c71015783c9bcaaefb1bdd2eb704b
1 <?xml version="1.0"?>
2 <clause number="16.3.1" title="Using alias directives">
3   <paragraph>A <non_terminal where="16.3.1">using-alias-directive</non_terminal> introduces an identifier that serves as an alias for a namespace or type within the immediately enclosing compilation unit or namespace body. <grammar_production><name><non_terminal where="16.3.1">using-alias-directive</non_terminal></name> : <rhs><keyword>using</keyword><non_terminal where="9.4.2">identifier</non_terminal><terminal>=</terminal><non_terminal where="10.8">namespace-or-type-name</non_terminal><terminal>;</terminal></rhs></grammar_production></paragraph>
4   <paragraph>Within member declarations in a compilation unit or namespace body that contains a <non_terminal where="16.3.1">using-alias-directive</non_terminal>, the identifier introduced by the <non_terminal where="16.3.1">using-alias-directive</non_terminal> can be used to reference the given namespace or type. </paragraph>
5   <paragraph>
6     <example>[Example: For example: <code_example><![CDATA[
7 namespace N1.N2  
8 {  
9    class A {}  
10 }  
11 namespace N3  
12 {  
13    using A = N1.N2.A;  
14    class B: A {}  
15 }  
16 ]]></code_example></example>
17   </paragraph>
18   <paragraph>
19     <example>Above, within member declarations in the N3 namespace, A is an alias for N1.N2.A, and thus class N3.B derives from class N1.N2.A. The same effect can be obtained by creating an alias R for N1.N2 and then referencing R.A: <code_example><![CDATA[
20 namespace N3  
21 {  
22    using R = N1.N2;  
23    class B: R.A {}  
24 }  
25 ]]></code_example>end example]</example>
26   </paragraph>
27   <paragraph>The identifier of a <non_terminal where="16.3.1">using-alias-directive</non_terminal> must be unique within the declaration space of the compilation unit or namespace that immediately contains the <non_terminal where="16.3.1">using-alias-directive</non_terminal>. <example>[Example: For example: <code_example><![CDATA[
28 namespace N3  
29 {  
30    class A {}  
31 }  
32 namespace N3  
33 {  
34    using A = N1.N2.A;    // Error, A already exists  
35 }  
36 ]]></code_example></example></paragraph>
37   <paragraph><example>Above, N3 already contains a member A, so it is a compile-time error for a <non_terminal where="16.3.1">using-alias-directive</non_terminal> to use that identifier. end example]</example> Likewise, it is a compile-time error for two or more <non_terminal where="16.3.1">using-alias-directive</non_terminal>s in the same compilation unit or namespace body to declare aliases by the same name. </paragraph>
38   <paragraph>A <non_terminal where="16.3.1">using-alias-directive</non_terminal> makes an alias available within a particular compilation unit or namespace body, but it does not contribute any new members to the underlying declaration space. In other words, a  <non_terminal where="16.3.1">using-alias-directive</non_terminal> is not transitive, but, rather, affects only the compilation unit or namespace body in which it occurs. </paragraph>
39   <paragraph>
40     <example>[Example: In the example <code_example><![CDATA[
41 namespace N3  
42 {  
43    using R = N1.N2;  
44 }  
45 namespace N3  
46 {  
47    class B: R.A {}      // Error, R unknown  
48 }  
49 ]]></code_example>the scope of the <non_terminal where="16.3.1">using-alias-directive</non_terminal> that introduces R only extends to member declarations in the namespace body in which it is contained, so R is unknown in the second namespace declaration. However, placing the <non_terminal where="16.3.1">using-alias-directive</non_terminal> in the containing compilation unit causes the alias to become available within both namespace declarations: <code_example><![CDATA[
50 using R = N1.N2;  
51 namespace N3  
52 {  
53    class B: R.A {}  
54 }  
55 namespace N3  
56 {  
57    class C: R.A {}  
58 }  
59 ]]></code_example>end example]</example>
60   </paragraph>
61   <paragraph>Just like regular members, names introduced by <non_terminal where="16.3.1">using-alias-directive</non_terminal>s are hidden by similarly named members in nested scopes. <example>[Example: In the example <code_example><![CDATA[
62 using R = N1.N2;  
63 namespace N3  
64 {  
65    class R {}  
66    class B: R.A {}    // Error, R has no member A  
67 }  
68 ]]></code_example>the reference to R.A in the declaration of B causes a compile-time error because R refers to N3.R, not N1.N2. end example]</example> </paragraph>
69   <paragraph>The order in which <non_terminal where="16.3.1">using-alias-directive</non_terminal>s are written has no significance, and resolution of the  <non_terminal where="10.8">namespace-or-type-name</non_terminal> referenced by a <non_terminal where="16.3.1">using-alias-directive</non_terminal> is not affected by the <non_terminal where="16.3.1">using-alias-directive</non_terminal> itself or by other <non_terminal where="16.3">using-directive</non_terminal>s in the immediately containing compilation unit or namespace body. In other words, the <non_terminal where="10.8">namespace-or-type-name</non_terminal> of a <non_terminal where="16.3.1">using-alias-directive</non_terminal> is resolved as if the immediately containing compilation unit or namespace body had no <non_terminal where="16.3">using-directive</non_terminal>s. <example>[Example: In the example <code_example><![CDATA[
70 namespace N1.N2 {}  
71 namespace N3  
72 {  
73    using R1 = N1;   // OK  
74    using R2 = N1.N2;   // OK  
75    using R3 = R1.N2;   // Error, R1 unknown  
76 }  
77 ]]></code_example>the last <non_terminal where="16.3.1">using-alias-directive</non_terminal> results in a compile-time error because it is not affected by the first  <non_terminal where="16.3.1">using-alias-directive</non_terminal>. end example]</example> </paragraph>
78   <paragraph>A <non_terminal where="16.3.1">using-alias-directive</non_terminal> can create an alias for any namespace or type, including the namespace within which it appears and any namespace or type nested within that namespace. </paragraph>
79   <paragraph>Accessing a namespace or type through an alias yields exactly the same result as accessing that namespace or type through its declared name. <example>[Example: For example, given <code_example><![CDATA[
80 namespace N1.N2  
81 {  
82    class A {}  
83 }  
84 namespace N3  
85 {  
86    using R1 = N1;  
87    using R2 = N1.N2;  
88    class B  
89    {  
90       N1.N2.A a;    // refers to N1.N2.A  
91       R1.N2.A b;    // refers to N1.N2.A  
92       R2.A c;     // refers to N1.N2.A  
93    }  
94 }  
95 ]]></code_example>the names N1.N2.A, R1.N2.A, and R2.A are equivalent and all refer to the class whose fully qualified name is N1.N2.A. end example]</example> </paragraph>
96 </clause>