authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-07-30 11:17:45+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-07-31 10:39:22+01:00
logbce6a7c21529e9b294a641b5d7472c000204aaef
tree8dd693fd3b3ed50e9461ecc7bda59c60bbae765b
parente941ce3e689991081d0ae726bfa1ccccf4fc196f

langref: improve `@import` documentation

Rewrite to be more clear and correct. Also, explain ZON imports. Resolves: #23314

1 files changed, 17 insertions(+), 26 deletions(-)

doc/langref.html.in+17-26
...@@ -4952,34 +4952,25 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val...@@ -4952,34 +4952,25 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
4952 {#header_close#}4952 {#header_close#}
49534953
4954 {#header_open|@import#}4954 {#header_open|@import#}
4955 <pre>{#syntax#}@import(comptime path: []const u8) type{#endsyntax#}</pre>4955 <pre>{#syntax#}@import(comptime target: []const u8) anytype{#endsyntax#}</pre>
4956 <p>4956 <p>Imports the file at {#syntax#}target{#endsyntax#}, adding it to the compilation if it is not already
4957 This function finds a zig file corresponding to {#syntax#}path{#endsyntax#} and adds it to the build,4957 added. {#syntax#}target{#endsyntax#} is either a relative path to another file from the file containing
4958 if it is not already added.4958 the {#syntax#}@import{#endsyntax#} call, or it is the name of a {#link|module|Compilation Model#}, with
4959 </p>4959 the import referring to the root source file of that module. Either way, the file path must end in
4960 <p>4960 either <code>.zig</code> (for a Zig source file) or <code>.zon</code> (for a ZON data file).</p>
4961 Zig source files are implicitly structs, with a name equal to the file's basename with the extension4961 <p>If {#syntax#}target{#endsyntax#} refers to a Zig source file, then {#syntax#}@import{#endsyntax#} returns
4962 truncated. {#syntax#}@import{#endsyntax#} returns the struct type corresponding to the file.4962 that file's {#link|corresponding struct type|Source File Structs#}, essentially as if the builtin call was
4963 </p>4963 replaced by {#syntax#}struct { FILE_CONTENTS }{#endsyntax#}. The return type is {#syntax#}type{#endsyntax#}.</p>
4964 <p>4964 <p>If {#syntax#}target{#endsyntax#} refers to a ZON file, then {#syntax#}@import{#endsyntax#} returns the value
4965 Declarations which have the {#syntax#}pub{#endsyntax#} keyword may be referenced from a different4965 of the literal in the file. If there is an inferred {#link|result type|Result Types#}, then the return type
4966 source file than the one they are declared in.4966 is that type, and the ZON literal is interpreted as that type ({#link|Result Types#} are propagated through
4967 </p>4967 the ZON expression). Otherwise, the return type is the type of the equivalent Zig expression, essentially as
4968 <p>4968 if the builtin call was replaced by the ZON file contents.</p>
4969 {#syntax#}path{#endsyntax#} can be a relative path or it can be the name of a package.4969 <p>The following modules are always available for import:</p>
4970 If it is a relative path, it is relative to the file that contains the {#syntax#}@import{#endsyntax#}
4971 function call.
4972 </p>
4973 <p>
4974 The following packages are always available:
4975 </p>
4976 <ul>4970 <ul>
4977 <li>{#syntax#}@import("std"){#endsyntax#} - Zig Standard Library</li>4971 <li>{#syntax#}@import("std"){#endsyntax#} - Zig Standard Library</li>
4978 <li>{#syntax#}@import("builtin"){#endsyntax#} - Target-specific information4972 <li>{#syntax#}@import("builtin"){#endsyntax#} - Target-specific information. The command <code>zig build-exe --show-builtin</code> outputs the source to stdout for reference.</li>
4979 The command <code>zig build-exe --show-builtin</code> outputs the source to stdout for reference.4973 <li>{#syntax#}@import("root"){#endsyntax#} - Alias for the root module. In typical project structures, this means it refers back to <code>src/main.zig</code>.
4980 </li>
4981 <li>{#syntax#}@import("root"){#endsyntax#} - Root source file
4982 This is usually <code>src/main.zig</code> but depends on what file is built.
4983 </li>4974 </li>
4984 </ul>4975 </ul>
4985 {#see_also|Compile Variables|@embedFile#}4976 {#see_also|Compile Variables|@embedFile#}