authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-08-31 17:01:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-01 17:54:07-07:00
logfeec4b0614c1fd64fa59634af8774a992c7a5646
treed0613028b86b6e4dd540f84ada64939d35deee4a
parentf4d3d29f925c370ec7a33e20fcce917bb90f48a8

langref: new usingnamespace semantics


1 files changed, 8 insertions(+), 19 deletions(-)

doc/langref.html.in+8-19
...@@ -5812,32 +5812,20 @@ test "@intToPtr for pointer to zero bit type" {...@@ -5812,32 +5812,20 @@ test "@intToPtr for pointer to zero bit type" {
58125812
5813 {#header_open|usingnamespace#}5813 {#header_open|usingnamespace#}
5814 <p>5814 <p>
5815 {#syntax#}usingnamespace{#endsyntax#} is a declaration that imports all the public declarations of5815 {#syntax#}usingnamespace{#endsyntax#} is a declaration that mixes all the public
5816 the operand, which must be a {#link|struct#}, {#link|union#}, or {#link|enum#}, into the current scope:5816 declarations of the operand, which must be a {#link|struct#}, {#link|union#}, {#link|enum#},
5817 or {#link|opaque#}, into the namespace:
5817 </p>5818 </p>
5818 {#code_begin|test|usingnamespace#}5819 {#code_begin|test|usingnamespace#}
5819usingnamespace @import("std");
5820
5821test "using std namespace" {5820test "using std namespace" {
5822 try testing.expect(true);5821 const S = struct {
5823}5822 usingnamespace @import("std");
5824 {#code_end#}
5825 <p>
5826 {#syntax#}usingnamespace{#endsyntax#} can also be used in containers:
5827 </p>
5828 {#code_begin|test|usingnamespace_inside_struct#}
5829test "using namespace inside struct" {
5830 const L = struct {
5831 usingnamespace struct {
5832 pub fn f() void {}
5833 };
5834 };5823 };
5835 L.f();5824 try S.testing.expect(true);
5836}5825}
5837 {#code_end#}5826 {#code_end#}
5838 <p>5827 <p>
5839 Instead of the above pattern, it is generally recommended to explicitly alias individual declarations.5828 {#syntax#}usingnamespace{#endsyntax#} has an important use case when organizing the public
5840 However, {#syntax#}usingnamespace{#endsyntax#} has an important use case when organizing the public
5841 API of a file or package. For example, one might have <code>c.zig</code> with all of the5829 API of a file or package. For example, one might have <code>c.zig</code> with all of the
5842 {#link|C imports|Import from C Header File#}:5830 {#link|C imports|Import from C Header File#}:
5843 </p>5831 </p>
...@@ -5858,6 +5846,7 @@ pub usingnamespace @cImport({...@@ -5858,6 +5846,7 @@ pub usingnamespace @cImport({
5858 </p>5846 </p>
5859 {#header_close#}5847 {#header_close#}
58605848
5849
5861 {#header_open|comptime#}5850 {#header_open|comptime#}
5862 <p>5851 <p>
5863 Zig places importance on the concept of whether an expression is known at compile-time.5852 Zig places importance on the concept of whether an expression is known at compile-time.