| ... | @@ -5812,32 +5812,20 @@ test "@intToPtr for pointer to zero bit type" { | ... | @@ -5812,32 +5812,20 @@ test "@intToPtr for pointer to zero bit type" { |
| 5812 | | 5812 | |
| 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 of | 5815 | {#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#} |
| 5819 | usingnamespace @import("std"); | | |
| 5820 | | | |
| 5821 | test "using std namespace" { | 5820 | test "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#} | | |
| 5829 | test "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 the | 5829 | 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#} |
| 5860 | | 5848 | |
| | 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. |