| ... | @@ -233,7 +233,7 @@ | ... | @@ -233,7 +233,7 @@ |
| 233 | const std = @import("std"); | 233 | const std = @import("std"); |
| 234 | | 234 | |
| 235 | pub fn main() !void { | 235 | pub fn main() !void { |
| 236 | const stdout = std.io.getStdOut().outStream(); | 236 | const stdout = std.io.getStdOut().writer(); |
| 237 | try stdout.print("Hello, {}!\n", .{"world"}); | 237 | try stdout.print("Hello, {}!\n", .{"world"}); |
| 238 | } | 238 | } |
| 239 | {#code_end#} | 239 | {#code_end#} |
| ... | @@ -269,16 +269,25 @@ pub fn main() !void { | ... | @@ -269,16 +269,25 @@ pub fn main() !void { |
| 269 | </p> | 269 | </p> |
| 270 | <p> | 270 | <p> |
| 271 | A function is a block of any number of statements and expressions that, as a whole, perform a task. | 271 | A function is a block of any number of statements and expressions that, as a whole, perform a task. |
| 272 | Functions may or may not return data after they are done performing their task. | 272 | Functions may or may not return data after they are done performing their task. If a function |
| | 273 | cannot perform its task, it might return an error. Zig makes all of this explicit. |
| 273 | </p> | 274 | </p> |
| 274 | <p> | 275 | <p> |
| 275 | In the <code>hello.zig</code> code sample, the <code>main</code> function is declared | 276 | In the <code>hello.zig</code> code sample, the <code>main</code> function is declared |
| 276 | with the {#syntax#}!void{#endsyntax#} return type. This return type tells the Zig compiler | 277 | with the {#syntax#}!void{#endsyntax#} return type. This return type is known as an {#link|Error Union Type#}. |
| 277 | and other people reading the code that the function will not return a value and it <i>might</i> fail. | 278 | This syntax tells the Zig compiler that the function will either return an |
| 278 | The {#syntax#}!{#endsyntax#} (bang, exclamation mark) before the {#syntax#}void{#endsyntax#} | 279 | error or a value. An error union type combines an {#link|Error Set Type#} and a {#link|Primitive Type|Primitive Types#}. |
| 279 | {#link|type|Primitive Types#} is what tells the Zig compiler an {#link|error|Errors#} <i>might</i> | 280 | The full form of an error union type is |
| 280 | occur. The {#syntax#}void{#endsyntax#} return type tells the Zig compiler the <code>main</code> | 281 | <code>&lt;error set type&gt;</code>{#syntax#}!{#endsyntax#}<code>&lt;primitive type&gt;</code>. In the code |
| 281 | function will not return a value. | 282 | sample, the error set type is not explicitly written on the left side of the {#syntax#}!{#endsyntax#} operator. |
| | 283 | When written this way, the error set type is a special kind of error union type that has an |
| | 284 | {#link|inferred error set type|Inferred Error Sets#}. The {#syntax#}void{#endsyntax#} after the {#syntax#}!{#endsyntax#} operator |
| | 285 | tells the compiler that the function will not return a value under normal circumstances (i.e. no errors occur). |
| | 286 | </p> |
| | 287 | <p> |
| | 288 | Note to experienced programmers: Zig also has the boolean {#link|operator|Operators#} {#syntax#}!a{#endsyntax#} |
| | 289 | where {#syntax#}a{#endsyntax#} is a value of type {#syntax#}bool{#endsyntax#}. Error union types contain the |
| | 290 | name of the type in the syntax: {#syntax#}!{#endsyntax#}<code>&lt;primitive type&gt;</code>. |
| 282 | </p> | 291 | </p> |
| 283 | <p> | 292 | <p> |
| 284 | In Zig, a function's block of statements and expressions are surrounded by <code>{</code> and | 293 | In Zig, a function's block of statements and expressions are surrounded by <code>{</code> and |
| ... | @@ -286,9 +295,9 @@ pub fn main() !void { | ... | @@ -286,9 +295,9 @@ pub fn main() !void { |
| 286 | the task of outputting <code>Hello, world!</code> to standard output. | 295 | the task of outputting <code>Hello, world!</code> to standard output. |
| 287 | </p> | 296 | </p> |
| 288 | <p> | 297 | <p> |
| 289 | First, a constant identifier, <code>stdout</code>, is initialized to represent the standard output | 298 | First, a constant identifier, <code>stdout</code>, is initialized to represent standard output's |
| 290 | stream. Then, the program tries to print the <code>Hello, world!</code> message to the standard output | 299 | writer. Then, the program tries to print the <code>Hello, world!</code> |
| 291 | stream. | 300 | message to standard output. |
| 292 | </p> | 301 | </p> |
| 293 | <p> | 302 | <p> |
| 294 | Functions sometimes need information to perform their task. In Zig, information is passed | 303 | Functions sometimes need information to perform their task. In Zig, information is passed |
| ... | @@ -310,14 +319,14 @@ pub fn main() !void { | ... | @@ -310,14 +319,14 @@ pub fn main() !void { |
| 310 | more statements or expressions left to execute in the <code>main</code> function, so the program exits. | 319 | more statements or expressions left to execute in the <code>main</code> function, so the program exits. |
| 311 | </p> | 320 | </p> |
| 312 | <p> | 321 | <p> |
| 313 | In Zig, the standard output stream's <code>print</code> function is allowed to fail because | 322 | In Zig, the standard output writer's <code>print</code> function is allowed to fail because |
| 314 | it is actually a function defined for a generic output stream. Consider a generic output stream that | 323 | it is actually a function defined as part of a generic Writer. Consider a generic Writer that |
| 315 | represents writing data to a file. When the disk is full, a write to the file will fail. However, | 324 | represents writing data to a file. When the disk is full, a write to the file will fail. |
| 316 | we typically do not expect writing text to the standard output stream to fail. To avoid having | 325 | However, we typically do not expect writing text to the standard output to fail. To avoid having |
| 317 | to handle the failure case of printing to a standard output, you can use alternate functions: the | 326 | to handle the failure case of printing to standard output, you can use alternate functions: the |
| 318 | <code>std.log</code> function for proper logging or the <code>std.debug.print</code> function. | 327 | <code>std.log</code> function for proper logging or the <code>std.debug.print</code> function. |
| 319 | This documentation will use the latter option to print to standard error (stderr) and silently | 328 | This documentation will use the latter option to print to standard error (stderr) and silently return |
| 320 | return on failure. The next code sample, <code>hello_again.zig</code> demonstrates the use of | 329 | on failure. The next code sample, <code>hello_again.zig</code> demonstrates the use of |
| 321 | <code>std.debug.print</code>. | 330 | <code>std.debug.print</code>. |
| 322 | </p> | 331 | </p> |
| 323 | {#code_begin|exe|hello_again#} | 332 | {#code_begin|exe|hello_again#} |