| ... | ... | @@ -161,8 +161,8 @@ pub fn main() void { |
| 161 | 161 | } |
| 162 | 162 | {#code_end#} |
| 163 | 163 | <p> |
| 164 | | Note that we also left off the <code class="zig">!</code> from the return type. |
| 165 | | In Zig, if your main function cannot fail, you must use the <code class="zig">void</code> return type. |
| 164 | Note that we also left off the {#syntax#}!{#endsyntax#} from the return type. |
| 165 | In Zig, if your main function cannot fail, you must use the {#syntax#}void{#endsyntax#} return type. |
| 166 | 166 | </p> |
| 167 | 167 | {#see_also|Values|@import|Errors|Root Source File#} |
| 168 | 168 | {#header_close#} |
| ... | ... | @@ -181,14 +181,14 @@ test "comments" { |
| 181 | 181 | } |
| 182 | 182 | {#code_end#} |
| 183 | 183 | <p> |
| 184 | | There are no multiline comments in Zig (e.g. like <code>/* */</code> |
| 184 | There are no multiline comments in Zig (e.g. like <code class="c">/* */</code> |
| 185 | 185 | comments in C). This helps allow Zig to have the property that each line |
| 186 | 186 | of code can be tokenized out of context. |
| 187 | 187 | </p> |
| 188 | 188 | {#header_open|Doc comments#} |
| 189 | 189 | <p> |
| 190 | 190 | A doc comment is one that begins with exactly three slashes (i.e. |
| 191 | | <code class="zig">///</code> but not <code class="zig">////</code>); |
| 191 | {#syntax#}///{#endsyntax#} but not {#syntax#}////{#endsyntax#}); |
| 192 | 192 | multiple doc comments in a row are merged together to form a multiline |
| 193 | 193 | doc comment. The doc comment documents whatever immediately follows it. |
| 194 | 194 | </p> |
| ... | ... | @@ -280,169 +280,169 @@ pub fn main() void { |
| 280 | 280 | </th> |
| 281 | 281 | </tr> |
| 282 | 282 | <tr> |
| 283 | | <td><code>i8</code></td> |
| 284 | | <td><code>int8_t</code></td> |
| 283 | <td>{#syntax#}i8{#endsyntax#}</td> |
| 284 | <td><code class="c">int8_t</code></td> |
| 285 | 285 | <td>signed 8-bit integer</td> |
| 286 | 286 | </tr> |
| 287 | 287 | <tr> |
| 288 | | <td><code>u8</code></td> |
| 289 | | <td><code>uint8_t</code></td> |
| 288 | <td>{#syntax#}u8{#endsyntax#}</td> |
| 289 | <td><code class="c">uint8_t</code></td> |
| 290 | 290 | <td>unsigned 8-bit integer</td> |
| 291 | 291 | </tr> |
| 292 | 292 | <tr> |
| 293 | | <td><code>i16</code></td> |
| 294 | | <td><code>int16_t</code></td> |
| 293 | <td>{#syntax#}i16{#endsyntax#}</td> |
| 294 | <td><code class="c">int16_t</code></td> |
| 295 | 295 | <td>signed 16-bit integer</td> |
| 296 | 296 | </tr> |
| 297 | 297 | <tr> |
| 298 | | <td><code>u16</code></td> |
| 299 | | <td><code>uint16_t</code></td> |
| 298 | <td>{#syntax#}u16{#endsyntax#}</td> |
| 299 | <td><code class="c">uint16_t</code></td> |
| 300 | 300 | <td>unsigned 16-bit integer</td> |
| 301 | 301 | </tr> |
| 302 | 302 | <tr> |
| 303 | | <td><code>i32</code></td> |
| 304 | | <td><code>int32_t</code></td> |
| 303 | <td>{#syntax#}i32{#endsyntax#}</td> |
| 304 | <td><code class="c">int32_t</code></td> |
| 305 | 305 | <td>signed 32-bit integer</td> |
| 306 | 306 | </tr> |
| 307 | 307 | <tr> |
| 308 | | <td><code>u32</code></td> |
| 309 | | <td><code>uint32_t</code></td> |
| 308 | <td>{#syntax#}u32{#endsyntax#}</td> |
| 309 | <td><code class="c">uint32_t</code></td> |
| 310 | 310 | <td>unsigned 32-bit integer</td> |
| 311 | 311 | </tr> |
| 312 | 312 | <tr> |
| 313 | | <td><code>i64</code></td> |
| 314 | | <td><code>int64_t</code></td> |
| 313 | <td>{#syntax#}i64{#endsyntax#}</td> |
| 314 | <td><code class="c">int64_t</code></td> |
| 315 | 315 | <td>signed 64-bit integer</td> |
| 316 | 316 | </tr> |
| 317 | 317 | <tr> |
| 318 | | <td><code>u64</code></td> |
| 319 | | <td><code>uint64_t</code></td> |
| 318 | <td>{#syntax#}u64{#endsyntax#}</td> |
| 319 | <td><code class="c">uint64_t</code></td> |
| 320 | 320 | <td>unsigned 64-bit integer</td> |
| 321 | 321 | </tr> |
| 322 | 322 | <tr> |
| 323 | | <td><code>i128</code></td> |
| 324 | | <td><code>__int128</code></td> |
| 323 | <td>{#syntax#}i128{#endsyntax#}</td> |
| 324 | <td><code class="c">__int128</code></td> |
| 325 | 325 | <td>signed 128-bit integer</td> |
| 326 | 326 | </tr> |
| 327 | 327 | <tr> |
| 328 | | <td><code>u128</code></td> |
| 329 | | <td><code>unsigned __int128</code></td> |
| 328 | <td>{#syntax#}u128{#endsyntax#}</td> |
| 329 | <td><code class="c">unsigned __int128</code></td> |
| 330 | 330 | <td>unsigned 128-bit integer</td> |
| 331 | 331 | </tr> |
| 332 | 332 | <tr> |
| 333 | | <td><code>isize</code></td> |
| 334 | | <td><code>intptr_t</code></td> |
| 333 | <td>{#syntax#}isize{#endsyntax#}</td> |
| 334 | <td><code class="c">intptr_t</code></td> |
| 335 | 335 | <td>signed pointer sized integer</td> |
| 336 | 336 | </tr> |
| 337 | 337 | <tr> |
| 338 | | <td><code>usize</code></td> |
| 339 | | <td><code>uintptr_t</code></td> |
| 338 | <td>{#syntax#}usize{#endsyntax#}</td> |
| 339 | <td><code class="c">uintptr_t</code></td> |
| 340 | 340 | <td>unsigned pointer sized integer</td> |
| 341 | 341 | </tr> |
| 342 | 342 | |
| 343 | 343 | <tr> |
| 344 | | <td><code>c_short</code></td> |
| 345 | | <td><code>short</code></td> |
| 344 | <td>{#syntax#}c_short{#endsyntax#}</td> |
| 345 | <td><code class="c">short</code></td> |
| 346 | 346 | <td>for ABI compatibility with C</td> |
| 347 | 347 | </tr> |
| 348 | 348 | <tr> |
| 349 | | <td><code>c_ushort</code></td> |
| 350 | | <td><code>unsigned short</code></td> |
| 349 | <td>{#syntax#}c_ushort{#endsyntax#}</td> |
| 350 | <td><code class="c">unsigned short</code></td> |
| 351 | 351 | <td>for ABI compatibility with C</td> |
| 352 | 352 | </tr> |
| 353 | 353 | <tr> |
| 354 | | <td><code>c_int</code></td> |
| 355 | | <td><code>int</code></td> |
| 354 | <td>{#syntax#}c_int{#endsyntax#}</td> |
| 355 | <td><code class="c">int</code></td> |
| 356 | 356 | <td>for ABI compatibility with C</td> |
| 357 | 357 | </tr> |
| 358 | 358 | <tr> |
| 359 | | <td><code>c_uint</code></td> |
| 360 | | <td><code>unsigned int</code></td> |
| 359 | <td>{#syntax#}c_uint{#endsyntax#}</td> |
| 360 | <td><code class="c">unsigned int</code></td> |
| 361 | 361 | <td>for ABI compatibility with C</td> |
| 362 | 362 | </tr> |
| 363 | 363 | <tr> |
| 364 | | <td><code>c_long</code></td> |
| 365 | | <td><code>long</code></td> |
| 364 | <td>{#syntax#}c_long{#endsyntax#}</td> |
| 365 | <td><code class="c">long</code></td> |
| 366 | 366 | <td>for ABI compatibility with C</td> |
| 367 | 367 | </tr> |
| 368 | 368 | <tr> |
| 369 | | <td><code>c_ulong</code></td> |
| 370 | | <td><code>unsigned long</code></td> |
| 369 | <td>{#syntax#}c_ulong{#endsyntax#}</td> |
| 370 | <td><code class="c">unsigned long</code></td> |
| 371 | 371 | <td>for ABI compatibility with C</td> |
| 372 | 372 | </tr> |
| 373 | 373 | <tr> |
| 374 | | <td><code>c_longlong</code></td> |
| 375 | | <td><code>long long</code></td> |
| 374 | <td>{#syntax#}c_longlong{#endsyntax#}</td> |
| 375 | <td><code class="c">long long</code></td> |
| 376 | 376 | <td>for ABI compatibility with C</td> |
| 377 | 377 | </tr> |
| 378 | 378 | <tr> |
| 379 | | <td><code>c_ulonglong</code></td> |
| 380 | | <td><code>unsigned long long</code></td> |
| 379 | <td>{#syntax#}c_ulonglong{#endsyntax#}</td> |
| 380 | <td><code class="c">unsigned long long</code></td> |
| 381 | 381 | <td>for ABI compatibility with C</td> |
| 382 | 382 | </tr> |
| 383 | 383 | <tr> |
| 384 | | <td><code>c_longdouble</code></td> |
| 385 | | <td><code>long double</code></td> |
| 384 | <td>{#syntax#}c_longdouble{#endsyntax#}</td> |
| 385 | <td><code class="c">long double</code></td> |
| 386 | 386 | <td>for ABI compatibility with C</td> |
| 387 | 387 | </tr> |
| 388 | 388 | <tr> |
| 389 | | <td><code>c_void</code></td> |
| 390 | | <td><code>void</code></td> |
| 389 | <td>{#syntax#}c_void{#endsyntax#}</td> |
| 390 | <td><code class="c">void</code></td> |
| 391 | 391 | <td>for ABI compatibility with C</td> |
| 392 | 392 | </tr> |
| 393 | 393 | |
| 394 | 394 | <tr> |
| 395 | | <td><code>f16</code></td> |
| 396 | | <td><code>float</code></td> |
| 395 | <td>{#syntax#}f16{#endsyntax#}</td> |
| 396 | <td><code class="c">float</code></td> |
| 397 | 397 | <td>16-bit floating point (10-bit mantissa) IEEE-754-2008 binary16</td> |
| 398 | 398 | </tr> |
| 399 | 399 | <tr> |
| 400 | | <td><code>f32</code></td> |
| 401 | | <td><code>float</code></td> |
| 400 | <td>{#syntax#}f32{#endsyntax#}</td> |
| 401 | <td><code class="c">float</code></td> |
| 402 | 402 | <td>32-bit floating point (23-bit mantissa) IEEE-754-2008 binary32</td> |
| 403 | 403 | </tr> |
| 404 | 404 | <tr> |
| 405 | | <td><code>f64</code></td> |
| 406 | | <td><code>double</code></td> |
| 405 | <td>{#syntax#}f64{#endsyntax#}</td> |
| 406 | <td><code class="c">double</code></td> |
| 407 | 407 | <td>64-bit floating point (52-bit mantissa) IEEE-754-2008 binary64</td> |
| 408 | 408 | </tr> |
| 409 | 409 | <tr> |
| 410 | | <td><code>f128</code></td> |
| 410 | <td>{#syntax#}f128{#endsyntax#}</td> |
| 411 | 411 | <td>(none)</td> |
| 412 | 412 | <td>128-bit floating point (112-bit mantissa) IEEE-754-2008 binary128</td> |
| 413 | 413 | </tr> |
| 414 | 414 | <tr> |
| 415 | | <td><code>bool</code></td> |
| 416 | | <td><code>bool</code></td> |
| 417 | | <td><code>true</code> or <code>false</code></td> |
| 415 | <td>{#syntax#}bool{#endsyntax#}</td> |
| 416 | <td><code class="c">bool</code></td> |
| 417 | <td>{#syntax#}true{#endsyntax#} or {#syntax#}false{#endsyntax#}</td> |
| 418 | 418 | </tr> |
| 419 | 419 | <tr> |
| 420 | | <td><code>void</code></td> |
| 420 | <td>{#syntax#}void{#endsyntax#}</td> |
| 421 | 421 | <td>(none)</td> |
| 422 | 422 | <td>0 bit type</td> |
| 423 | 423 | </tr> |
| 424 | 424 | <tr> |
| 425 | | <td><code>noreturn</code></td> |
| 425 | <td>{#syntax#}noreturn{#endsyntax#}</td> |
| 426 | 426 | <td>(none)</td> |
| 427 | | <td>the type of <code>break</code>, <code>continue</code>, <code>return</code>, <code>unreachable</code>, and <code>while (true) {}</code></td> |
| 427 | <td>the type of {#syntax#}break{#endsyntax#}, {#syntax#}continue{#endsyntax#}, {#syntax#}return{#endsyntax#}, {#syntax#}unreachable{#endsyntax#}, and {#syntax#}while (true) {}{#endsyntax#}</td> |
| 428 | 428 | </tr> |
| 429 | 429 | <tr> |
| 430 | | <td><code>type</code></td> |
| 430 | <td>{#syntax#}type{#endsyntax#}</td> |
| 431 | 431 | <td>(none)</td> |
| 432 | 432 | <td>the type of types</td> |
| 433 | 433 | </tr> |
| 434 | 434 | <tr> |
| 435 | | <td><code>error</code></td> |
| 435 | <td>{#syntax#}error{#endsyntax#}</td> |
| 436 | 436 | <td>(none)</td> |
| 437 | 437 | <td>an error code</td> |
| 438 | 438 | </tr> |
| 439 | 439 | <tr> |
| 440 | | <td><code>comptime_int</code></td> |
| 440 | <td>{#syntax#}comptime_int{#endsyntax#}</td> |
| 441 | 441 | <td>(none)</td> |
| 442 | 442 | <td>Only allowed for {#link|comptime#}-known values. The type of integer literals.</td> |
| 443 | 443 | </tr> |
| 444 | 444 | <tr> |
| 445 | | <td><code>comptime_float</code></td> |
| 445 | <td>{#syntax#}comptime_float{#endsyntax#}</td> |
| 446 | 446 | <td>(none)</td> |
| 447 | 447 | <td>Only allowed for {#link|comptime#}-known values. The type of float literals.</td> |
| 448 | 448 | </tr> |
| ... | ... | @@ -451,7 +451,7 @@ pub fn main() void { |
| 451 | 451 | <p> |
| 452 | 452 | In addition to the integer types above, arbitrary bit-width integers can be referenced by using |
| 453 | 453 | an identifier of <code>i</code> or </code>u</code> followed by digits. For example, the identifier |
| 454 | | <code>i7</code> refers to a signed 7-bit integer. |
| 454 | {#syntax#}i7{#endsyntax#} refers to a signed 7-bit integer. |
| 455 | 455 | </p> |
| 456 | 456 | {#see_also|Integers|Floats|void|Errors#} |
| 457 | 457 | {#header_close#} |
| ... | ... | @@ -467,15 +467,15 @@ pub fn main() void { |
| 467 | 467 | </th> |
| 468 | 468 | </tr> |
| 469 | 469 | <tr> |
| 470 | | <td><code>true</code> and <code>false</code></td> |
| 471 | | <td><code>bool</code> values</td> |
| 470 | <td>{#syntax#}true{#endsyntax#} and {#syntax#}false{#endsyntax#}</td> |
| 471 | <td>{#syntax#}bool{#endsyntax#} values</td> |
| 472 | 472 | </tr> |
| 473 | 473 | <tr> |
| 474 | | <td><code>null</code></td> |
| 475 | | <td>used to set an optional type to <code>null</code></td> |
| 474 | <td>{#syntax#}null{#endsyntax#}</td> |
| 475 | <td>used to set an optional type to {#syntax#}null{#endsyntax#}</td> |
| 476 | 476 | </tr> |
| 477 | 477 | <tr> |
| 478 | | <td><code>undefined</code></td> |
| 478 | <td>{#syntax#}undefined{#endsyntax#}</td> |
| 479 | 479 | <td>used to leave a value unspecified</td> |
| 480 | 480 | </tr> |
| 481 | 481 | </table> |
| ... | ... | @@ -515,52 +515,52 @@ test "string literals" { |
| 515 | 515 | </th> |
| 516 | 516 | </tr> |
| 517 | 517 | <tr> |
| 518 | | <td><code>\n</code></td> |
| 518 | <td><code>\n</code></td> |
| 519 | 519 | <td>Newline</td> |
| 520 | 520 | </tr> |
| 521 | 521 | <tr> |
| 522 | | <td><code>\r</code></td> |
| 522 | <td><code>\r</code></td> |
| 523 | 523 | <td>Carriage Return</td> |
| 524 | 524 | </tr> |
| 525 | 525 | <tr> |
| 526 | | <td><code>\t</code></td> |
| 526 | <td><code>\t</code></td> |
| 527 | 527 | <td>Tab</td> |
| 528 | 528 | </tr> |
| 529 | 529 | <tr> |
| 530 | | <td><code>\\</code></td> |
| 530 | <td><code>\\</code></td> |
| 531 | 531 | <td>Backslash</td> |
| 532 | 532 | </tr> |
| 533 | 533 | <tr> |
| 534 | | <td><code>\'</code></td> |
| 534 | <td><code>\'</code></td> |
| 535 | 535 | <td>Single Quote</td> |
| 536 | 536 | </tr> |
| 537 | 537 | <tr> |
| 538 | | <td><code>\"</code></td> |
| 538 | <td><code>\"</code></td> |
| 539 | 539 | <td>Double Quote</td> |
| 540 | 540 | </tr> |
| 541 | 541 | <tr> |
| 542 | | <td><code>\xNN</code></td> |
| 542 | <td><code>\xNN</code></td> |
| 543 | 543 | <td>hexadecimal 8-bit character code (2 digits)</td> |
| 544 | 544 | </tr> |
| 545 | 545 | <tr> |
| 546 | | <td><code>\uNNNN</code></td> |
| 546 | <td><code>\uNNNN</code></td> |
| 547 | 547 | <td>hexadecimal 16-bit Unicode character code UTF-8 encoded (4 digits)</td> |
| 548 | 548 | </tr> |
| 549 | 549 | <tr> |
| 550 | | <td><code>\UNNNNNN</code></td> |
| 550 | <td><code>\UNNNNNN</code></td> |
| 551 | 551 | <td>hexadecimal 24-bit Unicode character code UTF-8 encoded (6 digits)</td> |
| 552 | 552 | </tr> |
| 553 | 553 | </table> |
| 554 | 554 | </div> |
| 555 | | <p>Note that the maximum valid Unicode point is <code>0x10ffff</code>.</p> |
| 555 | <p>Note that the maximum valid Unicode point is {#syntax#}0x10ffff{#endsyntax#}.</p> |
| 556 | 556 | {#header_close#} |
| 557 | 557 | {#header_open|Multiline String Literals#} |
| 558 | 558 | <p> |
| 559 | 559 | Multiline string literals have no escapes and can span across multiple lines. |
| 560 | | To start a multiline string literal, use the <code>\\</code> token. Just like a comment, |
| 560 | To start a multiline string literal, use the {#syntax#}\\{#endsyntax#} token. Just like a comment, |
| 561 | 561 | the string literal goes until the end of the line. The end of the line is |
| 562 | 562 | not included in the string literal. |
| 563 | | However, if the next line begins with <code>\\</code> then a newline is appended and |
| 563 | However, if the next line begins with {#syntax#}\\{#endsyntax#} then a newline is appended and |
| 564 | 564 | the string literal continues. |
| 565 | 565 | </p> |
| 566 | 566 | {#code_begin|syntax#} |
| ... | ... | @@ -574,7 +574,7 @@ const hello_world_in_c = |
| 574 | 574 | ; |
| 575 | 575 | {#code_end#} |
| 576 | 576 | <p> |
| 577 | | For a multiline C string literal, prepend <code>c</code> to each <code>\\</code>: |
| 577 | For a multiline C string literal, prepend <code>c</code> to each {#syntax#}\\{#endsyntax#}: |
| 578 | 578 | </p> |
| 579 | 579 | {#code_begin|syntax#} |
| 580 | 580 | const c_string_literal = |
| ... | ... | @@ -587,14 +587,14 @@ const c_string_literal = |
| 587 | 587 | ; |
| 588 | 588 | {#code_end#} |
| 589 | 589 | <p> |
| 590 | | In this example the variable <code>c_string_literal</code> has type <code>[*]const char</code> and |
| 590 | In this example the variable {#syntax#}c_string_literal{#endsyntax#} has type {#syntax#}[*]const char{#endsyntax#} and |
| 591 | 591 | has a terminating null byte. |
| 592 | 592 | </p> |
| 593 | 593 | {#see_also|@embedFile#} |
| 594 | 594 | {#header_close#} |
| 595 | 595 | {#header_close#} |
| 596 | 596 | {#header_open|Assignment#} |
| 597 | | <p>Use the <code>const</code> keyword to assign a value to an identifier:</p> |
| 597 | <p>Use the {#syntax#}const{#endsyntax#} keyword to assign a value to an identifier:</p> |
| 598 | 598 | {#code_begin|test_err|cannot assign to constant#} |
| 599 | 599 | const x = 1234; |
| 600 | 600 | |
| ... | ... | @@ -610,8 +610,8 @@ test "assignment" { |
| 610 | 610 | foo(); |
| 611 | 611 | } |
| 612 | 612 | {#code_end#} |
| 613 | | <p><code>const</code> applies to all of the bytes that the identifier immediately addresses. {#link|Pointers#} have their own const-ness.</p> |
| 614 | | <p>If you need a variable that you can modify, use the <code>var</code> keyword:</p> |
| 613 | <p>{#syntax#}const{#endsyntax#} applies to all of the bytes that the identifier immediately addresses. {#link|Pointers#} have their own const-ness.</p> |
| 614 | <p>If you need a variable that you can modify, use the {#syntax#}var{#endsyntax#} keyword:</p> |
| 615 | 615 | {#code_begin|test#} |
| 616 | 616 | const assert = @import("std").debug.assert; |
| 617 | 617 | |
| ... | ... | @@ -632,7 +632,7 @@ test "initialization" { |
| 632 | 632 | } |
| 633 | 633 | {#code_end#} |
| 634 | 634 | {#header_open|undefined#} |
| 635 | | <p>Use <code>undefined</code> to leave variables uninitialized:</p> |
| 635 | <p>Use {#syntax#}undefined{#endsyntax#} to leave variables uninitialized:</p> |
| 636 | 636 | {#code_begin|test#} |
| 637 | 637 | const assert = @import("std").debug.assert; |
| 638 | 638 | |
| ... | ... | @@ -643,14 +643,14 @@ test "init with undefined" { |
| 643 | 643 | } |
| 644 | 644 | {#code_end#} |
| 645 | 645 | <p> |
| 646 | | <code>undefined</code> can be {#link|implicitly cast|Implicit Casts#} to any type. |
| 647 | | Once this happens, it is no longer possible to detect that the value is <code>undefined</code>. |
| 648 | | <code>undefined</code> means the value could be anything, even something that is nonsense |
| 649 | | according to the type. Translated into English, <code>undefined</code> means "Not a meaningful |
| 646 | {#syntax#}undefined{#endsyntax#} can be {#link|implicitly cast|Implicit Casts#} to any type. |
| 647 | Once this happens, it is no longer possible to detect that the value is {#syntax#}undefined{#endsyntax#}. |
| 648 | {#syntax#}undefined{#endsyntax#} means the value could be anything, even something that is nonsense |
| 649 | according to the type. Translated into English, {#syntax#}undefined{#endsyntax#} means "Not a meaningful |
| 650 | 650 | value. Using this value would be a bug. The value will be unused, or overwritten before being used." |
| 651 | 651 | </p> |
| 652 | 652 | <p> |
| 653 | | In {#link|Debug#} mode, Zig writes <code>0xaa</code> bytes to undefined memory. This is to catch |
| 653 | In {#link|Debug#} mode, Zig writes {#syntax#}0xaa{#endsyntax#} bytes to undefined memory. This is to catch |
| 654 | 654 | bugs early, and to help detect use of undefined memory in a debugger. |
| 655 | 655 | </p> |
| 656 | 656 | {#header_close#} |
| ... | ... | @@ -681,14 +681,14 @@ fn divide(a: i32, b: i32) i32 { |
| 681 | 681 | } |
| 682 | 682 | {#code_end#} |
| 683 | 683 | <p> |
| 684 | | In this function, values <code>a</code> and <code>b</code> are known only at runtime, |
| 684 | In this function, values {#syntax#}a{#endsyntax#} and {#syntax#}b{#endsyntax#} are known only at runtime, |
| 685 | 685 | and thus this division operation is vulnerable to both integer overflow and |
| 686 | 686 | division by zero. |
| 687 | 687 | </p> |
| 688 | 688 | <p> |
| 689 | | Operators such as <code>+</code> and <code>-</code> cause undefined behavior on |
| 690 | | integer overflow. Also available are operations such as <code>+%</code> and |
| 691 | | <code>-%</code> which are defined to have wrapping arithmetic on all targets. |
| 689 | Operators such as {#syntax#}+{#endsyntax#} and {#syntax#}-{#endsyntax#} cause undefined behavior on |
| 690 | integer overflow. Also available are operations such as {#syntax#}+%{#endsyntax#} and |
| 691 | {#syntax#}-%{#endsyntax#} which are defined to have wrapping arithmetic on all targets. |
| 692 | 692 | </p> |
| 693 | 693 | {#see_also|Integer Overflow|Division by Zero|Wrapping Operations#} |
| 694 | 694 | {#header_close#} |
| ... | ... | @@ -696,15 +696,15 @@ fn divide(a: i32, b: i32) i32 { |
| 696 | 696 | {#header_open|Floats#} |
| 697 | 697 | <p>Zig has the following floating point types:</p> |
| 698 | 698 | <ul> |
| 699 | | <li><code>f16</code> - IEEE-754-2008 binary16</li> |
| 700 | | <li><code>f32</code> - IEEE-754-2008 binary32</li> |
| 701 | | <li><code>f64</code> - IEEE-754-2008 binary64</li> |
| 702 | | <li><code>f128</code> - IEEE-754-2008 binary128</li> |
| 703 | | <li><code>c_longdouble</code> - matches <code>long double</code> for the target C ABI</li> |
| 699 | <li>{#syntax#}f16{#endsyntax#} - IEEE-754-2008 binary16</li> |
| 700 | <li>{#syntax#}f32{#endsyntax#} - IEEE-754-2008 binary32</li> |
| 701 | <li>{#syntax#}f64{#endsyntax#} - IEEE-754-2008 binary64</li> |
| 702 | <li>{#syntax#}f128{#endsyntax#} - IEEE-754-2008 binary128</li> |
| 703 | <li>{#syntax#}c_longdouble{#endsyntax#} - matches <code class="c">long double</code> for the target C ABI</li> |
| 704 | 704 | </ul> |
| 705 | 705 | {#header_open|Float Literals#} |
| 706 | 706 | <p> |
| 707 | | Float literals have type <code>comptime_float</code> which is guaranteed to hold at least all possible values |
| 707 | Float literals have type {#syntax#}comptime_float{#endsyntax#} which is guaranteed to hold at least all possible values |
| 708 | 708 | that the largest other floating point type can hold. Float literals {#link|implicitly cast|Implicit Casts#} to any other type. |
| 709 | 709 | </p> |
| 710 | 710 | {#code_begin|syntax#} |
| ... | ... | @@ -718,8 +718,8 @@ const yet_another_hex_float = 0x103.70P-5; |
| 718 | 718 | {#code_end#} |
| 719 | 719 | {#header_close#} |
| 720 | 720 | {#header_open|Floating Point Operations#} |
| 721 | | <p>By default floating point operations use <code>Strict</code> mode, |
| 722 | | but you can switch to <code>Optimized</code> mode on a per-block basis:</p> |
| 721 | <p>By default floating point operations use {#syntax#}Strict{#endsyntax#} mode, |
| 722 | but you can switch to {#syntax#}Optimized{#endsyntax#} mode on a per-block basis:</p> |
| 723 | 723 | {#code_begin|obj|foo#} |
| 724 | 724 | {#code_release_fast#} |
| 725 | 725 | const builtin = @import("builtin"); |
| ... | ... | @@ -772,8 +772,8 @@ pub fn main() void { |
| 772 | 772 | </th> |
| 773 | 773 | </tr> |
| 774 | 774 | <tr> |
| 775 | | <td><pre><code class="zig">a + b |
| 776 | | a += b</code></pre></td> |
| 775 | <td><pre>{#syntax#}a + b |
| 776 | a += b{#endsyntax#}</pre></td> |
| 777 | 777 | <td> |
| 778 | 778 | <ul> |
| 779 | 779 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -788,12 +788,12 @@ a += b</code></pre></td> |
| 788 | 788 | </ul> |
| 789 | 789 | </td> |
| 790 | 790 | <td> |
| 791 | | <pre><code class="zig">2 + 5 == 7</code></pre> |
| 791 | <pre>{#syntax#}2 + 5 == 7{#endsyntax#}</pre> |
| 792 | 792 | </td> |
| 793 | 793 | </tr> |
| 794 | 794 | <tr> |
| 795 | | <td><pre><code class="zig">a +% b |
| 796 | | a +%= b</code></pre></td> |
| 795 | <td><pre>{#syntax#}a +% b |
| 796 | a +%= b{#endsyntax#}</pre></td> |
| 797 | 797 | <td> |
| 798 | 798 | <ul> |
| 799 | 799 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -807,12 +807,12 @@ a +%= b</code></pre></td> |
| 807 | 807 | </ul> |
| 808 | 808 | </td> |
| 809 | 809 | <td> |
| 810 | | <pre><code class="zig">u32(@maxValue(u32)) +% 1 == 0</code></pre> |
| 810 | <pre>{#syntax#}u32(@maxValue(u32)) +% 1 == 0{#endsyntax#}</pre> |
| 811 | 811 | </td> |
| 812 | 812 | </tr> |
| 813 | 813 | <tr> |
| 814 | | <td><pre><code class="zig">a - b |
| 815 | | a -= b</code></pre></td> |
| 814 | <td><pre>{#syntax#}a - b |
| 815 | a -= b{#endsyntax#}</pre></td> |
| 816 | 816 | <td> |
| 817 | 817 | <ul> |
| 818 | 818 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -827,12 +827,12 @@ a -= b</code></pre></td> |
| 827 | 827 | </ul> |
| 828 | 828 | </td> |
| 829 | 829 | <td> |
| 830 | | <pre><code class="zig">2 - 5 == -3</code></pre> |
| 830 | <pre>{#syntax#}2 - 5 == -3{#endsyntax#}</pre> |
| 831 | 831 | </td> |
| 832 | 832 | </tr> |
| 833 | 833 | <tr> |
| 834 | | <td><pre><code class="zig">a -% b |
| 835 | | a -%= b</code></pre></td> |
| 834 | <td><pre>{#syntax#}a -% b |
| 835 | a -%= b{#endsyntax#}</pre></td> |
| 836 | 836 | <td> |
| 837 | 837 | <ul> |
| 838 | 838 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -846,11 +846,11 @@ a -%= b</code></pre></td> |
| 846 | 846 | </ul> |
| 847 | 847 | </td> |
| 848 | 848 | <td> |
| 849 | | <pre><code class="zig">u32(0) -% 1 == @maxValue(u32)</code></pre> |
| 849 | <pre>{#syntax#}u32(0) -% 1 == @maxValue(u32){#endsyntax#}</pre> |
| 850 | 850 | </td> |
| 851 | 851 | </tr> |
| 852 | 852 | <tr> |
| 853 | | <td><pre><code class="zig">-a<code></pre></td> |
| 853 | <td><pre>{#syntax#}-a{#endsyntax#}</pre></td> |
| 854 | 854 | <td> |
| 855 | 855 | <ul> |
| 856 | 856 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -864,11 +864,11 @@ a -%= b</code></pre></td> |
| 864 | 864 | </ul> |
| 865 | 865 | </td> |
| 866 | 866 | <td> |
| 867 | | <pre><code class="zig">-1 == 0 - 1</code></pre> |
| 867 | <pre>{#syntax#}-1 == 0 - 1{#endsyntax#}</pre> |
| 868 | 868 | </td> |
| 869 | 869 | </tr> |
| 870 | 870 | <tr> |
| 871 | | <td><pre><code class="zig">-%a<code></pre></td> |
| 871 | <td><pre>{#syntax#}-%a{#endsyntax#}</pre></td> |
| 872 | 872 | <td> |
| 873 | 873 | <ul> |
| 874 | 874 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -881,12 +881,12 @@ a -%= b</code></pre></td> |
| 881 | 881 | </ul> |
| 882 | 882 | </td> |
| 883 | 883 | <td> |
| 884 | | <pre><code class="zig">-%i32(@minValue(i32)) == @minValue(i32)</code></pre> |
| 884 | <pre>{#syntax#}-%i32(@minValue(i32)) == @minValue(i32){#endsyntax#}</pre> |
| 885 | 885 | </td> |
| 886 | 886 | </tr> |
| 887 | 887 | <tr> |
| 888 | | <td><pre><code class="zig">a * b |
| 889 | | a *= b</code></pre></td> |
| 888 | <td><pre>{#syntax#}a * b |
| 889 | a *= b{#endsyntax#}</pre></td> |
| 890 | 890 | <td> |
| 891 | 891 | <ul> |
| 892 | 892 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -901,12 +901,12 @@ a *= b</code></pre></td> |
| 901 | 901 | </ul> |
| 902 | 902 | </td> |
| 903 | 903 | <td> |
| 904 | | <pre><code class="zig">2 * 5 == 10</code></pre> |
| 904 | <pre>{#syntax#}2 * 5 == 10{#endsyntax#}</pre> |
| 905 | 905 | </td> |
| 906 | 906 | </tr> |
| 907 | 907 | <tr> |
| 908 | | <td><pre><code class="zig">a *% b |
| 909 | | a *%= b</code></pre></td> |
| 908 | <td><pre>{#syntax#}a *% b |
| 909 | a *%= b{#endsyntax#}</pre></td> |
| 910 | 910 | <td> |
| 911 | 911 | <ul> |
| 912 | 912 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -920,12 +920,12 @@ a *%= b</code></pre></td> |
| 920 | 920 | </ul> |
| 921 | 921 | </td> |
| 922 | 922 | <td> |
| 923 | | <pre><code class="zig">u8(200) *% 2 == 144</code></pre> |
| 923 | <pre>{#syntax#}u8(200) *% 2 == 144{#endsyntax#}</pre> |
| 924 | 924 | </td> |
| 925 | 925 | </tr> |
| 926 | 926 | <tr> |
| 927 | | <td><pre><code class="zig">a / b |
| 928 | | a /= b</code></pre></td> |
| 927 | <td><pre>{#syntax#}a / b |
| 928 | a /= b{#endsyntax#}</pre></td> |
| 929 | 929 | <td> |
| 930 | 930 | <ul> |
| 931 | 931 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -940,18 +940,18 @@ a /= b</code></pre></td> |
| 940 | 940 | <li>For non-compile-time-known signed integers, must use |
| 941 | 941 | {#link|@divTrunc#}, |
| 942 | 942 | {#link|@divFloor#}, or |
| 943 | | {#link|@divExact#} instead of <code>/</code>. |
| 943 | {#link|@divExact#} instead of {#syntax#}/{#endsyntax#}. |
| 944 | 944 | </li> |
| 945 | 945 | <li>Invokes {#link|Peer Type Resolution#} for the operands.</li> |
| 946 | 946 | </ul> |
| 947 | 947 | </td> |
| 948 | 948 | <td> |
| 949 | | <pre><code class="zig">10 / 5 == 2</code></pre> |
| 949 | <pre>{#syntax#}10 / 5 == 2{#endsyntax#}</pre> |
| 950 | 950 | </td> |
| 951 | 951 | </tr> |
| 952 | 952 | <tr> |
| 953 | | <td><pre><code class="zig">a % b |
| 954 | | a %= b</code></pre></td> |
| 953 | <td><pre>{#syntax#}a % b |
| 954 | a %= b{#endsyntax#}</pre></td> |
| 955 | 955 | <td> |
| 956 | 956 | <ul> |
| 957 | 957 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -964,18 +964,18 @@ a %= b</code></pre></td> |
| 964 | 964 | <li>Can cause {#link|Division by Zero#} for floats in {#link|FloatMode.Optimized Mode|Floating Point Operations#}.</li> |
| 965 | 965 | <li>For non-compile-time-known signed integers, must use |
| 966 | 966 | {#link|@rem#} or |
| 967 | | {#link|@mod#} instead of <code>%</code>. |
| 967 | {#link|@mod#} instead of {#syntax#}%{#endsyntax#}. |
| 968 | 968 | </li> |
| 969 | 969 | <li>Invokes {#link|Peer Type Resolution#} for the operands.</li> |
| 970 | 970 | </ul> |
| 971 | 971 | </td> |
| 972 | 972 | <td> |
| 973 | | <pre><code class="zig">10 % 3 == 1</code></pre> |
| 973 | <pre>{#syntax#}10 % 3 == 1{#endsyntax#}</pre> |
| 974 | 974 | </td> |
| 975 | 975 | </tr> |
| 976 | 976 | <tr> |
| 977 | | <td><pre><code class="zig">a &lt;&lt; b |
| 978 | | a &lt;&lt;= b</code></pre></td> |
| 977 | <td><pre>{#syntax#}a << b |
| 978 | a <<= b{#endsyntax#}</pre></td> |
| 979 | 979 | <td> |
| 980 | 980 | <ul> |
| 981 | 981 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -983,18 +983,18 @@ a &lt;&lt;= b</code></pre></td> |
| 983 | 983 | </td> |
| 984 | 984 | <td>Bit Shift Left. |
| 985 | 985 | <ul> |
| 986 | | <li><code>b</code> must be {#link|comptime-known|comptime#} or have a type with log2 number of bits as <code>a</code>.</li> |
| 986 | <li>{#syntax#}b{#endsyntax#} must be {#link|comptime-known|comptime#} or have a type with log2 number of bits as {#syntax#}a{#endsyntax#}.</li> |
| 987 | 987 | <li>See also {#link|@shlExact#}.</li> |
| 988 | 988 | <li>See also {#link|@shlWithOverflow#}.</li> |
| 989 | 989 | </ul> |
| 990 | 990 | </td> |
| 991 | 991 | <td> |
| 992 | | <pre><code class="zig">1 &lt;&lt; 8 == 256</code></pre> |
| 992 | <pre>{#syntax#}1 << 8 == 256{#endsyntax#}</pre> |
| 993 | 993 | </td> |
| 994 | 994 | </tr> |
| 995 | 995 | <tr> |
| 996 | | <td><pre><code class="zig">a &gt;&gt; b |
| 997 | | a &gt;&gt;= b</code></pre></td> |
| 996 | <td><pre>{#syntax#}a >> b |
| 997 | a >>= b{#endsyntax#}</pre></td> |
| 998 | 998 | <td> |
| 999 | 999 | <ul> |
| 1000 | 1000 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1002,17 +1002,17 @@ a &gt;&gt;= b</code></pre></td> |
| 1002 | 1002 | </td> |
| 1003 | 1003 | <td>Bit Shift Right. |
| 1004 | 1004 | <ul> |
| 1005 | | <li><code>b</code> must be {#link|comptime-known|comptime#} or have a type with log2 number of bits as <code>a</code>.</li> |
| 1005 | <li>{#syntax#}b{#endsyntax#} must be {#link|comptime-known|comptime#} or have a type with log2 number of bits as {#syntax#}a{#endsyntax#}.</li> |
| 1006 | 1006 | <li>See also {#link|@shrExact#}.</li> |
| 1007 | 1007 | </ul> |
| 1008 | 1008 | </td> |
| 1009 | 1009 | <td> |
| 1010 | | <pre><code class="zig">10 &gt;&gt; 1 == 5</code></pre> |
| 1010 | <pre>{#syntax#}10 >> 1 == 5{#endsyntax#}</pre> |
| 1011 | 1011 | </td> |
| 1012 | 1012 | </tr> |
| 1013 | 1013 | <tr> |
| 1014 | | <td><pre><code class="zig">a &amp; b |
| 1015 | | a &amp;= b</code></pre></td> |
| 1014 | <td><pre>{#syntax#}a & b |
| 1015 | a &= b{#endsyntax#}</pre></td> |
| 1016 | 1016 | <td> |
| 1017 | 1017 | <ul> |
| 1018 | 1018 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1024,12 +1024,12 @@ a &amp;= b</code></pre></td> |
| 1024 | 1024 | </ul> |
| 1025 | 1025 | </td> |
| 1026 | 1026 | <td> |
| 1027 | | <pre><code class="zig">0b011 &amp; 0b101 == 0b001</code></pre> |
| 1027 | <pre>{#syntax#}0b011 &amp; 0b101 == 0b001{#endsyntax#}</pre> |
| 1028 | 1028 | </td> |
| 1029 | 1029 | </tr> |
| 1030 | 1030 | <tr> |
| 1031 | | <td><pre><code class="zig">a | b |
| 1032 | | a |= b</code></pre></td> |
| 1031 | <td><pre>{#syntax#}a | b |
| 1032 | a |= b{#endsyntax#}</pre></td> |
| 1033 | 1033 | <td> |
| 1034 | 1034 | <ul> |
| 1035 | 1035 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1041,12 +1041,12 @@ a |= b</code></pre></td> |
| 1041 | 1041 | </ul> |
| 1042 | 1042 | </td> |
| 1043 | 1043 | <td> |
| 1044 | | <pre><code class="zig">0b010 | 0b100 == 0b110</code></pre> |
| 1044 | <pre>{#syntax#}0b010 | 0b100 == 0b110{#endsyntax#}</pre> |
| 1045 | 1045 | </td> |
| 1046 | 1046 | </tr> |
| 1047 | 1047 | <tr> |
| 1048 | | <td><pre><code class="zig">a ^ b |
| 1049 | | a ^= b</code></pre></td> |
| 1048 | <td><pre>{#syntax#}a ^ b |
| 1049 | a ^= b{#endsyntax#}</pre></td> |
| 1050 | 1050 | <td> |
| 1051 | 1051 | <ul> |
| 1052 | 1052 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1058,11 +1058,11 @@ a ^= b</code></pre></td> |
| 1058 | 1058 | </ul> |
| 1059 | 1059 | </td> |
| 1060 | 1060 | <td> |
| 1061 | | <pre><code class="zig">0b011 ^ 0b101 == 0b110</code></pre> |
| 1061 | <pre>{#syntax#}0b011 ^ 0b101 == 0b110{#endsyntax#}</pre> |
| 1062 | 1062 | </td> |
| 1063 | 1063 | </tr> |
| 1064 | 1064 | <tr> |
| 1065 | | <td><pre><code class="zig">~a<code></pre></td> |
| 1065 | <td><pre>{#syntax#}~a{#endsyntax#}</pre></td> |
| 1066 | 1066 | <td> |
| 1067 | 1067 | <ul> |
| 1068 | 1068 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1072,29 +1072,29 @@ a ^= b</code></pre></td> |
| 1072 | 1072 | Bitwise NOT. |
| 1073 | 1073 | </td> |
| 1074 | 1074 | <td> |
| 1075 | | <pre><code class="zig">~u8(0b0101111) == 0b1010000</code></pre> |
| 1075 | <pre>{#syntax#}~u8(0b0101111) == 0b1010000{#endsyntax#}</pre> |
| 1076 | 1076 | </td> |
| 1077 | 1077 | </tr> |
| 1078 | 1078 | <tr> |
| 1079 | | <td><pre><code class="zig">a orelse b</code></pre></td> |
| 1079 | <td><pre>{#syntax#}a orelse b{#endsyntax#}</pre></td> |
| 1080 | 1080 | <td> |
| 1081 | 1081 | <ul> |
| 1082 | 1082 | <li>{#link|Optionals#}</li> |
| 1083 | 1083 | </ul> |
| 1084 | 1084 | </td> |
| 1085 | | <td>If <code>a</code> is <code>null</code>, |
| 1086 | | returns <code>b</code> ("default value"), |
| 1087 | | otherwise returns the unwrapped value of <code>a</code>. |
| 1088 | | Note that <code>b</code> may be a value of type {#link|noreturn#}. |
| 1085 | <td>If {#syntax#}a{#endsyntax#} is {#syntax#}null{#endsyntax#}, |
| 1086 | returns {#syntax#}b{#endsyntax#} ("default value"), |
| 1087 | otherwise returns the unwrapped value of {#syntax#}a{#endsyntax#}. |
| 1088 | Note that {#syntax#}b{#endsyntax#} may be a value of type {#link|noreturn#}. |
| 1089 | 1089 | </td> |
| 1090 | 1090 | <td> |
| 1091 | | <pre><code class="zig">const value: ?u32 = null; |
| 1091 | <pre>{#syntax#}const value: ?u32 = null; |
| 1092 | 1092 | const unwrapped = value orelse 1234; |
| 1093 | | unwrapped == 1234</code></pre> |
| 1093 | unwrapped == 1234{#endsyntax#}</pre> |
| 1094 | 1094 | </td> |
| 1095 | 1095 | </tr> |
| 1096 | 1096 | <tr> |
| 1097 | | <td><pre><code class="zig">a.?</code></pre></td> |
| 1097 | <td><pre>{#syntax#}a.?{#endsyntax#}</pre></td> |
| 1098 | 1098 | <td> |
| 1099 | 1099 | <ul> |
| 1100 | 1100 | <li>{#link|Optionals#}</li> |
| ... | ... | @@ -1102,65 +1102,65 @@ unwrapped == 1234</code></pre> |
| 1102 | 1102 | </td> |
| 1103 | 1103 | <td> |
| 1104 | 1104 | Equivalent to: |
| 1105 | | <pre><code class="zig">a orelse unreachable</code></pre> |
| 1105 | <pre>{#syntax#}a orelse unreachable{#endsyntax#}</pre> |
| 1106 | 1106 | </td> |
| 1107 | 1107 | <td> |
| 1108 | | <pre><code class="zig">const value: ?u32 = 5678; |
| 1109 | | value.? == 5678</code></pre> |
| 1108 | <pre>{#syntax#}const value: ?u32 = 5678; |
| 1109 | value.? == 5678{#endsyntax#}</pre> |
| 1110 | 1110 | </td> |
| 1111 | 1111 | </tr> |
| 1112 | 1112 | <tr> |
| 1113 | | <td><pre><code class="zig">a catch b |
| 1114 | | a catch |err| b</code></pre></td> |
| 1113 | <td><pre>{#syntax#}a catch b |
| 1114 | a catch |err| b{#endsyntax#}</pre></td> |
| 1115 | 1115 | <td> |
| 1116 | 1116 | <ul> |
| 1117 | 1117 | <li>{#link|Error Unions|Errors#}</li> |
| 1118 | 1118 | </ul> |
| 1119 | 1119 | </td> |
| 1120 | | <td>If <code>a</code> is an <code>error</code>, |
| 1121 | | returns <code>b</code> ("default value"), |
| 1122 | | otherwise returns the unwrapped value of <code>a</code>. |
| 1123 | | Note that <code>b</code> may be a value of type {#link|noreturn#}. |
| 1124 | | <code>err</code> is the <code>error</code> and is in scope of the expression <code>b</code>. |
| 1120 | <td>If {#syntax#}a{#endsyntax#} is an {#syntax#}error{#endsyntax#}, |
| 1121 | returns {#syntax#}b{#endsyntax#} ("default value"), |
| 1122 | otherwise returns the unwrapped value of {#syntax#}a{#endsyntax#}. |
| 1123 | Note that {#syntax#}b{#endsyntax#} may be a value of type {#link|noreturn#}. |
| 1124 | {#syntax#}err{#endsyntax#} is the {#syntax#}error{#endsyntax#} and is in scope of the expression {#syntax#}b{#endsyntax#}. |
| 1125 | 1125 | </td> |
| 1126 | 1126 | <td> |
| 1127 | | <pre><code class="zig">const value: error!u32 = error.Broken; |
| 1127 | <pre>{#syntax#}const value: error!u32 = error.Broken; |
| 1128 | 1128 | const unwrapped = value catch 1234; |
| 1129 | | unwrapped == 1234</code></pre> |
| 1129 | unwrapped == 1234{#endsyntax#}</pre> |
| 1130 | 1130 | </td> |
| 1131 | 1131 | </tr> |
| 1132 | 1132 | <tr> |
| 1133 | | <td><pre><code class="zig">a and b<code></pre></td> |
| 1133 | <td><pre>{#syntax#}a and b{#endsyntax#}</pre></td> |
| 1134 | 1134 | <td> |
| 1135 | 1135 | <ul> |
| 1136 | 1136 | <li>{#link|bool|Primitive Types#}</li> |
| 1137 | 1137 | </ul> |
| 1138 | 1138 | </td> |
| 1139 | 1139 | <td> |
| 1140 | | If <code>a</code> is <code>false</code>, returns <code>false</code> |
| 1141 | | without evaluating <code>b</code>. Otherwise, returns <code>b</code>. |
| 1140 | If {#syntax#}a{#endsyntax#} is {#syntax#}false{#endsyntax#}, returns {#syntax#}false{#endsyntax#} |
| 1141 | without evaluating {#syntax#}b{#endsyntax#}. Otherwise, returns {#syntax#}b{#endsyntax#}. |
| 1142 | 1142 | </td> |
| 1143 | 1143 | <td> |
| 1144 | | <pre><code class="zig">false and true == false</code></pre> |
| 1144 | <pre>{#syntax#}false and true == false{#endsyntax#}</pre> |
| 1145 | 1145 | </td> |
| 1146 | 1146 | </tr> |
| 1147 | 1147 | <tr> |
| 1148 | | <td><pre><code class="zig">a or b<code></pre></td> |
| 1148 | <td><pre>{#syntax#}a or b{#endsyntax#}</pre></td> |
| 1149 | 1149 | <td> |
| 1150 | 1150 | <ul> |
| 1151 | 1151 | <li>{#link|bool|Primitive Types#}</li> |
| 1152 | 1152 | </ul> |
| 1153 | 1153 | </td> |
| 1154 | 1154 | <td> |
| 1155 | | If <code>a</code> is <code>true</code>, returns <code>true</code> |
| 1156 | | without evaluating <code>b</code>. Otherwise, returns <code>b</code>. |
| 1155 | If {#syntax#}a{#endsyntax#} is {#syntax#}true{#endsyntax#}, returns {#syntax#}true{#endsyntax#} |
| 1156 | without evaluating {#syntax#}b{#endsyntax#}. Otherwise, returns {#syntax#}b{#endsyntax#}. |
| 1157 | 1157 | </td> |
| 1158 | 1158 | <td> |
| 1159 | | <pre><code class="zig">false or true == true</code></pre> |
| 1159 | <pre>{#syntax#}false or true == true{#endsyntax#}</pre> |
| 1160 | 1160 | </td> |
| 1161 | 1161 | </tr> |
| 1162 | 1162 | <tr> |
| 1163 | | <td><pre><code class="zig">!a<code></pre></td> |
| 1163 | <td><pre>{#syntax#}!a{#endsyntax#}</pre></td> |
| 1164 | 1164 | <td> |
| 1165 | 1165 | <ul> |
| 1166 | 1166 | <li>{#link|bool|Primitive Types#}</li> |
| ... | ... | @@ -1170,11 +1170,11 @@ unwrapped == 1234</code></pre> |
| 1170 | 1170 | Boolean NOT. |
| 1171 | 1171 | </td> |
| 1172 | 1172 | <td> |
| 1173 | | <pre><code class="zig">!false == true</code></pre> |
| 1173 | <pre>{#syntax#}!false == true{#endsyntax#}</pre> |
| 1174 | 1174 | </td> |
| 1175 | 1175 | </tr> |
| 1176 | 1176 | <tr> |
| 1177 | | <td><pre><code class="zig">a == b<code></pre></td> |
| 1177 | <td><pre>{#syntax#}a == b{#endsyntax#}</pre></td> |
| 1178 | 1178 | <td> |
| 1179 | 1179 | <ul> |
| 1180 | 1180 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1184,30 +1184,30 @@ unwrapped == 1234</code></pre> |
| 1184 | 1184 | </ul> |
| 1185 | 1185 | </td> |
| 1186 | 1186 | <td> |
| 1187 | | Returns <code>true</code> if a and b are equal, otherwise returns <code>false</code>. |
| 1187 | Returns {#syntax#}true{#endsyntax#} if a and b are equal, otherwise returns {#syntax#}false{#endsyntax#}. |
| 1188 | 1188 | Invokes {#link|Peer Type Resolution#} for the operands. |
| 1189 | 1189 | </td> |
| 1190 | 1190 | <td> |
| 1191 | | <pre><code class="zig">(1 == 1) == true</code></pre> |
| 1191 | <pre>{#syntax#}(1 == 1) == true{#endsyntax#}</pre> |
| 1192 | 1192 | </td> |
| 1193 | 1193 | </tr> |
| 1194 | 1194 | <tr> |
| 1195 | | <td><pre><code class="zig">a == null<code></pre></td> |
| 1195 | <td><pre>{#syntax#}a == null{#endsyntax#}</pre></td> |
| 1196 | 1196 | <td> |
| 1197 | 1197 | <ul> |
| 1198 | 1198 | <li>{#link|Optionals#}</li> |
| 1199 | 1199 | </ul> |
| 1200 | 1200 | </td> |
| 1201 | 1201 | <td> |
| 1202 | | Returns <code>true</code> if a is <code>null</code>, otherwise returns <code>false</code>. |
| 1202 | Returns {#syntax#}true{#endsyntax#} if a is {#syntax#}null{#endsyntax#}, otherwise returns {#syntax#}false{#endsyntax#}. |
| 1203 | 1203 | </td> |
| 1204 | 1204 | <td> |
| 1205 | | <pre><code class="zig">const value: ?u32 = null; |
| 1206 | | value == null</code></pre> |
| 1205 | <pre>{#syntax#}const value: ?u32 = null; |
| 1206 | value == null{#endsyntax#}</pre> |
| 1207 | 1207 | </td> |
| 1208 | 1208 | </tr> |
| 1209 | 1209 | <tr> |
| 1210 | | <td><pre><code class="zig">a != b<code></pre></td> |
| 1210 | <td><pre>{#syntax#}a != b{#endsyntax#}</pre></td> |
| 1211 | 1211 | <td> |
| 1212 | 1212 | <ul> |
| 1213 | 1213 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1217,15 +1217,15 @@ value == null</code></pre> |
| 1217 | 1217 | </ul> |
| 1218 | 1218 | </td> |
| 1219 | 1219 | <td> |
| 1220 | | Returns <code>false</code> if a and b are equal, otherwise returns <code>true</code>. |
| 1220 | Returns {#syntax#}false{#endsyntax#} if a and b are equal, otherwise returns {#syntax#}true{#endsyntax#}. |
| 1221 | 1221 | Invokes {#link|Peer Type Resolution#} for the operands. |
| 1222 | 1222 | </td> |
| 1223 | 1223 | <td> |
| 1224 | | <pre><code class="zig">(1 != 1) == false</code></pre> |
| 1224 | <pre>{#syntax#}(1 != 1) == false{#endsyntax#}</pre> |
| 1225 | 1225 | </td> |
| 1226 | 1226 | </tr> |
| 1227 | 1227 | <tr> |
| 1228 | | <td><pre><code class="zig">a &gt; b<code></pre></td> |
| 1228 | <td><pre>{#syntax#}a > b{#endsyntax#}</pre></td> |
| 1229 | 1229 | <td> |
| 1230 | 1230 | <ul> |
| 1231 | 1231 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1233,15 +1233,15 @@ value == null</code></pre> |
| 1233 | 1233 | </ul> |
| 1234 | 1234 | </td> |
| 1235 | 1235 | <td> |
| 1236 | | Returns <code>true</code> if a is greater than b, otherwise returns <code>false</code>. |
| 1236 | Returns {#syntax#}true{#endsyntax#} if a is greater than b, otherwise returns {#syntax#}false{#endsyntax#}. |
| 1237 | 1237 | Invokes {#link|Peer Type Resolution#} for the operands. |
| 1238 | 1238 | </td> |
| 1239 | 1239 | <td> |
| 1240 | | <pre><code class="zig">(2 &gt; 1) == true</code></pre> |
| 1240 | <pre>{#syntax#}(2 > 1) == true{#endsyntax#}</pre> |
| 1241 | 1241 | </td> |
| 1242 | 1242 | </tr> |
| 1243 | 1243 | <tr> |
| 1244 | | <td><pre><code class="zig">a &gt;= b<code></pre></td> |
| 1244 | <td><pre>{#syntax#}a >= b{#endsyntax#}</pre></td> |
| 1245 | 1245 | <td> |
| 1246 | 1246 | <ul> |
| 1247 | 1247 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1249,15 +1249,15 @@ value == null</code></pre> |
| 1249 | 1249 | </ul> |
| 1250 | 1250 | </td> |
| 1251 | 1251 | <td> |
| 1252 | | Returns <code>true</code> if a is greater than or equal to b, otherwise returns <code>false</code>. |
| 1252 | Returns {#syntax#}true{#endsyntax#} if a is greater than or equal to b, otherwise returns {#syntax#}false{#endsyntax#}. |
| 1253 | 1253 | Invokes {#link|Peer Type Resolution#} for the operands. |
| 1254 | 1254 | </td> |
| 1255 | 1255 | <td> |
| 1256 | | <pre><code class="zig">(2 &gt;= 1) == true</code></pre> |
| 1256 | <pre>{#syntax#}(2 >= 1) == true{#endsyntax#}</pre> |
| 1257 | 1257 | </td> |
| 1258 | 1258 | </tr> |
| 1259 | 1259 | <tr> |
| 1260 | | <td><pre><code class="zig">a &lt; b<code></pre></td> |
| 1260 | <td><pre>{#syntax#}a < b{#endsyntax#}</pre></td> |
| 1261 | 1261 | <td> |
| 1262 | 1262 | <ul> |
| 1263 | 1263 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1265,15 +1265,15 @@ value == null</code></pre> |
| 1265 | 1265 | </ul> |
| 1266 | 1266 | </td> |
| 1267 | 1267 | <td> |
| 1268 | | Returns <code>true</code> if a is less than b, otherwise returns <code>false</code>. |
| 1268 | Returns {#syntax#}true{#endsyntax#} if a is less than b, otherwise returns {#syntax#}false{#endsyntax#}. |
| 1269 | 1269 | Invokes {#link|Peer Type Resolution#} for the operands. |
| 1270 | 1270 | </td> |
| 1271 | 1271 | <td> |
| 1272 | | <pre><code class="zig">(1 &lt; 2) == true</code></pre> |
| 1272 | <pre>{#syntax#}(1 < 2) == true{#endsyntax#}></pre> |
| 1273 | 1273 | </td> |
| 1274 | 1274 | </tr> |
| 1275 | 1275 | <tr> |
| 1276 | | <td><pre><code class="zig">a &lt;= b<code></pre></td> |
| 1276 | <td><pre>{#syntax#}a <= b{#endsyntax#}</pre></td> |
| 1277 | 1277 | <td> |
| 1278 | 1278 | <ul> |
| 1279 | 1279 | <li>{#link|Integers#}</li> |
| ... | ... | @@ -1281,15 +1281,15 @@ value == null</code></pre> |
| 1281 | 1281 | </ul> |
| 1282 | 1282 | </td> |
| 1283 | 1283 | <td> |
| 1284 | | Returns <code>true</code> if a is less than or equal to b, otherwise returns <code>false</code>. |
| 1284 | Returns {#syntax#}true{#endsyntax#} if a is less than or equal to b, otherwise returns {#syntax#}false{#endsyntax#}. |
| 1285 | 1285 | Invokes {#link|Peer Type Resolution#} for the operands. |
| 1286 | 1286 | </td> |
| 1287 | 1287 | <td> |
| 1288 | | <pre><code class="zig">(1 &lt;= 2) == true</code></pre> |
| 1288 | <pre>{#syntax#}(1 <= 2) == true{#endsyntax#}</pre> |
| 1289 | 1289 | </td> |
| 1290 | 1290 | </tr> |
| 1291 | 1291 | <tr> |
| 1292 | | <td><pre><code class="zig">a ++ b<code></pre></td> |
| 1292 | <td><pre>{#syntax#}a ++ b{#endsyntax#}</pre></td> |
| 1293 | 1293 | <td> |
| 1294 | 1294 | <ul> |
| 1295 | 1295 | <li>{#link|Arrays#}</li> |
| ... | ... | @@ -1298,19 +1298,19 @@ value == null</code></pre> |
| 1298 | 1298 | <td> |
| 1299 | 1299 | Array concatenation. |
| 1300 | 1300 | <ul> |
| 1301 | | <li>Only available when <code>a</code> and <code>b</code> are {#link|compile-time known|comptime#}. |
| 1301 | <li>Only available when {#syntax#}a{#endsyntax#} and {#syntax#}b{#endsyntax#} are {#link|compile-time known|comptime#}. |
| 1302 | 1302 | </ul> |
| 1303 | 1303 | </td> |
| 1304 | 1304 | <td> |
| 1305 | | <pre><code class="zig">const mem = @import("std").mem; |
| 1305 | <pre>{#syntax#}const mem = @import("std").mem; |
| 1306 | 1306 | const array1 = []u32{1,2}; |
| 1307 | 1307 | const array2 = []u32{3,4}; |
| 1308 | 1308 | const together = array1 ++ array2; |
| 1309 | | mem.eql(u32, together, []u32{1,2,3,4})</code></pre> |
| 1309 | mem.eql(u32, together, []u32{1,2,3,4}){#endsyntax#}</pre> |
| 1310 | 1310 | </td> |
| 1311 | 1311 | </tr> |
| 1312 | 1312 | <tr> |
| 1313 | | <td><pre><code class="zig">a ** b<code></pre></td> |
| 1313 | <td><pre>{#syntax#}a ** b{#endsyntax#}</pre></td> |
| 1314 | 1314 | <td> |
| 1315 | 1315 | <ul> |
| 1316 | 1316 | <li>{#link|Arrays#}</li> |
| ... | ... | @@ -1319,17 +1319,17 @@ mem.eql(u32, together, []u32{1,2,3,4})</code></pre> |
| 1319 | 1319 | <td> |
| 1320 | 1320 | Array multiplication. |
| 1321 | 1321 | <ul> |
| 1322 | | <li>Only available when <code>a</code> and <code>b</code> are {#link|compile-time known|comptime#}. |
| 1322 | <li>Only available when {#syntax#}a{#endsyntax#} and {#syntax#}b{#endsyntax#} are {#link|compile-time known|comptime#}. |
| 1323 | 1323 | </ul> |
| 1324 | 1324 | </td> |
| 1325 | 1325 | <td> |
| 1326 | | <pre><code class="zig">const mem = @import("std").mem; |
| 1326 | <pre>{#syntax#}const mem = @import("std").mem; |
| 1327 | 1327 | const pattern = "ab" ** 3; |
| 1328 | | mem.eql(u8, pattern, "ababab")</code></pre> |
| 1328 | mem.eql(u8, pattern, "ababab"){#endsyntax#}</pre> |
| 1329 | 1329 | </td> |
| 1330 | 1330 | </tr> |
| 1331 | 1331 | <tr> |
| 1332 | | <td><pre><code class="zig">a.*<code></pre></td> |
| 1332 | <td><pre>{#syntax#}a.*{#endsyntax#}</pre></td> |
| 1333 | 1333 | <td> |
| 1334 | 1334 | <ul> |
| 1335 | 1335 | <li>{#link|Pointers#}</li> |
| ... | ... | @@ -1339,13 +1339,13 @@ mem.eql(u8, pattern, "ababab")</code></pre> |
| 1339 | 1339 | Pointer dereference. |
| 1340 | 1340 | </td> |
| 1341 | 1341 | <td> |
| 1342 | | <pre><code class="zig">const x: u32 = 1234; |
| 1343 | | const ptr = &amp;x; |
| 1344 | | x.* == 1234</code></pre> |
| 1342 | <pre>{#syntax#}const x: u32 = 1234; |
| 1343 | const ptr = &x; |
| 1344 | x.* == 1234{#endsyntax#}</pre> |
| 1345 | 1345 | </td> |
| 1346 | 1346 | </tr> |
| 1347 | 1347 | <tr> |
| 1348 | | <td><pre><code class="zig">&amp;a<code></pre></td> |
| 1348 | <td><pre>{#syntax#}&amp;a{#endsyntax#}</pre></td> |
| 1349 | 1349 | <td> |
| 1350 | 1350 | All types |
| 1351 | 1351 | </td> |
| ... | ... | @@ -1353,13 +1353,13 @@ x.* == 1234</code></pre> |
| 1353 | 1353 | Address of. |
| 1354 | 1354 | </td> |
| 1355 | 1355 | <td> |
| 1356 | | <pre><code class="zig">const x: u32 = 1234; |
| 1357 | | const ptr = &amp;x; |
| 1358 | | x.* == 1234</code></pre> |
| 1356 | <pre>{#syntax#}const x: u32 = 1234; |
| 1357 | const ptr = &x; |
| 1358 | x.* == 1234{#endsyntax#}</pre> |
| 1359 | 1359 | </td> |
| 1360 | 1360 | </tr> |
| 1361 | 1361 | <tr> |
| 1362 | | <td><pre><code class="zig">a || b<code></pre></td> |
| 1362 | <td><pre>{#syntax#}a || b{#endsyntax#}</pre></td> |
| 1363 | 1363 | <td> |
| 1364 | 1364 | <ul> |
| 1365 | 1365 | <li>{#link|Error Set Type#}</li> |
| ... | ... | @@ -1369,30 +1369,30 @@ x.* == 1234</code></pre> |
| 1369 | 1369 | {#link|Merging Error Sets#} |
| 1370 | 1370 | </td> |
| 1371 | 1371 | <td> |
| 1372 | | <pre><code class="zig">const A = error{One}; |
| 1372 | <pre>{#syntax#}const A = error{One}; |
| 1373 | 1373 | const B = error{Two}; |
| 1374 | | (A || B) == error{One, Two}</code></pre> |
| 1374 | (A || B) == error{One, Two}{#endsyntax#}</pre> |
| 1375 | 1375 | </td> |
| 1376 | 1376 | </tr> |
| 1377 | 1377 | </table> |
| 1378 | 1378 | </div> |
| 1379 | 1379 | {#header_close#} |
| 1380 | 1380 | {#header_open|Precedence#} |
| 1381 | | <pre><code>x() x[] x.y |
| 1381 | <pre>{#syntax#}x() x[] x.y |
| 1382 | 1382 | a!b |
| 1383 | | !x -x -%x ~x &amp;x ?x |
| 1383 | !x -x -%x ~x &x ?x |
| 1384 | 1384 | x{} x.* x.? |
| 1385 | 1385 | ! * / % ** *% || |
| 1386 | 1386 | + - ++ +% -% |
| 1387 | | &lt;&lt; &gt;&gt; |
| 1388 | | &amp; |
| 1387 | << >> |
| 1388 | & |
| 1389 | 1389 | ^ |
| 1390 | 1390 | | |
| 1391 | | == != &lt; &gt; &lt;= &gt;= |
| 1391 | == != < > <= >= |
| 1392 | 1392 | and |
| 1393 | 1393 | or |
| 1394 | 1394 | orelse catch |
| 1395 | | = *= /= %= += -= &lt;&lt;= &gt;&gt;= &amp;= ^= |=</code></pre> |
| 1395 | = *= /= %= += -= <<= >>= &= ^= |={#endsyntax#}</pre> |
| 1396 | 1396 | {#header_close#} |
| 1397 | 1397 | {#header_close#} |
| 1398 | 1398 | {#header_open|Arrays#} |
| ... | ... | @@ -1641,7 +1641,7 @@ test "pointer child type" { |
| 1641 | 1641 | </p> |
| 1642 | 1642 | <p> |
| 1643 | 1643 | Alignment depends on the CPU architecture, but is always a power of two, and |
| 1644 | | less than <code>1 &lt;&lt; 29</code>. |
| 1644 | less than {#syntax#}1 << 29{#endsyntax#}. |
| 1645 | 1645 | </p> |
| 1646 | 1646 | <p> |
| 1647 | 1647 | In Zig, a pointer type has an alignment value. If the value is equal to the |
| ... | ... | @@ -1661,8 +1661,8 @@ test "variable alignment" { |
| 1661 | 1661 | } |
| 1662 | 1662 | } |
| 1663 | 1663 | {#code_end#} |
| 1664 | | <p>In the same way that a <code>*i32</code> can be {#link|implicitly cast|Implicit Casts#} to a |
| 1665 | | <code>*const i32</code>, a pointer with a larger alignment can be implicitly |
| 1664 | <p>In the same way that a {#syntax#}*i32{#endsyntax#} can be {#link|implicitly cast|Implicit Casts#} to a |
| 1665 | {#syntax#}*const i32{#endsyntax#}, a pointer with a larger alignment can be implicitly |
| 1666 | 1666 | cast to a pointer with a smaller alignment, but not vice versa. |
| 1667 | 1667 | </p> |
| 1668 | 1668 | <p> |
| ... | ... | @@ -1717,14 +1717,14 @@ fn foo(bytes: []u8) u32 { |
| 1717 | 1717 | {#header_open|Type Based Alias Analysis#} |
| 1718 | 1718 | <p>Zig uses Type Based Alias Analysis (also known as Strict Aliasing) to |
| 1719 | 1719 | perform some optimizations. This means that pointers of different types must |
| 1720 | | not alias the same memory, with the exception of <code>u8</code>. Pointers to |
| 1721 | | <code>u8</code> can alias any memory. |
| 1720 | not alias the same memory, with the exception of {#syntax#}u8{#endsyntax#}. Pointers to |
| 1721 | {#syntax#}u8{#endsyntax#} can alias any memory. |
| 1722 | 1722 | </p> |
| 1723 | 1723 | <p>As an example, this code produces undefined behavior:</p> |
| 1724 | | <pre><code class="zig">@ptrCast(*u32, f32(12.34)).*</code></pre> |
| 1724 | <pre>{#syntax#}@ptrCast(*u32, f32(12.34)).*{#endsyntax#}</pre> |
| 1725 | 1725 | <p>Instead, use {#link|@bitCast#}: |
| 1726 | | <pre><code class="zig">@bitCast(u32, f32(12.34))</code></pre> |
| 1727 | | <p>As an added benefit, the <code>@bitCast</code> version works at compile-time.</p> |
| 1726 | <pre>{#syntax#}@bitCast(u32, f32(12.34)){#endsyntax#}</pre> |
| 1727 | <p>As an added benefit, the {#syntax#}@bitCast{#endsyntax#} version works at compile-time.</p> |
| 1728 | 1728 | {#see_also|Slices|Memory#} |
| 1729 | 1729 | {#header_close#} |
| 1730 | 1730 | {#header_close#} |
| ... | ... | @@ -1952,9 +1952,9 @@ test "linked list" { |
| 1952 | 1952 | <ul> |
| 1953 | 1953 | <li>If the struct is in the initialization expression of a variable, it gets named after |
| 1954 | 1954 | that variable.</li> |
| 1955 | | <li>If the struct is in the <code>return</code> expression, it gets named after |
| 1955 | <li>If the struct is in the {#syntax#}return{#endsyntax#} expression, it gets named after |
| 1956 | 1956 | the function it is returning from, with the parameter values serialized.</li> |
| 1957 | | <li>Otherwise, the struct gets a same such as <code>(anonymous struct at file.zig:7:38)</code>.</li> |
| 1957 | <li>Otherwise, the struct gets a same such as {#syntax#}(anonymous struct at file.zig:7:38){#endsyntax#}.</li> |
| 1958 | 1958 | </ul> |
| 1959 | 1959 | {#code_begin|exe|struct_name#} |
| 1960 | 1960 | const std = @import("std"); |
| ... | ... | @@ -2086,7 +2086,7 @@ const Foo = enum { A, B, C }; |
| 2086 | 2086 | export fn entry(foo: Foo) void { } |
| 2087 | 2087 | {#code_end#} |
| 2088 | 2088 | <p> |
| 2089 | | For a C-ABI-compatible enum, use <code class="zig">extern enum</code>: |
| 2089 | For a C-ABI-compatible enum, use {#syntax#}extern enum{#endsyntax#}: |
| 2090 | 2090 | </p> |
| 2091 | 2091 | {#code_begin|obj#} |
| 2092 | 2092 | const Foo = extern enum { A, B, C }; |
| ... | ... | @@ -2095,7 +2095,7 @@ export fn entry(foo: Foo) void { } |
| 2095 | 2095 | {#header_close#} |
| 2096 | 2096 | {#header_open|packed enum#} |
| 2097 | 2097 | <p>By default, the size of enums is not guaranteed.</p> |
| 2098 | | <p><code>packed enum</code> causes the size of the enum to be the same as the size of the integer tag type |
| 2098 | <p>{#syntax#}packed enum{#endsyntax#} causes the size of the enum to be the same as the size of the integer tag type |
| 2099 | 2099 | of the enum:</p> |
| 2100 | 2100 | {#code_begin|test#} |
| 2101 | 2101 | const std = @import("std"); |
| ... | ... | @@ -2246,7 +2246,7 @@ test "access variable after block scope" { |
| 2246 | 2246 | x += 1; |
| 2247 | 2247 | } |
| 2248 | 2248 | {#code_end#} |
| 2249 | | <p>Blocks are expressions. When labeled, <code>break</code> can be used |
| 2249 | <p>Blocks are expressions. When labeled, {#syntax#}break{#endsyntax#} can be used |
| 2250 | 2250 | to return a value from the block: |
| 2251 | 2251 | </p> |
| 2252 | 2252 | {#code_begin|test#} |
| ... | ... | @@ -2264,7 +2264,7 @@ test "labeled break from labeled block expression" { |
| 2264 | 2264 | assert(y == 124); |
| 2265 | 2265 | } |
| 2266 | 2266 | {#code_end#} |
| 2267 | | <p>Here, <code>blk</code> can be any name.</p> |
| 2267 | <p>Here, {#syntax#}blk{#endsyntax#} can be any name.</p> |
| 2268 | 2268 | {#see_also|Labeled while|Labeled for#} |
| 2269 | 2269 | {#header_close#} |
| 2270 | 2270 | {#header_open|switch#} |
| ... | ... | @@ -2380,7 +2380,7 @@ test "while basic" { |
| 2380 | 2380 | } |
| 2381 | 2381 | {#code_end#} |
| 2382 | 2382 | <p> |
| 2383 | | Use <code>break</code> to exit a while loop early. |
| 2383 | Use {#syntax#}break{#endsyntax#} to exit a while loop early. |
| 2384 | 2384 | </p> |
| 2385 | 2385 | {#code_begin|test|while#} |
| 2386 | 2386 | const assert = @import("std").debug.assert; |
| ... | ... | @@ -2396,7 +2396,7 @@ test "while break" { |
| 2396 | 2396 | } |
| 2397 | 2397 | {#code_end#} |
| 2398 | 2398 | <p> |
| 2399 | | Use <code>continue</code> to jump back to the beginning of the loop. |
| 2399 | Use {#syntax#}continue{#endsyntax#} to jump back to the beginning of the loop. |
| 2400 | 2400 | </p> |
| 2401 | 2401 | {#code_begin|test|while#} |
| 2402 | 2402 | const assert = @import("std").debug.assert; |
| ... | ... | @@ -2414,7 +2414,7 @@ test "while continue" { |
| 2414 | 2414 | {#code_end#} |
| 2415 | 2415 | <p> |
| 2416 | 2416 | While loops support a continue expression which is executed when the loop |
| 2417 | | is continued. The <code>continue</code> keyword respects this expression. |
| 2417 | is continued. The {#syntax#}continue{#endsyntax#} keyword respects this expression. |
| 2418 | 2418 | </p> |
| 2419 | 2419 | {#code_begin|test|while#} |
| 2420 | 2420 | const assert = @import("std").debug.assert; |
| ... | ... | @@ -2436,13 +2436,13 @@ test "while loop continue expression, more complicated" { |
| 2436 | 2436 | {#code_end#} |
| 2437 | 2437 | <p> |
| 2438 | 2438 | While loops are expressions. The result of the expression is the |
| 2439 | | result of the <code>else</code> clause of a while loop, which is executed when |
| 2439 | result of the {#syntax#}else{#endsyntax#} clause of a while loop, which is executed when |
| 2440 | 2440 | the condition of the while loop is tested as false. |
| 2441 | 2441 | </p> |
| 2442 | 2442 | <p> |
| 2443 | | <code>break</code>, like <code>return</code>, accepts a value |
| 2444 | | parameter. This is the result of the <code>while</code> expression. |
| 2445 | | When you <code>break</code> from a while loop, the <code>else</code> branch is not |
| 2443 | {#syntax#}break{#endsyntax#}, like {#syntax#}return{#endsyntax#}, accepts a value |
| 2444 | parameter. This is the result of the {#syntax#}while{#endsyntax#} expression. |
| 2445 | When you {#syntax#}break{#endsyntax#} from a while loop, the {#syntax#}else{#endsyntax#} branch is not |
| 2446 | 2446 | evaluated. |
| 2447 | 2447 | </p> |
| 2448 | 2448 | {#code_begin|test|while#} |
| ... | ... | @@ -2463,8 +2463,8 @@ fn rangeHasNumber(begin: usize, end: usize, number: usize) bool { |
| 2463 | 2463 | } |
| 2464 | 2464 | {#code_end#} |
| 2465 | 2465 | {#header_open|Labeled while#} |
| 2466 | | <p>When a <code>while</code> loop is labeled, it can be referenced from a <code>break</code> |
| 2467 | | or <code>continue</code> from within a nested loop:</p> |
| 2466 | <p>When a {#syntax#}while{#endsyntax#} loop is labeled, it can be referenced from a {#syntax#}break{#endsyntax#} |
| 2467 | or {#syntax#}continue{#endsyntax#} from within a nested loop:</p> |
| 2468 | 2468 | {#code_begin|test#} |
| 2469 | 2469 | test "nested break" { |
| 2470 | 2470 | outer: while (true) { |
| ... | ... | @@ -2491,11 +2491,11 @@ test "nested continue" { |
| 2491 | 2491 | exits. |
| 2492 | 2492 | </p> |
| 2493 | 2493 | <p> |
| 2494 | | When the <code>|x|</code> syntax is present on a <code>while</code> expression, |
| 2494 | When the {#syntax#}|x|{#endsyntax#} syntax is present on a {#syntax#}while{#endsyntax#} expression, |
| 2495 | 2495 | the while condition must have an {#link|Optional Type#}. |
| 2496 | 2496 | </p> |
| 2497 | 2497 | <p> |
| 2498 | | The <code>else</code> branch is allowed on optional iteration. In this case, it will |
| 2498 | The {#syntax#}else{#endsyntax#} branch is allowed on optional iteration. In this case, it will |
| 2499 | 2499 | be executed on the first null value encountered. |
| 2500 | 2500 | </p> |
| 2501 | 2501 | {#code_begin|test|while#} |
| ... | ... | @@ -2537,7 +2537,7 @@ fn eventuallyNullSequence() ?u32 { |
| 2537 | 2537 | the loop is finished. |
| 2538 | 2538 | </p> |
| 2539 | 2539 | <p> |
| 2540 | | When the <code>else |x|</code> syntax is present on a <code>while</code> expression, |
| 2540 | When the {#syntax#}else |x|{#endsyntax#} syntax is present on a {#syntax#}while{#endsyntax#} expression, |
| 2541 | 2541 | the while condition must have an {#link|Error Union Type#}. |
| 2542 | 2542 | </p> |
| 2543 | 2543 | {#code_begin|test|while#} |
| ... | ... | @@ -2593,7 +2593,7 @@ fn typeNameLength(comptime T: type) usize { |
| 2593 | 2593 | } |
| 2594 | 2594 | {#code_end#} |
| 2595 | 2595 | <p> |
| 2596 | | It is recommended to use <code>inline</code> loops only for one of these reasons: |
| 2596 | It is recommended to use {#syntax#}inline{#endsyntax#} loops only for one of these reasons: |
| 2597 | 2597 | </p> |
| 2598 | 2598 | <ul> |
| 2599 | 2599 | <li>You need the loop to execute at {#link|comptime#} for the semantics to work.</li> |
| ... | ... | @@ -2671,8 +2671,8 @@ test "for else" { |
| 2671 | 2671 | } |
| 2672 | 2672 | {#code_end#} |
| 2673 | 2673 | {#header_open|Labeled for#} |
| 2674 | | <p>When a <code>for</code> loop is labeled, it can be referenced from a <code>break</code> |
| 2675 | | or <code>continue</code> from within a nested loop:</p> |
| 2674 | <p>When a {#syntax#}for{#endsyntax#} loop is labeled, it can be referenced from a {#syntax#}break{#endsyntax#} |
| 2675 | or {#syntax#}continue{#endsyntax#} from within a nested loop:</p> |
| 2676 | 2676 | {#code_begin|test#} |
| 2677 | 2677 | const std = @import("std"); |
| 2678 | 2678 | const assert = std.debug.assert; |
| ... | ... | @@ -2732,7 +2732,7 @@ fn typeNameLength(comptime T: type) usize { |
| 2732 | 2732 | } |
| 2733 | 2733 | {#code_end#} |
| 2734 | 2734 | <p> |
| 2735 | | It is recommended to use <code>inline</code> loops only for one of these reasons: |
| 2735 | It is recommended to use {#syntax#}inline{#endsyntax#} loops only for one of these reasons: |
| 2736 | 2736 | </p> |
| 2737 | 2737 | <ul> |
| 2738 | 2738 | <li>You need the loop to execute at {#link|comptime#} for the semantics to work.</li> |
| ... | ... | @@ -2932,13 +2932,13 @@ test "errdefer unwinding" { |
| 2932 | 2932 | {#header_close#} |
| 2933 | 2933 | {#header_open|unreachable#} |
| 2934 | 2934 | <p> |
| 2935 | | In <code>Debug</code> and <code>ReleaseSafe</code> mode, and when using <code>zig test</code>, |
| 2936 | | <code>unreachable</code> emits a call to <code>panic</code> with the message <code>reached unreachable code</code>. |
| 2935 | In {#syntax#}Debug{#endsyntax#} and {#syntax#}ReleaseSafe{#endsyntax#} mode, and when using <code>zig test</code>, |
| 2936 | {#syntax#}unreachable{#endsyntax#} emits a call to {#syntax#}panic{#endsyntax#} with the message <code>reached unreachable code</code>. |
| 2937 | 2937 | </p> |
| 2938 | 2938 | <p> |
| 2939 | | In <code>ReleaseFast</code> mode, the optimizer uses the assumption that <code>unreachable</code> code |
| 2940 | | will never be hit to perform optimizations. However, <code>zig test</code> even in <code>ReleaseFast</code> mode |
| 2941 | | still emits <code>unreachable</code> as calls to <code>panic</code>. |
| 2939 | In {#syntax#}ReleaseFast{#endsyntax#} mode, the optimizer uses the assumption that {#syntax#}unreachable{#endsyntax#} code |
| 2940 | will never be hit to perform optimizations. However, <code>zig test</code> even in {#syntax#}ReleaseFast{#endsyntax#} mode |
| 2941 | still emits {#syntax#}unreachable{#endsyntax#} as calls to {#syntax#}panic{#endsyntax#}. |
| 2942 | 2942 | </p> |
| 2943 | 2943 | {#header_open|Basics#} |
| 2944 | 2944 | {#code_begin|test#} |
| ... | ... | @@ -2984,17 +2984,17 @@ test "type of unreachable" { |
| 2984 | 2984 | {#header_close#} |
| 2985 | 2985 | {#header_open|noreturn#} |
| 2986 | 2986 | <p> |
| 2987 | | <code>noreturn</code> is the type of: |
| 2987 | {#syntax#}noreturn{#endsyntax#} is the type of: |
| 2988 | 2988 | </p> |
| 2989 | 2989 | <ul> |
| 2990 | | <li><code>break</code></li> |
| 2991 | | <li><code>continue</code></li> |
| 2992 | | <li><code>return</code></li> |
| 2993 | | <li><code>unreachable</code></li> |
| 2994 | | <li><code>while (true) {}</code></li> |
| 2990 | <li>{#syntax#}break{#endsyntax#}</li> |
| 2991 | <li>{#syntax#}continue{#endsyntax#}</li> |
| 2992 | <li>{#syntax#}return{#endsyntax#}</li> |
| 2993 | <li>{#syntax#}unreachable{#endsyntax#}</li> |
| 2994 | <li>{#syntax#}while (true) {}{#endsyntax#}</li> |
| 2995 | 2995 | </ul> |
| 2996 | | <p>When resolving types together, such as <code>if</code> clauses or <code>switch</code> prongs, |
| 2997 | | the <code>noreturn</code> type is compatible with every other type. Consider: |
| 2996 | <p>When resolving types together, such as {#syntax#}if{#endsyntax#} clauses or {#syntax#}switch{#endsyntax#} prongs, |
| 2997 | the {#syntax#}noreturn{#endsyntax#} type is compatible with every other type. Consider: |
| 2998 | 2998 | </p> |
| 2999 | 2999 | {#code_begin|test#} |
| 3000 | 3000 | fn foo(condition: bool, b: u32) void { |
| ... | ... | @@ -3005,7 +3005,7 @@ test "noreturn" { |
| 3005 | 3005 | foo(false, 1); |
| 3006 | 3006 | } |
| 3007 | 3007 | {#code_end#} |
| 3008 | | <p>Another use case for <code>noreturn</code> is the <code>exit</code> function:</p> |
| 3008 | <p>Another use case for {#syntax#}noreturn{#endsyntax#} is the {#syntax#}exit{#endsyntax#} function:</p> |
| 3009 | 3009 | {#code_begin|test#} |
| 3010 | 3010 | {#target_windows#} |
| 3011 | 3011 | pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: c_uint) noreturn; |
| ... | ... | @@ -3134,7 +3134,7 @@ test "fn reflection" { |
| 3134 | 3134 | </p> |
| 3135 | 3135 | <p> |
| 3136 | 3136 | The number of unique error values across the entire compilation should determine the size of the error set type. |
| 3137 | | However right now it is hard coded to be a <code>u16</code>. See <a href="https://github.com/ziglang/zig/issues/786">#768</a>. |
| 3137 | However right now it is hard coded to be a {#syntax#}u16{#endsyntax#}. See <a href="https://github.com/ziglang/zig/issues/786">#768</a>. |
| 3138 | 3138 | </p> |
| 3139 | 3139 | <p> |
| 3140 | 3140 | You can {#link|implicitly cast|Implicit Casts#} an error from a subset to its superset: |
| ... | ... | @@ -3197,7 +3197,7 @@ const err = (error {FileNotFound}).FileNotFound; |
| 3197 | 3197 | This becomes useful when using {#link|Inferred Error Sets#}. |
| 3198 | 3198 | </p> |
| 3199 | 3199 | {#header_open|The Global Error Set#} |
| 3200 | | <p><code>error</code> refers to the global error set. |
| 3200 | <p>{#syntax#}error{#endsyntax#} refers to the global error set. |
| 3201 | 3201 | This is the error set that contains all errors in the entire compilation unit. |
| 3202 | 3202 | It is a superset of all other error sets and a subset of none of them. |
| 3203 | 3203 | </p> |
| ... | ... | @@ -3216,7 +3216,7 @@ const err = (error {FileNotFound}).FileNotFound; |
| 3216 | 3216 | {#header_close#} |
| 3217 | 3217 | {#header_open|Error Union Type#} |
| 3218 | 3218 | <p> |
| 3219 | | An error set type and normal type can be combined with the <code>!</code> |
| 3219 | An error set type and normal type can be combined with the {#syntax#}!{#endsyntax#} |
| 3220 | 3220 | binary operator to form an error union type. You are likely to use an |
| 3221 | 3221 | error union type more often than an error set type by itself. |
| 3222 | 3222 | </p> |
| ... | ... | @@ -3263,14 +3263,14 @@ test "parse u64" { |
| 3263 | 3263 | } |
| 3264 | 3264 | {#code_end#} |
| 3265 | 3265 | <p> |
| 3266 | | Notice the return type is <code>!u64</code>. This means that the function |
| 3266 | Notice the return type is {#syntax#}!u64{#endsyntax#}. This means that the function |
| 3267 | 3267 | either returns an unsigned 64 bit integer, or an error. We left off the error set |
| 3268 | | to the left of the <code>!</code>, so the error set is inferred. |
| 3268 | to the left of the {#syntax#}!{#endsyntax#}, so the error set is inferred. |
| 3269 | 3269 | </p> |
| 3270 | 3270 | <p> |
| 3271 | 3271 | Within the function definition, you can see some return statements that return |
| 3272 | | an error, and at the bottom a return statement that returns a <code>u64</code>. |
| 3273 | | Both types {#link|implicitly cast|Implicit Casts#} to <code>error!u64</code>. |
| 3272 | an error, and at the bottom a return statement that returns a {#syntax#}u64{#endsyntax#}. |
| 3273 | Both types {#link|implicitly cast|Implicit Casts#} to {#syntax#}error!u64{#endsyntax#}. |
| 3274 | 3274 | </p> |
| 3275 | 3275 | <p> |
| 3276 | 3276 | What it looks like to use this function varies depending on what you're |
| ... | ... | @@ -3283,7 +3283,7 @@ test "parse u64" { |
| 3283 | 3283 | <li>You want to take a different action for each possible error.</li> |
| 3284 | 3284 | </ul> |
| 3285 | 3285 | {#header_open|catch#} |
| 3286 | | <p>If you want to provide a default value, you can use the <code>catch</code> binary operator:</p> |
| 3286 | <p>If you want to provide a default value, you can use the {#syntax#}catch{#endsyntax#} binary operator:</p> |
| 3287 | 3287 | {#code_begin|syntax#} |
| 3288 | 3288 | fn doAThing(str: []u8) void { |
| 3289 | 3289 | const number = parseU64(str, 10) catch 13; |
| ... | ... | @@ -3291,9 +3291,9 @@ fn doAThing(str: []u8) void { |
| 3291 | 3291 | } |
| 3292 | 3292 | {#code_end#} |
| 3293 | 3293 | <p> |
| 3294 | | In this code, <code>number</code> will be equal to the successfully parsed string, or |
| 3295 | | a default value of 13. The type of the right hand side of the binary <code>catch</code> operator must |
| 3296 | | match the unwrapped error union type, or be of type <code>noreturn</code>. |
| 3294 | In this code, {#syntax#}number{#endsyntax#} will be equal to the successfully parsed string, or |
| 3295 | a default value of 13. The type of the right hand side of the binary {#syntax#}catch{#endsyntax#} operator must |
| 3296 | match the unwrapped error union type, or be of type {#syntax#}noreturn{#endsyntax#}. |
| 3297 | 3297 | </p> |
| 3298 | 3298 | {#header_close#} |
| 3299 | 3299 | {#header_open|try#} |
| ... | ... | @@ -3306,7 +3306,7 @@ fn doAThing(str: []u8) !void { |
| 3306 | 3306 | } |
| 3307 | 3307 | {#code_end#} |
| 3308 | 3308 | <p> |
| 3309 | | There is a shortcut for this. The <code>try</code> expression: |
| 3309 | There is a shortcut for this. The {#syntax#}try{#endsyntax#} expression: |
| 3310 | 3310 | </p> |
| 3311 | 3311 | {#code_begin|syntax#} |
| 3312 | 3312 | fn doAThing(str: []u8) !void { |
| ... | ... | @@ -3315,7 +3315,7 @@ fn doAThing(str: []u8) !void { |
| 3315 | 3315 | } |
| 3316 | 3316 | {#code_end#} |
| 3317 | 3317 | <p> |
| 3318 | | <code>try</code> evaluates an error union expression. If it is an error, it returns |
| 3318 | {#syntax#}try{#endsyntax#} evaluates an error union expression. If it is an error, it returns |
| 3319 | 3319 | from the current function with the same error. Otherwise, the expression results in |
| 3320 | 3320 | the unwrapped value. |
| 3321 | 3321 | </p> |
| ... | ... | @@ -3327,7 +3327,7 @@ fn doAThing(str: []u8) !void { |
| 3327 | 3327 | {#code_begin|syntax#}const number = parseU64("1234", 10) catch unreachable;{#code_end#} |
| 3328 | 3328 | <p> |
| 3329 | 3329 | Here we know for sure that "1234" will parse successfully. So we put the |
| 3330 | | <code>unreachable</code> value on the right hand side. <code>unreachable</code> generates |
| 3330 | {#syntax#}unreachable{#endsyntax#} value on the right hand side. {#syntax#}unreachable{#endsyntax#} generates |
| 3331 | 3331 | a panic in Debug and ReleaseSafe modes and undefined behavior in ReleaseFast mode. So, while we're debugging the |
| 3332 | 3332 | application, if there <em>was</em> a surprise error here, the application would crash |
| 3333 | 3333 | appropriately. |
| ... | ... | @@ -3352,7 +3352,7 @@ fn doAThing(str: []u8) void { |
| 3352 | 3352 | {#header_open|errdefer#} |
| 3353 | 3353 | <p> |
| 3354 | 3354 | The other component to error handling is defer statements. |
| 3355 | | In addition to an unconditional {#link|defer#}, Zig has <code>errdefer</code>, |
| 3355 | In addition to an unconditional {#link|defer#}, Zig has {#syntax#}errdefer{#endsyntax#}, |
| 3356 | 3356 | which evaluates the deferred expression on block exit path if and only if |
| 3357 | 3357 | the function returned with an error from the block. |
| 3358 | 3358 | </p> |
| ... | ... | @@ -3390,7 +3390,7 @@ fn createFoo(param: i32) !Foo { |
| 3390 | 3390 | <ul> |
| 3391 | 3391 | <li>These primitives give enough expressiveness that it's completely practical |
| 3392 | 3392 | to have failing to check for an error be a compile error. If you really want |
| 3393 | | to ignore the error, you can add <code>catch unreachable</code> and |
| 3393 | to ignore the error, you can add {#syntax#}catch unreachable{#endsyntax#} and |
| 3394 | 3394 | get the added benefit of crashing in Debug and ReleaseSafe modes if your assumption was wrong. |
| 3395 | 3395 | </li> |
| 3396 | 3396 | <li> |
| ... | ... | @@ -3401,7 +3401,7 @@ fn createFoo(param: i32) !Foo { |
| 3401 | 3401 | </ul> |
| 3402 | 3402 | {#see_also|defer|if|switch#} |
| 3403 | 3403 | |
| 3404 | | <p>An error union is created with the <code>!</code> binary operator. |
| 3404 | <p>An error union is created with the {#syntax#}!{#endsyntax#} binary operator. |
| 3405 | 3405 | You can use compile-time reflection to access the child type of an error union:</p> |
| 3406 | 3406 | {#code_begin|test#} |
| 3407 | 3407 | const assert = @import("std").debug.assert; |
| ... | ... | @@ -3424,15 +3424,15 @@ test "error union" { |
| 3424 | 3424 | {#code_end#} |
| 3425 | 3425 | {#header_open|Merging Error Sets#} |
| 3426 | 3426 | <p> |
| 3427 | | Use the <code>||</code> operator to merge two error sets together. The resulting |
| 3427 | Use the {#syntax#}||{#endsyntax#} operator to merge two error sets together. The resulting |
| 3428 | 3428 | error set contains the errors of both error sets. Doc comments from the left-hand |
| 3429 | 3429 | side override doc comments from the right-hand side. In this example, the doc |
| 3430 | | comments for <code>C.PathNotFound</code> is <code>A doc comment</code>. |
| 3430 | comments for {#syntax#}C.PathNotFound{#endsyntax#} is <code>A doc comment</code>. |
| 3431 | 3431 | </p> |
| 3432 | 3432 | <p> |
| 3433 | 3433 | This is especially useful for functions which return different error sets depending |
| 3434 | 3434 | on {#link|comptime#} branches. For example, the Zig standard library uses |
| 3435 | | <code>LinuxFileOpenError || WindowsFileOpenError</code> for the error set of opening |
| 3435 | {#syntax#}LinuxFileOpenError || WindowsFileOpenError{#endsyntax#} for the error set of opening |
| 3436 | 3436 | files. |
| 3437 | 3437 | </p> |
| 3438 | 3438 | {#code_begin|test#} |
| ... | ... | @@ -3565,8 +3565,8 @@ fn bang2() !void { |
| 3565 | 3565 | Look closely at this example. This is no stack trace. |
| 3566 | 3566 | </p> |
| 3567 | 3567 | <p> |
| 3568 | | You can see that the final error bubbled up was <code>PermissionDenied</code>, |
| 3569 | | but the original error that started this whole thing was <code>FileNotFound</code>. In the <code>bar</code> function, the code handles the original error code, |
| 3568 | You can see that the final error bubbled up was {#syntax#}PermissionDenied{#endsyntax#}, |
| 3569 | but the original error that started this whole thing was {#syntax#}FileNotFound{#endsyntax#}. In the {#syntax#}bar{#endsyntax#} function, the code handles the original error code, |
| 3570 | 3570 | and then returns another one, from the switch statement. Error Return Traces make this clear, whereas a stack trace would look like this: |
| 3571 | 3571 | </p> |
| 3572 | 3572 | {#code_begin|exe_err#} |
| ... | ... | @@ -3612,7 +3612,7 @@ fn bang2() void { |
| 3612 | 3612 | {#code_end#} |
| 3613 | 3613 | <p> |
| 3614 | 3614 | Here, the stack trace does not explain how the control |
| 3615 | | flow in <code>bar</code> got to the <code>hello()</code> call. |
| 3615 | flow in {#syntax#}bar{#endsyntax#} got to the {#syntax#}hello(){#endsyntax#} call. |
| 3616 | 3616 | One would have to open a debugger or further instrument the application |
| 3617 | 3617 | in order to find out. The error return trace, on the other hand, |
| 3618 | 3618 | shows exactly how the error bubbled up. |
| ... | ... | @@ -3631,8 +3631,8 @@ fn bang2() void { |
| 3631 | 3631 | </p> |
| 3632 | 3632 | <ul> |
| 3633 | 3633 | <li>Return an error from main</li> |
| 3634 | | <li>An error makes its way to <code>catch unreachable</code> and you have not overridden the default panic handler</li> |
| 3635 | | <li>Use {#link|errorReturnTrace#} to access the current return trace. You can use <code>std.debug.dumpStackTrace</code> to print it. This function returns comptime-known {#link|null#} when building without error return tracing support.</li> |
| 3634 | <li>An error makes its way to {#syntax#}catch unreachable{#endsyntax#} and you have not overridden the default panic handler</li> |
| 3635 | <li>Use {#link|errorReturnTrace#} to access the current return trace. You can use {#syntax#}std.debug.dumpStackTrace{#endsyntax#} to print it. This function returns comptime-known {#link|null#} when building without error return tracing support.</li> |
| 3636 | 3636 | </ul> |
| 3637 | 3637 | {#header_open|Implementation Details#} |
| 3638 | 3638 | <p> |
| ... | ... | @@ -3643,7 +3643,7 @@ fn bang2() void { |
| 3643 | 3643 | <li>when returning errors</li> |
| 3644 | 3644 | </ul> |
| 3645 | 3645 | <p> |
| 3646 | | For the case when no errors are returned, the cost is a single memory write operation, only in the first non-failable function in the call graph that calls a failable function, i.e. when a function returning <code>void</code> calls a function returning <code>error</code>. |
| 3646 | For the case when no errors are returned, the cost is a single memory write operation, only in the first non-failable function in the call graph that calls a failable function, i.e. when a function returning {#syntax#}void{#endsyntax#} calls a function returning {#syntax#}error{#endsyntax#}. |
| 3647 | 3647 | This is to initialize this struct in the stack memory: |
| 3648 | 3648 | </p> |
| 3649 | 3649 | {#code_begin|syntax#} |
| ... | ... | @@ -3656,13 +3656,13 @@ pub const StackTrace = struct { |
| 3656 | 3656 | Here, N is the maximum function call depth as determined by call graph analysis. Recursion is ignored and counts for 2. |
| 3657 | 3657 | </p> |
| 3658 | 3658 | <p> |
| 3659 | | A pointer to <code>StackTrace</code> is passed as a secret parameter to every function that can return an error, but it's always the first parameter, so it can likely sit in a register and stay there. |
| 3659 | A pointer to {#syntax#}StackTrace{#endsyntax#} is passed as a secret parameter to every function that can return an error, but it's always the first parameter, so it can likely sit in a register and stay there. |
| 3660 | 3660 | </p> |
| 3661 | 3661 | <p> |
| 3662 | 3662 | That's it for the path when no errors occur. It's practically free in terms of performance. |
| 3663 | 3663 | </p> |
| 3664 | 3664 | <p> |
| 3665 | | When generating the code for a function that returns an error, just before the <code>return</code> statement (only for the <code>return</code> statements that return errors), Zig generates a call to this function: |
| 3665 | When generating the code for a function that returns an error, just before the {#syntax#}return{#endsyntax#} statement (only for the {#syntax#}return{#endsyntax#} statements that return errors), Zig generates a call to this function: |
| 3666 | 3666 | </p> |
| 3667 | 3667 | {#code_begin|syntax#} |
| 3668 | 3668 | // marked as "no-inline" in LLVM IR |
| ... | ... | @@ -3677,7 +3677,7 @@ fn __zig_return_error(stack_trace: *StackTrace) void { |
| 3677 | 3677 | <p> |
| 3678 | 3678 | As for code size cost, 1 function call before a return statement is no big deal. Even so, |
| 3679 | 3679 | I have <a href="https://github.com/ziglang/zig/issues/690">a plan</a> to make the call to |
| 3680 | | <code>__zig_return_error</code> a tail call, which brings the code size cost down to actually zero. What is a return statement in code without error return tracing can become a jump instruction in code with error return tracing. |
| 3680 | {#syntax#}__zig_return_error{#endsyntax#} a tail call, which brings the code size cost down to actually zero. What is a return statement in code without error return tracing can become a jump instruction in code with error return tracing. |
| 3681 | 3681 | </p> |
| 3682 | 3682 | {#header_close#} |
| 3683 | 3683 | {#header_close#} |
| ... | ... | @@ -3699,7 +3699,7 @@ const normal_int: i32 = 1234; |
| 3699 | 3699 | const optional_int: ?i32 = 5678; |
| 3700 | 3700 | {#code_end#} |
| 3701 | 3701 | <p> |
| 3702 | | Now the variable <code>optional_int</code> could be an <code>i32</code>, or <code>null</code>. |
| 3702 | Now the variable {#syntax#}optional_int{#endsyntax#} could be an {#syntax#}i32{#endsyntax#}, or {#syntax#}null{#endsyntax#}. |
| 3703 | 3703 | </p> |
| 3704 | 3704 | <p> |
| 3705 | 3705 | Instead of integers, let's talk about pointers. Null references are the source of many runtime |
| ... | ... | @@ -3740,8 +3740,8 @@ fn doAThing() ?*Foo { |
| 3740 | 3740 | {#code_end#} |
| 3741 | 3741 | <p> |
| 3742 | 3742 | Here, Zig is at least as convenient, if not more, than C. And, the type of "ptr" |
| 3743 | | is <code>*u8</code> <em>not</em> <code>?*u8</code>. The <code>orelse</code> keyword |
| 3744 | | unwrapped the optional type and therefore <code>ptr</code> is guaranteed to be non-null everywhere |
| 3743 | is {#syntax#}*u8{#endsyntax#} <em>not</em> {#syntax#}?*u8{#endsyntax#}. The {#syntax#}orelse{#endsyntax#} keyword |
| 3744 | unwrapped the optional type and therefore {#syntax#}ptr{#endsyntax#} is guaranteed to be non-null everywhere |
| 3745 | 3745 | it is used in the function. |
| 3746 | 3746 | </p> |
| 3747 | 3747 | <p> |
| ... | ... | @@ -3772,7 +3772,7 @@ fn doAThing(optional_foo: ?*Foo) void { |
| 3772 | 3772 | {#code_end#} |
| 3773 | 3773 | <p> |
| 3774 | 3774 | Once again, the notable thing here is that inside the if block, |
| 3775 | | <code>foo</code> is no longer an optional pointer, it is a pointer, which |
| 3775 | {#syntax#}foo{#endsyntax#} is no longer an optional pointer, it is a pointer, which |
| 3776 | 3776 | cannot be null. |
| 3777 | 3777 | </p> |
| 3778 | 3778 | <p> |
| ... | ... | @@ -3783,7 +3783,7 @@ fn doAThing(optional_foo: ?*Foo) void { |
| 3783 | 3783 | cannot be null. |
| 3784 | 3784 | </p> |
| 3785 | 3785 | {#header_open|Optional Type#} |
| 3786 | | <p>An optional is created by putting <code>?</code> in front of a type. You can use compile-time |
| 3786 | <p>An optional is created by putting {#syntax#}?{#endsyntax#} in front of a type. You can use compile-time |
| 3787 | 3787 | reflection to access the child type of an optional:</p> |
| 3788 | 3788 | {#code_begin|test#} |
| 3789 | 3789 | const assert = @import("std").debug.assert; |
| ... | ... | @@ -3802,7 +3802,7 @@ test "optional type" { |
| 3802 | 3802 | {#header_close#} |
| 3803 | 3803 | {#header_open|null#} |
| 3804 | 3804 | <p> |
| 3805 | | Just like {#link|undefined#}, <code>null</code> has its own type, and the only way to use it is to |
| 3805 | Just like {#link|undefined#}, {#syntax#}null{#endsyntax#} has its own type, and the only way to use it is to |
| 3806 | 3806 | cast it to a different type: |
| 3807 | 3807 | </p> |
| 3808 | 3808 | {#code_begin|syntax#} |
| ... | ... | @@ -3850,9 +3850,9 @@ test "implicit cast - invoke a type as a function" { |
| 3850 | 3850 | of the qualifiers, no matter how nested the qualifiers are: |
| 3851 | 3851 | </p> |
| 3852 | 3852 | <ul> |
| 3853 | | <li><code>const</code> - non-const to const is allowed</li> |
| 3854 | | <li><code>volatile</code> - non-volatile to volatile is allowed</li> |
| 3855 | | <li><code>align</code> - bigger to smaller alignment is allowed </li> |
| 3853 | <li>{#syntax#}const{#endsyntax#} - non-const to const is allowed</li> |
| 3854 | <li>{#syntax#}volatile{#endsyntax#} - non-volatile to volatile is allowed</li> |
| 3855 | <li>{#syntax#}align{#endsyntax#} - bigger to smaller alignment is allowed </li> |
| 3856 | 3856 | <li>{#link|error sets|Error Set Type#} to supersets is allowed</li> |
| 3857 | 3857 | </ul> |
| 3858 | 3858 | <p> |
| ... | ... | @@ -4100,7 +4100,7 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) error![]u8 { |
| 4100 | 4100 | |
| 4101 | 4101 | {#header_open|void#} |
| 4102 | 4102 | <p> |
| 4103 | | <code>void</code> represents a type that has no value. Code that makes use of void values is |
| 4103 | {#syntax#}void{#endsyntax#} represents a type that has no value. Code that makes use of void values is |
| 4104 | 4104 | not included in the final generated code: |
| 4105 | 4105 | </p> |
| 4106 | 4106 | {#code_begin|syntax#} |
| ... | ... | @@ -4110,7 +4110,7 @@ export fn entry() void { |
| 4110 | 4110 | x = y; |
| 4111 | 4111 | } |
| 4112 | 4112 | {#code_end#} |
| 4113 | | <p>When this turns into LLVM IR, there is no code generated in the body of <code>entry</code>, |
| 4113 | <p>When this turns into LLVM IR, there is no code generated in the body of {#syntax#}entry{#endsyntax#}, |
| 4114 | 4114 | even in debug mode. For example, on x86_64:</p> |
| 4115 | 4115 | <pre><code>0000000000000010 &lt;entry&gt;: |
| 4116 | 4116 | 10:	55 	push %rbp |
| ... | ... | @@ -4120,9 +4120,9 @@ export fn entry() void { |
| 4120 | 4120 | <p>These assembly instructions do not have any code associated with the void values - |
| 4121 | 4121 | they only perform the function call prologue and epilog.</p> |
| 4122 | 4122 | <p> |
| 4123 | | <code>void</code> can be useful for instantiating generic types. For example, given a |
| 4124 | | <code>Map(Key, Value)</code>, one can pass <code>void</code> for the <code>Value</code> |
| 4125 | | type to make it into a <code>Set</code>: |
| 4123 | {#syntax#}void{#endsyntax#} can be useful for instantiating generic types. For example, given a |
| 4124 | {#syntax#}Map(Key, Value){#endsyntax#}, one can pass {#syntax#}void{#endsyntax#} for the {#syntax#}Value{#endsyntax#} |
| 4125 | type to make it into a {#syntax#}Set{#endsyntax#}: |
| 4126 | 4126 | </p> |
| 4127 | 4127 | {#code_begin|test#} |
| 4128 | 4128 | const std = @import("std"); |
| ... | ... | @@ -4151,17 +4151,17 @@ fn eql_i32(a: i32, b: i32) bool { |
| 4151 | 4151 | } |
| 4152 | 4152 | {#code_end#} |
| 4153 | 4153 | <p>Note that this is different than using a dummy value for the hash map value. |
| 4154 | | By using <code>void</code> as the type of the value, the hash map entry type has no value field, and |
| 4154 | By using {#syntax#}void{#endsyntax#} as the type of the value, the hash map entry type has no value field, and |
| 4155 | 4155 | thus the hash map takes up less space. Further, all the code that deals with storing and loading the |
| 4156 | 4156 | value is deleted, as seen above. |
| 4157 | 4157 | </p> |
| 4158 | 4158 | <p> |
| 4159 | | <code>void</code> is distinct from <code>c_void</code>, which is defined like this: |
| 4160 | | <code>pub const c_void = @OpaqueType();</code>. |
| 4161 | | <code>void</code> has a known size of 0 bytes, and <code>c_void</code> has an unknown, but non-zero, size. |
| 4159 | {#syntax#}void{#endsyntax#} is distinct from {#syntax#}c_void{#endsyntax#}, which is defined like this: |
| 4160 | {#syntax#}pub const c_void = @OpaqueType();{#endsyntax#}. |
| 4161 | {#syntax#}void{#endsyntax#} has a known size of 0 bytes, and {#syntax#}c_void{#endsyntax#} has an unknown, but non-zero, size. |
| 4162 | 4162 | </p> |
| 4163 | 4163 | <p> |
| 4164 | | Expressions of type <code>void</code> are the only ones whose value can be ignored. For example: |
| 4164 | Expressions of type {#syntax#}void{#endsyntax#} are the only ones whose value can be ignored. For example: |
| 4165 | 4165 | </p> |
| 4166 | 4166 | {#code_begin|test_err|expression value is ignored#} |
| 4167 | 4167 | test "ignoring expression value" { |
| ... | ... | @@ -4172,7 +4172,7 @@ fn foo() i32 { |
| 4172 | 4172 | return 1234; |
| 4173 | 4173 | } |
| 4174 | 4174 | {#code_end#} |
| 4175 | | <p>However, if the expression has type <code>void</code>:</p> |
| 4175 | <p>However, if the expression has type {#syntax#}void{#endsyntax#}:</p> |
| 4176 | 4176 | {#code_begin|test#} |
| 4177 | 4177 | test "ignoring expression value" { |
| 4178 | 4178 | foo(); |
| ... | ... | @@ -4207,10 +4207,10 @@ fn gimmeTheBiggerInteger(a: u64, b: u64) u64 { |
| 4207 | 4207 | <p> |
| 4208 | 4208 | In Zig, types are first-class citizens. They can be assigned to variables, passed as parameters to functions, |
| 4209 | 4209 | and returned from functions. However, they can only be used in expressions which are known at <em>compile-time</em>, |
| 4210 | | which is why the parameter <code>T</code> in the above snippet must be marked with <code>comptime</code>. |
| 4210 | which is why the parameter {#syntax#}T{#endsyntax#} in the above snippet must be marked with {#syntax#}comptime{#endsyntax#}. |
| 4211 | 4211 | </p> |
| 4212 | 4212 | <p> |
| 4213 | | A <code>comptime</code> parameter means that: |
| 4213 | A {#syntax#}comptime{#endsyntax#} parameter means that: |
| 4214 | 4214 | </p> |
| 4215 | 4215 | <ul> |
| 4216 | 4216 | <li>At the callsite, the value must be known at compile-time, or it is a compile error.</li> |
| ... | ... | @@ -4255,7 +4255,7 @@ test "try to compare bools" { |
| 4255 | 4255 | } |
| 4256 | 4256 | {#code_end#} |
| 4257 | 4257 | <p> |
| 4258 | | On the flip side, inside the function definition with the <code>comptime</code> parameter, the |
| 4258 | On the flip side, inside the function definition with the {#syntax#}comptime{#endsyntax#} parameter, the |
| 4259 | 4259 | value is known at compile-time. This means that we actually could make this work for the bool type |
| 4260 | 4260 | if we wanted to: |
| 4261 | 4261 | </p> |
| ... | ... | @@ -4274,12 +4274,12 @@ test "try to compare bools" { |
| 4274 | 4274 | } |
| 4275 | 4275 | {#code_end#} |
| 4276 | 4276 | <p> |
| 4277 | | This works because Zig implicitly inlines <code>if</code> expressions when the condition |
| 4277 | This works because Zig implicitly inlines {#syntax#}if{#endsyntax#} expressions when the condition |
| 4278 | 4278 | is known at compile-time, and the compiler guarantees that it will skip analysis of |
| 4279 | 4279 | the branch not taken. |
| 4280 | 4280 | </p> |
| 4281 | 4281 | <p> |
| 4282 | | This means that the actual function generated for <code>max</code> in this situation looks like |
| 4282 | This means that the actual function generated for {#syntax#}max{#endsyntax#} in this situation looks like |
| 4283 | 4283 | this: |
| 4284 | 4284 | </p> |
| 4285 | 4285 | {#code_begin|syntax#} |
| ... | ... | @@ -4292,18 +4292,18 @@ fn max(a: bool, b: bool) bool { |
| 4292 | 4292 | the necessary run-time code to accomplish the task. |
| 4293 | 4293 | </p> |
| 4294 | 4294 | <p> |
| 4295 | | This works the same way for <code>switch</code> expressions - they are implicitly inlined |
| 4295 | This works the same way for {#syntax#}switch{#endsyntax#} expressions - they are implicitly inlined |
| 4296 | 4296 | when the target expression is compile-time known. |
| 4297 | 4297 | </p> |
| 4298 | 4298 | {#header_close#} |
| 4299 | 4299 | {#header_open|Compile-Time Variables#} |
| 4300 | 4300 | <p> |
| 4301 | | In Zig, the programmer can label variables as <code>comptime</code>. This guarantees to the compiler |
| 4301 | In Zig, the programmer can label variables as {#syntax#}comptime{#endsyntax#}. This guarantees to the compiler |
| 4302 | 4302 | that every load and store of the variable is performed at compile-time. Any violation of this results in a |
| 4303 | 4303 | compile error. |
| 4304 | 4304 | </p> |
| 4305 | 4305 | <p> |
| 4306 | | This combined with the fact that we can <code>inline</code> loops allows us to write |
| 4306 | This combined with the fact that we can {#syntax#}inline{#endsyntax#} loops allows us to write |
| 4307 | 4307 | a function which is partially evaluated at compile-time and partially at run-time. |
| 4308 | 4308 | </p> |
| 4309 | 4309 | <p> |
| ... | ... | @@ -4346,8 +4346,8 @@ test "perform fn" { |
| 4346 | 4346 | <p> |
| 4347 | 4347 | This example is a bit contrived, because the compile-time evaluation component is unnecessary; |
| 4348 | 4348 | this code would work fine if it was all done at run-time. But it does end up generating |
| 4349 | | different code. In this example, the function <code>performFn</code> is generated three different times, |
| 4350 | | for the different values of <code>prefix_char</code> provided: |
| 4349 | different code. In this example, the function {#syntax#}performFn{#endsyntax#} is generated three different times, |
| 4350 | for the different values of {#syntax#}prefix_char{#endsyntax#} provided: |
| 4351 | 4351 | </p> |
| 4352 | 4352 | {#code_begin|syntax#} |
| 4353 | 4353 | // From the line: |
| ... | ... | @@ -4388,7 +4388,7 @@ fn performFn(start_value: i32) i32 { |
| 4388 | 4388 | {#header_open|Compile-Time Expressions#} |
| 4389 | 4389 | <p> |
| 4390 | 4390 | In Zig, it matters whether a given expression is known at compile-time or run-time. A programmer can |
| 4391 | | use a <code>comptime</code> expression to guarantee that the expression will be evaluated at compile-time. |
| 4391 | use a {#syntax#}comptime{#endsyntax#} expression to guarantee that the expression will be evaluated at compile-time. |
| 4392 | 4392 | If this cannot be accomplished, the compiler will emit an error. For example: |
| 4393 | 4393 | </p> |
| 4394 | 4394 | {#code_begin|test_err|unable to evaluate constant expression#} |
| ... | ... | @@ -4401,16 +4401,16 @@ test "foo" { |
| 4401 | 4401 | } |
| 4402 | 4402 | {#code_end#} |
| 4403 | 4403 | <p> |
| 4404 | | It doesn't make sense that a program could call <code>exit()</code> (or any other external function) |
| 4405 | | at compile-time, so this is a compile error. However, a <code>comptime</code> expression does much |
| 4404 | It doesn't make sense that a program could call {#syntax#}exit(){#endsyntax#} (or any other external function) |
| 4405 | at compile-time, so this is a compile error. However, a {#syntax#}comptime{#endsyntax#} expression does much |
| 4406 | 4406 | more than sometimes cause a compile error. |
| 4407 | 4407 | </p> |
| 4408 | 4408 | <p> |
| 4409 | | Within a <code>comptime</code> expression: |
| 4409 | Within a {#syntax#}comptime{#endsyntax#} expression: |
| 4410 | 4410 | </p> |
| 4411 | 4411 | <ul> |
| 4412 | | <li>All variables are <code>comptime</code> variables.</li> |
| 4413 | | <li>All <code>if</code>, <code>while</code>, <code>for</code>, and <code>switch</code> |
| 4412 | <li>All variables are {#syntax#}comptime{#endsyntax#} variables.</li> |
| 4413 | <li>All {#syntax#}if{#endsyntax#}, {#syntax#}while{#endsyntax#}, {#syntax#}for{#endsyntax#}, and {#syntax#}switch{#endsyntax#} |
| 4414 | 4414 | expressions are evaluated at compile-time, or emit a compile error if this is not possible.</li> |
| 4415 | 4415 | <li>All function calls cause the compiler to interpret the function at compile-time, emitting a |
| 4416 | 4416 | compile error if the function tries to do something that has global run-time side effects.</li> |
| ... | ... | @@ -4487,7 +4487,7 @@ test "fibonacci" { |
| 4487 | 4487 | {#link|@setEvalBranchQuota#} to change the default number 1000 to something else. |
| 4488 | 4488 | </p> |
| 4489 | 4489 | <p> |
| 4490 | | What if we fix the base case, but put the wrong value in the <code>assert</code> line? |
| 4490 | What if we fix the base case, but put the wrong value in the {#syntax#}assert{#endsyntax#} line? |
| 4491 | 4491 | </p> |
| 4492 | 4492 | {#code_begin|test_err|encountered @panic at compile-time#} |
| 4493 | 4493 | const assert = @import("std").debug.assert; |
| ... | ... | @@ -4504,16 +4504,16 @@ test "fibonacci" { |
| 4504 | 4504 | } |
| 4505 | 4505 | {#code_end#} |
| 4506 | 4506 | <p> |
| 4507 | | What happened is Zig started interpreting the <code>assert</code> function with the |
| 4508 | | parameter <code>ok</code> set to <code>false</code>. When the interpreter hit |
| 4509 | | <code>unreachable</code> it emitted a compile error, because reaching unreachable |
| 4507 | What happened is Zig started interpreting the {#syntax#}assert{#endsyntax#} function with the |
| 4508 | parameter {#syntax#}ok{#endsyntax#} set to {#syntax#}false{#endsyntax#}. When the interpreter hit |
| 4509 | {#syntax#}unreachable{#endsyntax#} it emitted a compile error, because reaching unreachable |
| 4510 | 4510 | code is undefined behavior, and undefined behavior causes a compile error if it is detected |
| 4511 | 4511 | at compile-time. |
| 4512 | 4512 | </p> |
| 4513 | 4513 | |
| 4514 | 4514 | <p> |
| 4515 | 4515 | In the global scope (outside of any function), all expressions are implicitly |
| 4516 | | <code>comptime</code> expressions. This means that we can use functions to |
| 4516 | {#syntax#}comptime{#endsyntax#} expressions. This means that we can use functions to |
| 4517 | 4517 | initialize complex static data. For example: |
| 4518 | 4518 | </p> |
| 4519 | 4519 | {#code_begin|test#} |
| ... | ... | @@ -4561,7 +4561,7 @@ test "variable values" { |
| 4561 | 4561 | @1 = internal unnamed_addr constant i32 1060</code></pre> |
| 4562 | 4562 | <p> |
| 4563 | 4563 | Note that we did not have to do anything special with the syntax of these functions. For example, |
| 4564 | | we could call the <code>sum</code> function as is with a slice of numbers whose length and values were |
| 4564 | we could call the {#syntax#}sum{#endsyntax#} function as is with a slice of numbers whose length and values were |
| 4565 | 4565 | only known at run-time. |
| 4566 | 4566 | </p> |
| 4567 | 4567 | {#header_close#} |
| ... | ... | @@ -4573,8 +4573,8 @@ test "variable values" { |
| 4573 | 4573 | generic data structure. |
| 4574 | 4574 | </p> |
| 4575 | 4575 | <p> |
| 4576 | | Here is an example of a generic <code>List</code> data structure, that we will instantiate with |
| 4577 | | the type <code>i32</code>. In Zig we refer to the type as <code>List(i32)</code>. |
| 4576 | Here is an example of a generic {#syntax#}List{#endsyntax#} data structure, that we will instantiate with |
| 4577 | the type {#syntax#}i32{#endsyntax#}. In Zig we refer to the type as {#syntax#}List(i32){#endsyntax#}. |
| 4578 | 4578 | </p> |
| 4579 | 4579 | {#code_begin|syntax#} |
| 4580 | 4580 | fn List(comptime T: type) type { |
| ... | ... | @@ -4585,8 +4585,8 @@ fn List(comptime T: type) type { |
| 4585 | 4585 | } |
| 4586 | 4586 | {#code_end#} |
| 4587 | 4587 | <p> |
| 4588 | | That's it. It's a function that returns an anonymous <code>struct</code>. For the purposes of error messages |
| 4589 | | and debugging, Zig infers the name <code>"List(i32)"</code> from the function name and parameters invoked when creating |
| 4588 | That's it. It's a function that returns an anonymous {#syntax#}struct{#endsyntax#}. For the purposes of error messages |
| 4589 | and debugging, Zig infers the name {#syntax#}"List(i32)"{#endsyntax#} from the function name and parameters invoked when creating |
| 4590 | 4590 | the anonymous struct. |
| 4591 | 4591 | </p> |
| 4592 | 4592 | <p> |
| ... | ... | @@ -4602,13 +4602,13 @@ const Node = struct { |
| 4602 | 4602 | <p> |
| 4603 | 4603 | This works because all top level declarations are order-independent, and as long as there isn't |
| 4604 | 4604 | an actual infinite regression, values can refer to themselves, directly or indirectly. In this case, |
| 4605 | | <code>Node</code> refers to itself as a pointer, which is not actually an infinite regression, so |
| 4605 | {#syntax#}Node{#endsyntax#} refers to itself as a pointer, which is not actually an infinite regression, so |
| 4606 | 4606 | it works fine. |
| 4607 | 4607 | </p> |
| 4608 | 4608 | {#header_close#} |
| 4609 | 4609 | {#header_open|Case Study: printf in Zig#} |
| 4610 | 4610 | <p> |
| 4611 | | Putting all of this together, let's see how <code>printf</code> works in Zig. |
| 4611 | Putting all of this together, let's see how {#syntax#}printf{#endsyntax#} works in Zig. |
| 4612 | 4612 | </p> |
| 4613 | 4613 | {#code_begin|exe|printf#} |
| 4614 | 4614 | const warn = @import("std").debug.warn; |
| ... | ... | @@ -4709,7 +4709,7 @@ pub fn printf(self: *OutStream, arg0: i32, arg1: []const u8) !void { |
| 4709 | 4709 | } |
| 4710 | 4710 | {#code_end#} |
| 4711 | 4711 | <p> |
| 4712 | | <code>printValue</code> is a function that takes a parameter of any type, and does different things depending |
| 4712 | {#syntax#}printValue{#endsyntax#} is a function that takes a parameter of any type, and does different things depending |
| 4713 | 4713 | on the type: |
| 4714 | 4714 | </p> |
| 4715 | 4715 | {#code_begin|syntax#} |
| ... | ... | @@ -4725,7 +4725,7 @@ pub fn printValue(self: *OutStream, value: var) !void { |
| 4725 | 4725 | } |
| 4726 | 4726 | {#code_end#} |
| 4727 | 4727 | <p> |
| 4728 | | And now, what happens if we give too many arguments to <code>printf</code>? |
| 4728 | And now, what happens if we give too many arguments to {#syntax#}printf{#endsyntax#}? |
| 4729 | 4729 | </p> |
| 4730 | 4730 | {#code_begin|test_err|Unused arguments#} |
| 4731 | 4731 | const warn = @import("std").debug.warn; |
| ... | ... | @@ -4743,7 +4743,7 @@ test "printf too many arguments" { |
| 4743 | 4743 | </p> |
| 4744 | 4744 | <p> |
| 4745 | 4745 | Zig doesn't care whether the format argument is a string literal, |
| 4746 | | only that it is a compile-time known value that is implicitly castable to a <code>[]const u8</code>: |
| 4746 | only that it is a compile-time known value that is implicitly castable to a {#syntax#}[]const u8{#endsyntax#}: |
| 4747 | 4747 | </p> |
| 4748 | 4748 | {#code_begin|exe|printf#} |
| 4749 | 4749 | const warn = @import("std").debug.warn; |
| ... | ... | @@ -4797,16 +4797,16 @@ pub fn main() void { |
| 4797 | 4797 | </p> |
| 4798 | 4798 | {#header_open|Minimal Coroutine Example#} |
| 4799 | 4799 | <p> |
| 4800 | | Declare a coroutine with the <code>async</code> keyword. |
| 4800 | Declare a coroutine with the {#syntax#}async{#endsyntax#} keyword. |
| 4801 | 4801 | The expression in angle brackets must evaluate to a struct |
| 4802 | 4802 | which has these fields: |
| 4803 | 4803 | </p> |
| 4804 | 4804 | <ul> |
| 4805 | | <li><code>allocFn: fn (self: *Allocator, byte_count: usize, alignment: u29) Error![]u8</code> - where <code>Error</code> can be any error set.</li> |
| 4806 | | <li><code>freeFn: fn (self: *Allocator, old_mem: []u8) void</code></li> |
| 4805 | <li>{#syntax#}allocFn: fn (self: *Allocator, byte_count: usize, alignment: u29) Error![]u8{#endsyntax#} - where {#syntax#}Error{#endsyntax#} can be any error set.</li> |
| 4806 | <li>{#syntax#}freeFn: fn (self: *Allocator, old_mem: []u8) void{#endsyntax#}</li> |
| 4807 | 4807 | </ul> |
| 4808 | 4808 | <p> |
| 4809 | | You may notice that this corresponds to the <code>std.mem.Allocator</code> interface. |
| 4809 | You may notice that this corresponds to the {#syntax#}std.mem.Allocator{#endsyntax#} interface. |
| 4810 | 4810 | This makes it convenient to integrate with existing allocators. Note, however, |
| 4811 | 4811 | that the language feature does not depend on the standard library, and any struct which |
| 4812 | 4812 | has these fields is allowed. |
| ... | ... | @@ -4816,13 +4816,13 @@ pub fn main() void { |
| 4816 | 4816 | the function generic. Zig will infer the allocator type when the async function is called. |
| 4817 | 4817 | </p> |
| 4818 | 4818 | <p> |
| 4819 | | Call a coroutine with the <code>async</code> keyword. Here, the expression in angle brackets |
| 4819 | Call a coroutine with the {#syntax#}async{#endsyntax#} keyword. Here, the expression in angle brackets |
| 4820 | 4820 | is a pointer to the allocator struct that the coroutine expects. |
| 4821 | 4821 | </p> |
| 4822 | 4822 | <p> |
| 4823 | | The result of an async function call is a <code>promise->T</code> type, where <code>T</code> |
| 4823 | The result of an async function call is a {#syntax#}promise->T{#endsyntax#} type, where {#syntax#}T{#endsyntax#} |
| 4824 | 4824 | is the return type of the async function. Once a promise has been created, it must be |
| 4825 | | consumed, either with <code>cancel</code> or <code>await</code>: |
| 4825 | consumed, either with {#syntax#}cancel{#endsyntax#} or {#syntax#}await{#endsyntax#}: |
| 4826 | 4826 | </p> |
| 4827 | 4827 | <p> |
| 4828 | 4828 | Async functions start executing when created, so in the following example, the entire |
| ... | ... | @@ -4911,18 +4911,18 @@ async fn testSuspendBlock() void { |
| 4911 | 4911 | {#code_end#} |
| 4912 | 4912 | <p> |
| 4913 | 4913 | Every suspend point in an async function represents a point at which the coroutine |
| 4914 | | could be destroyed. If that happens, <code>defer</code> expressions that are in |
| 4915 | | scope are run, as well as <code>errdefer</code> expressions. |
| 4914 | could be destroyed. If that happens, {#syntax#}defer{#endsyntax#} expressions that are in |
| 4915 | scope are run, as well as {#syntax#}errdefer{#endsyntax#} expressions. |
| 4916 | 4916 | </p> |
| 4917 | 4917 | <p> |
| 4918 | 4918 | {#link|Await#} counts as a suspend point. |
| 4919 | 4919 | </p> |
| 4920 | 4920 | {#header_open|Resuming from Suspend Blocks#} |
| 4921 | 4921 | <p> |
| 4922 | | Upon entering a <code>suspend</code> block, the coroutine is already considered |
| 4922 | Upon entering a {#syntax#}suspend{#endsyntax#} block, the coroutine is already considered |
| 4923 | 4923 | suspended, and can be resumed. For example, if you started another kernel thread, |
| 4924 | | and had that thread call <code>resume</code> on the promise handle provided by the |
| 4925 | | <code>suspend</code> block, the new thread would begin executing after the suspend |
| 4924 | and had that thread call {#syntax#}resume{#endsyntax#} on the promise handle provided by the |
| 4925 | {#syntax#}suspend{#endsyntax#} block, the new thread would begin executing after the suspend |
| 4926 | 4926 | block, while the old thread continued executing the suspend block. |
| 4927 | 4927 | </p> |
| 4928 | 4928 | <p> |
| ... | ... | @@ -4957,26 +4957,26 @@ async fn testResumeFromSuspend(my_result: *i32) void { |
| 4957 | 4957 | {#header_close#} |
| 4958 | 4958 | {#header_open|Await#} |
| 4959 | 4959 | <p> |
| 4960 | | The <code>await</code> keyword is used to coordinate with an async function's |
| 4961 | | <code>return</code> statement. |
| 4960 | The {#syntax#}await{#endsyntax#} keyword is used to coordinate with an async function's |
| 4961 | {#syntax#}return{#endsyntax#} statement. |
| 4962 | 4962 | </p> |
| 4963 | 4963 | <p> |
| 4964 | | <code>await</code> is valid only in an <code>async</code> function, and it takes |
| 4964 | {#syntax#}await{#endsyntax#} is valid only in an {#syntax#}async{#endsyntax#} function, and it takes |
| 4965 | 4965 | as an operand a promise handle. |
| 4966 | 4966 | If the async function associated with the promise handle has already returned, |
| 4967 | | then <code>await</code> destroys the target async function, and gives the return value. |
| 4968 | | Otherwise, <code>await</code> suspends the current async function, registering its |
| 4967 | then {#syntax#}await{#endsyntax#} destroys the target async function, and gives the return value. |
| 4968 | Otherwise, {#syntax#}await{#endsyntax#} suspends the current async function, registering its |
| 4969 | 4969 | promise handle with the target coroutine. It becomes the target coroutine's responsibility |
| 4970 | 4970 | to have ensured that it will be resumed or destroyed. When the target coroutine reaches |
| 4971 | 4971 | its return statement, it gives the return value to the awaiter, destroys itself, and then |
| 4972 | 4972 | resumes the awaiter. |
| 4973 | 4973 | </p> |
| 4974 | 4974 | <p> |
| 4975 | | A promise handle must be consumed exactly once after it is created, either by <code>cancel</code> or <code>await</code>. |
| 4975 | A promise handle must be consumed exactly once after it is created, either by {#syntax#}cancel{#endsyntax#} or {#syntax#}await{#endsyntax#}. |
| 4976 | 4976 | </p> |
| 4977 | 4977 | <p> |
| 4978 | | <code>await</code> counts as a suspend point, and therefore at every <code>await</code>, |
| 4979 | | a coroutine can be potentially destroyed, which would run <code>defer</code> and <code>errdefer</code> expressions. |
| 4978 | {#syntax#}await{#endsyntax#} counts as a suspend point, and therefore at every {#syntax#}await{#endsyntax#}, |
| 4979 | a coroutine can be potentially destroyed, which would run {#syntax#}defer{#endsyntax#} and {#syntax#}errdefer{#endsyntax#} expressions. |
| 4980 | 4980 | </p> |
| 4981 | 4981 | {#code_begin|test#} |
| 4982 | 4982 | const std = @import("std"); |
| ... | ... | @@ -5020,9 +5020,9 @@ fn seq(c: u8) void { |
| 5020 | 5020 | } |
| 5021 | 5021 | {#code_end#} |
| 5022 | 5022 | <p> |
| 5023 | | In general, <code>suspend</code> is lower level than <code>await</code>. Most application |
| 5024 | | code will use only <code>async</code> and <code>await</code>, but event loop |
| 5025 | | implementations will make use of <code>suspend</code> internally. |
| 5023 | In general, {#syntax#}suspend{#endsyntax#} is lower level than {#syntax#}await{#endsyntax#}. Most application |
| 5024 | code will use only {#syntax#}async{#endsyntax#} and {#syntax#}await{#endsyntax#}, but event loop |
| 5025 | implementations will make use of {#syntax#}suspend{#endsyntax#} internally. |
| 5026 | 5026 | </p> |
| 5027 | 5027 | {#header_close#} |
| 5028 | 5028 | {#header_open|Open Issues#} |
| ... | ... | @@ -5052,36 +5052,36 @@ fn seq(c: u8) void { |
| 5052 | 5052 | {#header_open|Builtin Functions#} |
| 5053 | 5053 | <p> |
| 5054 | 5054 | Builtin functions are provided by the compiler and are prefixed with <code>@</code>. |
| 5055 | | The <code>comptime</code> keyword on a parameter means that the parameter must be known |
| 5055 | The {#syntax#}comptime{#endsyntax#} keyword on a parameter means that the parameter must be known |
| 5056 | 5056 | at compile time. |
| 5057 | 5057 | </p> |
| 5058 | 5058 | {#header_open|@addWithOverflow#} |
| 5059 | | <pre><code class="zig">@addWithOverflow(comptime T: type, a: T, b: T, result: *T) bool</code></pre> |
| 5059 | <pre>{#syntax#}@addWithOverflow(comptime T: type, a: T, b: T, result: *T) bool{#endsyntax#}</pre> |
| 5060 | 5060 | <p> |
| 5061 | | Performs <code>result.* = a + b</code>. If overflow or underflow occurs, |
| 5062 | | stores the overflowed bits in <code>result</code> and returns <code>true</code>. |
| 5063 | | If no overflow or underflow occurs, returns <code>false</code>. |
| 5061 | Performs {#syntax#}result.* = a + b{#endsyntax#}. If overflow or underflow occurs, |
| 5062 | stores the overflowed bits in {#syntax#}result{#endsyntax#} and returns {#syntax#}true{#endsyntax#}. |
| 5063 | If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}. |
| 5064 | 5064 | </p> |
| 5065 | 5065 | {#header_close#} |
| 5066 | 5066 | {#header_open|@ArgType#} |
| 5067 | | <pre><code class="zig">@ArgType(comptime T: type, comptime n: usize) type</code></pre> |
| 5067 | <pre>{#syntax#}@ArgType(comptime T: type, comptime n: usize) type{#endsyntax#}</pre> |
| 5068 | 5068 | <p> |
| 5069 | | This builtin function takes a function type and returns the type of the parameter at index <code>n</code>. |
| 5069 | This builtin function takes a function type and returns the type of the parameter at index {#syntax#}n{#endsyntax#}. |
| 5070 | 5070 | </p> |
| 5071 | 5071 | <p> |
| 5072 | | <code>T</code> must be a function type. |
| 5072 | {#syntax#}T{#endsyntax#} must be a function type. |
| 5073 | 5073 | </p> |
| 5074 | 5074 | <p> |
| 5075 | 5075 | Note: This function is deprecated. Use {#link|@typeInfo#} instead. |
| 5076 | 5076 | </p> |
| 5077 | 5077 | {#header_close#} |
| 5078 | 5078 | {#header_open|@atomicLoad#} |
| 5079 | | <pre><code class="zig">@atomicLoad(comptime T: type, ptr: *const T, comptime ordering: builtin.AtomicOrder) T</code></pre> |
| 5079 | <pre>{#syntax#}@atomicLoad(comptime T: type, ptr: *const T, comptime ordering: builtin.AtomicOrder) T{#endsyntax#}</pre> |
| 5080 | 5080 | <p> |
| 5081 | 5081 | This builtin function atomically dereferences a pointer and returns the value. |
| 5082 | 5082 | </p> |
| 5083 | 5083 | <p> |
| 5084 | | <code>T</code> must be a pointer type, a <code>bool</code>, |
| 5084 | {#syntax#}T{#endsyntax#} must be a pointer type, a {#syntax#}bool{#endsyntax#}, |
| 5085 | 5085 | or an integer whose bit count meets these requirements: |
| 5086 | 5086 | </p> |
| 5087 | 5087 | <ul> |
| ... | ... | @@ -5095,12 +5095,12 @@ fn seq(c: u8) void { |
| 5095 | 5095 | </p> |
| 5096 | 5096 | {#header_close#} |
| 5097 | 5097 | {#header_open|@atomicRmw#} |
| 5098 | | <pre><code class="zig">@atomicRmw(comptime T: type, ptr: *T, comptime op: builtin.AtomicRmwOp, operand: T, comptime ordering: builtin.AtomicOrder) T</code></pre> |
| 5098 | <pre>{#syntax#}@atomicRmw(comptime T: type, ptr: *T, comptime op: builtin.AtomicRmwOp, operand: T, comptime ordering: builtin.AtomicOrder) T{#endsyntax#}</pre> |
| 5099 | 5099 | <p> |
| 5100 | 5100 | This builtin function atomically modifies memory and then returns the previous value. |
| 5101 | 5101 | </p> |
| 5102 | 5102 | <p> |
| 5103 | | <code>T</code> must be a pointer type, a <code>bool</code>, |
| 5103 | {#syntax#}T{#endsyntax#} must be a pointer type, a {#syntax#}bool{#endsyntax#}, |
| 5104 | 5104 | or an integer whose bit count meets these requirements: |
| 5105 | 5105 | </p> |
| 5106 | 5106 | <ul> |
| ... | ... | @@ -5114,29 +5114,29 @@ fn seq(c: u8) void { |
| 5114 | 5114 | </p> |
| 5115 | 5115 | {#header_close#} |
| 5116 | 5116 | {#header_open|@bitCast#} |
| 5117 | | <pre><code class="zig">@bitCast(comptime DestType: type, value: var) DestType</code></pre> |
| 5117 | <pre>{#syntax#}@bitCast(comptime DestType: type, value: var) DestType{#endsyntax#}</pre> |
| 5118 | 5118 | <p> |
| 5119 | 5119 | Converts a value of one type to another type. |
| 5120 | 5120 | </p> |
| 5121 | 5121 | <p> |
| 5122 | | Asserts that <code>@sizeOf(@typeOf(value)) == @sizeOf(DestType)</code>. |
| 5122 | Asserts that {#syntax#}@sizeOf(@typeOf(value)) == @sizeOf(DestType){#endsyntax#}. |
| 5123 | 5123 | </p> |
| 5124 | 5124 | <p> |
| 5125 | | Asserts that <code>@typeId(DestType) != @import("builtin").TypeId.Pointer</code>. Use <code>@ptrCast</code> or <code>@intToPtr</code> if you need this. |
| 5125 | Asserts that {#syntax#}@typeId(DestType) != @import("builtin").TypeId.Pointer{#endsyntax#}. Use {#syntax#}@ptrCast{#endsyntax#} or {#syntax#}@intToPtr{#endsyntax#} if you need this. |
| 5126 | 5126 | </p> |
| 5127 | 5127 | <p> |
| 5128 | 5128 | Can be used for these things for example: |
| 5129 | 5129 | </p> |
| 5130 | 5130 | <ul> |
| 5131 | | <li>Convert <code>f32</code> to <code>u32</code> bits</li> |
| 5132 | | <li>Convert <code>i32</code> to <code>u32</code> preserving twos complement</li> |
| 5131 | <li>Convert {#syntax#}f32{#endsyntax#} to {#syntax#}u32{#endsyntax#} bits</li> |
| 5132 | <li>Convert {#syntax#}i32{#endsyntax#} to {#syntax#}u32{#endsyntax#} preserving twos complement</li> |
| 5133 | 5133 | </ul> |
| 5134 | 5134 | <p> |
| 5135 | | Works at compile-time if <code>value</code> is known at compile time. It's a compile error to bitcast a struct to a scalar type of the same size since structs have undefined layout. However if the struct is packed then it works. |
| 5135 | Works at compile-time if {#syntax#}value{#endsyntax#} is known at compile time. It's a compile error to bitcast a struct to a scalar type of the same size since structs have undefined layout. However if the struct is packed then it works. |
| 5136 | 5136 | </p> |
| 5137 | 5137 | {#header_close#} |
| 5138 | 5138 | {#header_open|@breakpoint#} |
| 5139 | | <pre><code class="zig">@breakpoint()</code></pre> |
| 5139 | <pre>{#syntax#}@breakpoint(){#endsyntax#}</pre> |
| 5140 | 5140 | <p> |
| 5141 | 5141 | This function inserts a platform-specific debug trap instruction which causes |
| 5142 | 5142 | debuggers to break there. |
| ... | ... | @@ -5147,10 +5147,10 @@ fn seq(c: u8) void { |
| 5147 | 5147 | |
| 5148 | 5148 | {#header_close#} |
| 5149 | 5149 | {#header_open|@alignCast#} |
| 5150 | | <pre><code class="zig">@alignCast(comptime alignment: u29, ptr: var) var</code></pre> |
| 5150 | <pre>{#syntax#}@alignCast(comptime alignment: u29, ptr: var) var{#endsyntax#}</pre> |
| 5151 | 5151 | <p> |
| 5152 | | <code>ptr</code> can be <code>*T</code>, <code>fn()</code>, <code>?*T</code>, |
| 5153 | | <code>?fn()</code>, or <code>[]T</code>. It returns the same type as <code>ptr</code> |
| 5152 | {#syntax#}ptr{#endsyntax#} can be {#syntax#}*T{#endsyntax#}, {#syntax#}fn(){#endsyntax#}, {#syntax#}?*T{#endsyntax#}, |
| 5153 | {#syntax#}?fn(){#endsyntax#}, or {#syntax#}[]T{#endsyntax#}. It returns the same type as {#syntax#}ptr{#endsyntax#} |
| 5154 | 5154 | except with the alignment adjusted to the new value. |
| 5155 | 5155 | </p> |
| 5156 | 5156 | <p>A {#link|pointer alignment safety check|Incorrect Pointer Alignment#} is added |
| ... | ... | @@ -5158,16 +5158,16 @@ fn seq(c: u8) void { |
| 5158 | 5158 | |
| 5159 | 5159 | {#header_close#} |
| 5160 | 5160 | {#header_open|@alignOf#} |
| 5161 | | <pre><code class="zig">@alignOf(comptime T: type) comptime_int</code></pre> |
| 5161 | <pre>{#syntax#}@alignOf(comptime T: type) comptime_int{#endsyntax#}</pre> |
| 5162 | 5162 | <p> |
| 5163 | 5163 | This function returns the number of bytes that this type should be aligned to |
| 5164 | 5164 | for the current target to match the C ABI. When the child type of a pointer has |
| 5165 | 5165 | this alignment, the alignment can be omitted from the type. |
| 5166 | 5166 | </p> |
| 5167 | | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 5167 | <pre>{#syntax#}const assert = @import("std").debug.assert; |
| 5168 | 5168 | comptime { |
| 5169 | 5169 | assert(*u32 == *align(@alignOf(u32)) u32); |
| 5170 | | }</code></pre> |
| 5170 | }{#endsyntax#}</pre> |
| 5171 | 5171 | <p> |
| 5172 | 5172 | The result is a target-specific compile time constant. It is guaranteed to be |
| 5173 | 5173 | less than or equal to {#link|@sizeOf(T)|@sizeOf#}. |
| ... | ... | @@ -5176,21 +5176,21 @@ comptime { |
| 5176 | 5176 | {#header_close#} |
| 5177 | 5177 | |
| 5178 | 5178 | {#header_open|@boolToInt#} |
| 5179 | | <pre><code class="zig">@boolToInt(value: bool) u1</code></pre> |
| 5179 | <pre>{#syntax#}@boolToInt(value: bool) u1{#endsyntax#}</pre> |
| 5180 | 5180 | <p> |
| 5181 | | Converts <code>true</code> to <code>u1(1)</code> and <code>false</code> to |
| 5182 | | <code>u1(0)</code>. |
| 5181 | Converts {#syntax#}true{#endsyntax#} to {#syntax#}u1(1){#endsyntax#} and {#syntax#}false{#endsyntax#} to |
| 5182 | {#syntax#}u1(0){#endsyntax#}. |
| 5183 | 5183 | </p> |
| 5184 | 5184 | <p> |
| 5185 | | If the value is known at compile-time, the return type is <code>comptime_int</code> |
| 5186 | | instead of <code>u1</code>. |
| 5185 | If the value is known at compile-time, the return type is {#syntax#}comptime_int{#endsyntax#} |
| 5186 | instead of {#syntax#}u1{#endsyntax#}. |
| 5187 | 5187 | </p> |
| 5188 | 5188 | {#header_close#} |
| 5189 | 5189 | |
| 5190 | 5190 | {#header_open|@bytesToSlice#} |
| 5191 | | <pre><code class="zig">@bytesToSlice(comptime Element: type, bytes: []u8) []Element</code></pre> |
| 5191 | <pre>{#syntax#}@bytesToSlice(comptime Element: type, bytes: []u8) []Element{#endsyntax#}</pre> |
| 5192 | 5192 | <p> |
| 5193 | | Converts a slice of bytes or array of bytes into a slice of <code>Element</code>. |
| 5193 | Converts a slice of bytes or array of bytes into a slice of {#syntax#}Element{#endsyntax#}. |
| 5194 | 5194 | The resulting slice has the same {#link|pointer|Pointers#} properties as the parameter. |
| 5195 | 5195 | </p> |
| 5196 | 5196 | <p> |
| ... | ... | @@ -5200,12 +5200,12 @@ comptime { |
| 5200 | 5200 | {#header_close#} |
| 5201 | 5201 | |
| 5202 | 5202 | {#header_open|@cDefine#} |
| 5203 | | <pre><code class="zig">@cDefine(comptime name: []u8, value)</code></pre> |
| 5203 | <pre>{#syntax#}@cDefine(comptime name: []u8, value){#endsyntax#}</pre> |
| 5204 | 5204 | <p> |
| 5205 | | This function can only occur inside <code>@cImport</code>. |
| 5205 | This function can only occur inside {#syntax#}@cImport{#endsyntax#}. |
| 5206 | 5206 | </p> |
| 5207 | 5207 | <p> |
| 5208 | | This appends <code>#define $name $value</code> to the <code>@cImport</code> |
| 5208 | This appends <code>#define $name $value</code> to the {#syntax#}@cImport{#endsyntax#} |
| 5209 | 5209 | temporary buffer. |
| 5210 | 5210 | </p> |
| 5211 | 5211 | <p> |
| ... | ... | @@ -5215,72 +5215,72 @@ comptime { |
| 5215 | 5215 | <p> |
| 5216 | 5216 | Use the void value, like this: |
| 5217 | 5217 | </p> |
| 5218 | | <pre><code class="zig">@cDefine("_GNU_SOURCE", {})</code></pre> |
| 5218 | <pre>{#syntax#}@cDefine("_GNU_SOURCE", {}){#endsyntax#}</pre> |
| 5219 | 5219 | {#see_also|Import from C Header File|@cInclude|@cImport|@cUndef|void#} |
| 5220 | 5220 | {#header_close#} |
| 5221 | 5221 | {#header_open|@cImport#} |
| 5222 | | <pre><code class="zig">@cImport(expression) (namespace)</code></pre> |
| 5222 | <pre>{#syntax#}@cImport(expression) (namespace){#endsyntax#}</pre> |
| 5223 | 5223 | <p> |
| 5224 | 5224 | This function parses C code and imports the functions, types, variables, and |
| 5225 | 5225 | compatible macro definitions into the result namespace. |
| 5226 | 5226 | </p> |
| 5227 | 5227 | <p> |
| 5228 | | <code>expression</code> is interpreted at compile time. The builtin functions |
| 5229 | | <code>@cInclude</code>, <code>@cDefine</code>, and <code>@cUndef</code> work |
| 5228 | {#syntax#}expression{#endsyntax#} is interpreted at compile time. The builtin functions |
| 5229 | {#syntax#}@cInclude{#endsyntax#}, {#syntax#}@cDefine{#endsyntax#}, and {#syntax#}@cUndef{#endsyntax#} work |
| 5230 | 5230 | within this expression, appending to a temporary buffer which is then parsed as C code. |
| 5231 | 5231 | </p> |
| 5232 | 5232 | <p> |
| 5233 | | Usually you should only have one <code>@cImport</code> in your entire application, because it saves the compiler |
| 5233 | Usually you should only have one {#syntax#}@cImport{#endsyntax#} in your entire application, because it saves the compiler |
| 5234 | 5234 | from invoking clang multiple times, and prevents inline functions from being duplicated. |
| 5235 | 5235 | </p> |
| 5236 | 5236 | <p> |
| 5237 | | Reasons for having multiple <code>@cImport</code> expressions would be: |
| 5237 | Reasons for having multiple {#syntax#}@cImport{#endsyntax#} expressions would be: |
| 5238 | 5238 | </p> |
| 5239 | 5239 | <ul> |
| 5240 | | <li>To avoid a symbol collision, for example if foo.h and bar.h both <code>#define CONNECTION_COUNT</code></li> |
| 5240 | <li>To avoid a symbol collision, for example if foo.h and bar.h both <code>#define CONNECTION_COUNT</code></li> |
| 5241 | 5241 | <li>To analyze the C code with different preprocessor defines</li> |
| 5242 | 5242 | </ul> |
| 5243 | 5243 | {#see_also|Import from C Header File|@cInclude|@cDefine|@cUndef#} |
| 5244 | 5244 | {#header_close#} |
| 5245 | 5245 | {#header_open|@cInclude#} |
| 5246 | | <pre><code class="zig">@cInclude(comptime path: []u8)</code></pre> |
| 5246 | <pre>{#syntax#}@cInclude(comptime path: []u8){#endsyntax#}</pre> |
| 5247 | 5247 | <p> |
| 5248 | | This function can only occur inside <code>@cImport</code>. |
| 5248 | This function can only occur inside {#syntax#}@cImport{#endsyntax#}. |
| 5249 | 5249 | </p> |
| 5250 | 5250 | <p> |
| 5251 | | This appends <code>#include <$path>\n</code> to the <code>c_import</code> |
| 5251 | This appends <code>#include <$path>\n</code> to the {#syntax#}c_import{#endsyntax#} |
| 5252 | 5252 | temporary buffer. |
| 5253 | 5253 | </p> |
| 5254 | 5254 | {#see_also|Import from C Header File|@cImport|@cDefine|@cUndef#} |
| 5255 | 5255 | {#header_close#} |
| 5256 | 5256 | {#header_open|@cUndef#} |
| 5257 | | <pre><code class="zig">@cUndef(comptime name: []u8)</code></pre> |
| 5257 | <pre>{#syntax#}@cUndef(comptime name: []u8){#endsyntax#}</pre> |
| 5258 | 5258 | <p> |
| 5259 | | This function can only occur inside <code>@cImport</code>. |
| 5259 | This function can only occur inside {#syntax#}@cImport{#endsyntax#}. |
| 5260 | 5260 | </p> |
| 5261 | 5261 | <p> |
| 5262 | | This appends <code>#undef $name</code> to the <code>@cImport</code> |
| 5262 | This appends <code>#undef $name</code> to the {#syntax#}@cImport{#endsyntax#} |
| 5263 | 5263 | temporary buffer. |
| 5264 | 5264 | </p> |
| 5265 | 5265 | {#see_also|Import from C Header File|@cImport|@cDefine|@cInclude#} |
| 5266 | 5266 | {#header_close#} |
| 5267 | 5267 | {#header_open|@clz#} |
| 5268 | | <pre><code class="zig">@clz(x: T) U</code></pre> |
| 5268 | <pre>{#syntax#}@clz(x: T) U{#endsyntax#}</pre> |
| 5269 | 5269 | <p> |
| 5270 | | This function counts the number of leading zeroes in <code>x</code> which is an integer |
| 5271 | | type <code>T</code>. |
| 5270 | This function counts the number of leading zeroes in {#syntax#}x{#endsyntax#} which is an integer |
| 5271 | type {#syntax#}T{#endsyntax#}. |
| 5272 | 5272 | </p> |
| 5273 | 5273 | <p> |
| 5274 | | The return type <code>U</code> is an unsigned integer with the minimum number |
| 5275 | | of bits that can represent the value <code>T.bit_count</code>. |
| 5274 | The return type {#syntax#}U{#endsyntax#} is an unsigned integer with the minimum number |
| 5275 | of bits that can represent the value {#syntax#}T.bit_count{#endsyntax#}. |
| 5276 | 5276 | </p> |
| 5277 | 5277 | <p> |
| 5278 | | If <code>x</code> is zero, <code>@clz</code> returns <code>T.bit_count</code>. |
| 5278 | If {#syntax#}x{#endsyntax#} is zero, {#syntax#}@clz{#endsyntax#} returns {#syntax#}T.bit_count{#endsyntax#}. |
| 5279 | 5279 | </p> |
| 5280 | 5280 | {#see_also|@ctz|@popCount#} |
| 5281 | 5281 | {#header_close#} |
| 5282 | 5282 | {#header_open|@cmpxchgStrong#} |
| 5283 | | <pre><code class="zig">@cmpxchgStrong(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T</code></pre> |
| 5283 | <pre>{#syntax#}@cmpxchgStrong(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T{#endsyntax#}</pre> |
| 5284 | 5284 | <p> |
| 5285 | 5285 | This function performs a strong atomic compare exchange operation. It's the equivalent of this code, |
| 5286 | 5286 | except atomic: |
| ... | ... | @@ -5301,13 +5301,13 @@ fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_v |
| 5301 | 5301 | more efficiently in machine instructions. |
| 5302 | 5302 | </p> |
| 5303 | 5303 | <p> |
| 5304 | | <code>AtomicOrder</code> can be found with <code>@import("builtin").AtomicOrder</code>. |
| 5304 | {#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("builtin").AtomicOrder{#endsyntax#}. |
| 5305 | 5305 | </p> |
| 5306 | | <p><code>@typeOf(ptr).alignment</code> must be <code>&gt;= @sizeOf(T).</code></p> |
| 5306 | <p>{#syntax#}@typeOf(ptr).alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}</p> |
| 5307 | 5307 | {#see_also|Compile Variables|cmpxchgWeak#} |
| 5308 | 5308 | {#header_close#} |
| 5309 | 5309 | {#header_open|@cmpxchgWeak#} |
| 5310 | | <pre><code class="zig">@cmpxchgWeak(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T</code></pre> |
| 5310 | <pre>{#syntax#}@cmpxchgWeak(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T{#endsyntax#}</pre> |
| 5311 | 5311 | <p> |
| 5312 | 5312 | This function performs a weak atomic compare exchange operation. It's the equivalent of this code, |
| 5313 | 5313 | except atomic: |
| ... | ... | @@ -5324,30 +5324,30 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val |
| 5324 | 5324 | } |
| 5325 | 5325 | {#code_end#} |
| 5326 | 5326 | <p> |
| 5327 | | If you are using cmpxchg in a loop, the sporadic failure will be no problem, and <code>cmpxchgWeak</code> |
| 5327 | If you are using cmpxchg in a loop, the sporadic failure will be no problem, and {#syntax#}cmpxchgWeak{#endsyntax#} |
| 5328 | 5328 | is the better choice, because it can be implemented more efficiently in machine instructions. |
| 5329 | 5329 | However if you need a stronger guarantee, use {#link|@cmpxchgStrong#}. |
| 5330 | 5330 | </p> |
| 5331 | 5331 | <p> |
| 5332 | | <code>AtomicOrder</code> can be found with <code>@import("builtin").AtomicOrder</code>. |
| 5332 | {#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("builtin").AtomicOrder{#endsyntax#}. |
| 5333 | 5333 | </p> |
| 5334 | | <p><code>@typeOf(ptr).alignment</code> must be <code>&gt;= @sizeOf(T).</code></p> |
| 5334 | <p>{#syntax#}@typeOf(ptr).alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}</p> |
| 5335 | 5335 | {#see_also|Compile Variables|cmpxchgStrong#} |
| 5336 | 5336 | {#header_close#} |
| 5337 | 5337 | {#header_open|@compileError#} |
| 5338 | | <pre><code class="zig">@compileError(comptime msg: []u8)</code></pre> |
| 5338 | <pre>{#syntax#}@compileError(comptime msg: []u8){#endsyntax#}</pre> |
| 5339 | 5339 | <p> |
| 5340 | 5340 | This function, when semantically analyzed, causes a compile error with the |
| 5341 | | message <code>msg</code>. |
| 5341 | message {#syntax#}msg{#endsyntax#}. |
| 5342 | 5342 | </p> |
| 5343 | 5343 | <p> |
| 5344 | 5344 | There are several ways that code avoids being semantically checked, such as |
| 5345 | | using <code>if</code> or <code>switch</code> with compile time constants, |
| 5346 | | and <code>comptime</code> functions. |
| 5345 | using {#syntax#}if{#endsyntax#} or {#syntax#}switch{#endsyntax#} with compile time constants, |
| 5346 | and {#syntax#}comptime{#endsyntax#} functions. |
| 5347 | 5347 | </p> |
| 5348 | 5348 | {#header_close#} |
| 5349 | 5349 | {#header_open|@compileLog#} |
| 5350 | | <pre><code class="zig">@compileLog(args: ...)</code></pre> |
| 5350 | <pre>{#syntax#}@compileLog(args: ...){#endsyntax#}</pre> |
| 5351 | 5351 | <p> |
| 5352 | 5352 | This function prints the arguments passed to it at compile-time. |
| 5353 | 5353 | </p> |
| ... | ... | @@ -5382,7 +5382,7 @@ test "main" { |
| 5382 | 5382 | will ouput: |
| 5383 | 5383 | </p> |
| 5384 | 5384 | <p> |
| 5385 | | If all <code>@compileLog</code> calls are removed or |
| 5385 | If all {#syntax#}@compileLog{#endsyntax#} calls are removed or |
| 5386 | 5386 | not encountered by analysis, the |
| 5387 | 5387 | program compiles successfully and the generated executable prints: |
| 5388 | 5388 | </p> |
| ... | ... | @@ -5401,88 +5401,88 @@ test "main" { |
| 5401 | 5401 | {#code_end#} |
| 5402 | 5402 | {#header_close#} |
| 5403 | 5403 | {#header_open|@ctz#} |
| 5404 | | <pre><code class="zig">@ctz(x: T) U</code></pre> |
| 5404 | <pre>{#syntax#}@ctz(x: T) U{#endsyntax#}</pre> |
| 5405 | 5405 | <p> |
| 5406 | | This function counts the number of trailing zeroes in <code>x</code> which is an integer |
| 5407 | | type <code>T</code>. |
| 5406 | This function counts the number of trailing zeroes in {#syntax#}x{#endsyntax#} which is an integer |
| 5407 | type {#syntax#}T{#endsyntax#}. |
| 5408 | 5408 | </p> |
| 5409 | 5409 | <p> |
| 5410 | | The return type <code>U</code> is an unsigned integer with the minimum number |
| 5411 | | of bits that can represent the value <code>T.bit_count</code>. |
| 5410 | The return type {#syntax#}U{#endsyntax#} is an unsigned integer with the minimum number |
| 5411 | of bits that can represent the value {#syntax#}T.bit_count{#endsyntax#}. |
| 5412 | 5412 | </p> |
| 5413 | 5413 | <p> |
| 5414 | | If <code>x</code> is zero, <code>@ctz</code> returns <code>T.bit_count</code>. |
| 5414 | If {#syntax#}x{#endsyntax#} is zero, {#syntax#}@ctz{#endsyntax#} returns {#syntax#}T.bit_count{#endsyntax#}. |
| 5415 | 5415 | </p> |
| 5416 | 5416 | {#see_also|@clz|@popCount#} |
| 5417 | 5417 | {#header_close#} |
| 5418 | 5418 | {#header_open|@divExact#} |
| 5419 | | <pre><code class="zig">@divExact(numerator: T, denominator: T) T</code></pre> |
| 5419 | <pre>{#syntax#}@divExact(numerator: T, denominator: T) T{#endsyntax#}</pre> |
| 5420 | 5420 | <p> |
| 5421 | | Exact division. Caller guarantees <code>denominator != 0</code> and |
| 5422 | | <code>@divTrunc(numerator, denominator) * denominator == numerator</code>. |
| 5421 | Exact division. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and |
| 5422 | {#syntax#}@divTrunc(numerator, denominator) * denominator == numerator{#endsyntax#}. |
| 5423 | 5423 | </p> |
| 5424 | 5424 | <ul> |
| 5425 | | <li><code>@divExact(6, 3) == 2</code></li> |
| 5426 | | <li><code>@divExact(a, b) * b == a</code></li> |
| 5425 | <li>{#syntax#}@divExact(6, 3) == 2{#endsyntax#}</li> |
| 5426 | <li>{#syntax#}@divExact(a, b) * b == a{#endsyntax#}</li> |
| 5427 | 5427 | </ul> |
| 5428 | | <p>For a function that returns a possible error code, use <code>@import("std").math.divExact</code>.</p> |
| 5428 | <p>For a function that returns a possible error code, use {#syntax#}@import("std").math.divExact{#endsyntax#}.</p> |
| 5429 | 5429 | {#see_also|@divTrunc|@divFloor#} |
| 5430 | 5430 | {#header_close#} |
| 5431 | 5431 | {#header_open|@divFloor#} |
| 5432 | | <pre><code class="zig">@divFloor(numerator: T, denominator: T) T</code></pre> |
| 5432 | <pre>{#syntax#}@divFloor(numerator: T, denominator: T) T{#endsyntax#}</pre> |
| 5433 | 5433 | <p> |
| 5434 | 5434 | Floored division. Rounds toward negative infinity. For unsigned integers it is |
| 5435 | | the same as <code>numerator / denominator</code>. Caller guarantees <code>denominator != 0</code> and |
| 5436 | | <code>!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)</code>. |
| 5435 | the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and |
| 5436 | {#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1){#endsyntax#}. |
| 5437 | 5437 | </p> |
| 5438 | 5438 | <ul> |
| 5439 | | <li><code>@divFloor(-5, 3) == -2</code></li> |
| 5440 | | <li><code>@divFloor(a, b) + @mod(a, b) == a</code></li> |
| 5439 | <li>{#syntax#}@divFloor(-5, 3) == -2{#endsyntax#}</li> |
| 5440 | <li>{#syntax#}@divFloor(a, b) + @mod(a, b) == a{#endsyntax#}</li> |
| 5441 | 5441 | </ul> |
| 5442 | | <p>For a function that returns a possible error code, use <code>@import("std").math.divFloor</code>.</p> |
| 5442 | <p>For a function that returns a possible error code, use {#syntax#}@import("std").math.divFloor{#endsyntax#}.</p> |
| 5443 | 5443 | {#see_also|@divTrunc|@divExact#} |
| 5444 | 5444 | {#header_close#} |
| 5445 | 5445 | {#header_open|@divTrunc#} |
| 5446 | | <pre><code class="zig">@divTrunc(numerator: T, denominator: T) T</code></pre> |
| 5446 | <pre>{#syntax#}@divTrunc(numerator: T, denominator: T) T{#endsyntax#}</pre> |
| 5447 | 5447 | <p> |
| 5448 | 5448 | Truncated division. Rounds toward zero. For unsigned integers it is |
| 5449 | | the same as <code>numerator / denominator</code>. Caller guarantees <code>denominator != 0</code> and |
| 5450 | | <code>!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)</code>. |
| 5449 | the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and |
| 5450 | {#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1){#endsyntax#}. |
| 5451 | 5451 | </p> |
| 5452 | 5452 | <ul> |
| 5453 | | <li><code>@divTrunc(-5, 3) == -1</code></li> |
| 5454 | | <li><code>@divTrunc(a, b) + @rem(a, b) == a</code></li> |
| 5453 | <li>{#syntax#}@divTrunc(-5, 3) == -1{#endsyntax#}</li> |
| 5454 | <li>{#syntax#}@divTrunc(a, b) + @rem(a, b) == a{#endsyntax#}</li> |
| 5455 | 5455 | </ul> |
| 5456 | | <p>For a function that returns a possible error code, use <code>@import("std").math.divTrunc</code>.</p> |
| 5456 | <p>For a function that returns a possible error code, use {#syntax#}@import("std").math.divTrunc{#endsyntax#}.</p> |
| 5457 | 5457 | {#see_also|@divFloor|@divExact#} |
| 5458 | 5458 | {#header_close#} |
| 5459 | 5459 | {#header_open|@embedFile#} |
| 5460 | | <pre><code class="zig">@embedFile(comptime path: []const u8) [X]u8</code></pre> |
| 5460 | <pre>{#syntax#}@embedFile(comptime path: []const u8) [X]u8{#endsyntax#}</pre> |
| 5461 | 5461 | <p> |
| 5462 | 5462 | This function returns a compile time constant fixed-size array with length |
| 5463 | | equal to the byte count of the file given by <code>path</code>. The contents of the array |
| 5463 | equal to the byte count of the file given by {#syntax#}path{#endsyntax#}. The contents of the array |
| 5464 | 5464 | are the contents of the file. |
| 5465 | 5465 | </p> |
| 5466 | 5466 | <p> |
| 5467 | | <code>path</code> is absolute or relative to the current file, just like <code>@import</code>. |
| 5467 | {#syntax#}path{#endsyntax#} is absolute or relative to the current file, just like {#syntax#}@import{#endsyntax#}. |
| 5468 | 5468 | </p> |
| 5469 | 5469 | {#see_also|@import#} |
| 5470 | 5470 | {#header_close#} |
| 5471 | 5471 | |
| 5472 | 5472 | {#header_open|@enumToInt#} |
| 5473 | | <pre><code class="zig">@enumToInt(enum_value: var) var</code></pre> |
| 5473 | <pre>{#syntax#}@enumToInt(enum_value: var) var{#endsyntax#}</pre> |
| 5474 | 5474 | <p> |
| 5475 | 5475 | Converts an enumeration value into its integer tag type. |
| 5476 | 5476 | </p> |
| 5477 | 5477 | <p> |
| 5478 | | If the enum has only 1 possible value, the resut is a <code class="zig">comptime_int</code> |
| 5478 | If the enum has only 1 possible value, the resut is a {#syntax#}comptime_int{#endsyntax#} |
| 5479 | 5479 | known at {#link|comptime#}. |
| 5480 | 5480 | </p> |
| 5481 | 5481 | {#see_also|@intToEnum#} |
| 5482 | 5482 | {#header_close#} |
| 5483 | 5483 | |
| 5484 | 5484 | {#header_open|@errSetCast#} |
| 5485 | | <pre><code class="zig">@errSetCast(comptime T: DestType, value: var) DestType</code></pre> |
| 5485 | <pre>{#syntax#}@errSetCast(comptime T: DestType, value: var) DestType{#endsyntax#}</pre> |
| 5486 | 5486 | <p> |
| 5487 | 5487 | Converts an error value from one error set to another error set. Attempting to convert an error |
| 5488 | 5488 | which is not in the destination error set results in safety-protected {#link|Undefined Behavior#}. |
| ... | ... | @@ -5490,24 +5490,24 @@ test "main" { |
| 5490 | 5490 | {#header_close#} |
| 5491 | 5491 | |
| 5492 | 5492 | {#header_open|@errorName#} |
| 5493 | | <pre><code class="zig">@errorName(err: error) []u8</code></pre> |
| 5493 | <pre>{#syntax#}@errorName(err: error) []u8{#endsyntax#}</pre> |
| 5494 | 5494 | <p> |
| 5495 | 5495 | This function returns the string representation of an error. If an error |
| 5496 | 5496 | declaration is: |
| 5497 | 5497 | </p> |
| 5498 | | <pre><code class="zig">error OutOfMem</code></pre> |
| 5498 | <pre>{#syntax#}error OutOfMem{#endsyntax#}</pre> |
| 5499 | 5499 | <p> |
| 5500 | | Then the string representation is <code>"OutOfMem"</code>. |
| 5500 | Then the string representation is {#syntax#}"OutOfMem"{#endsyntax#}. |
| 5501 | 5501 | </p> |
| 5502 | 5502 | <p> |
| 5503 | | If there are no calls to <code>@errorName</code> in an entire application, |
| 5504 | | or all calls have a compile-time known value for <code>err</code>, then no |
| 5503 | If there are no calls to {#syntax#}@errorName{#endsyntax#} in an entire application, |
| 5504 | or all calls have a compile-time known value for {#syntax#}err{#endsyntax#}, then no |
| 5505 | 5505 | error name table will be generated. |
| 5506 | 5506 | </p> |
| 5507 | 5507 | {#header_close#} |
| 5508 | 5508 | |
| 5509 | 5509 | {#header_open|@errorReturnTrace#} |
| 5510 | | <pre><code class="zig">@errorReturnTrace() ?*builtin.StackTrace</code></pre> |
| 5510 | <pre>{#syntax#}@errorReturnTrace() ?*builtin.StackTrace{#endsyntax#}</pre> |
| 5511 | 5511 | <p> |
| 5512 | 5512 | If the binary is built with error return tracing, and this function is invoked in a |
| 5513 | 5513 | function that calls a function with an error or error union return type, returns a |
| ... | ... | @@ -5516,13 +5516,13 @@ test "main" { |
| 5516 | 5516 | {#header_close#} |
| 5517 | 5517 | |
| 5518 | 5518 | {#header_open|@errorToInt#} |
| 5519 | | <pre><code class="zig">@errorToInt(err: var) @IntType(false, @sizeOf(error) * 8)</code></pre> |
| 5519 | <pre>{#syntax#}@errorToInt(err: var) @IntType(false, @sizeOf(error) * 8){#endsyntax#}</pre> |
| 5520 | 5520 | <p> |
| 5521 | 5521 | Supports the following types: |
| 5522 | 5522 | </p> |
| 5523 | 5523 | <ul> |
| 5524 | 5524 | <li>error unions</li> |
| 5525 | | <li><code>E!void</code></li> |
| 5525 | <li>{#syntax#}E!void{#endsyntax#}</li> |
| 5526 | 5526 | </ul> |
| 5527 | 5527 | <p> |
| 5528 | 5528 | Converts an error to the integer representation of an error. |
| ... | ... | @@ -5535,38 +5535,41 @@ test "main" { |
| 5535 | 5535 | {#header_close#} |
| 5536 | 5536 | |
| 5537 | 5537 | {#header_open|@export#} |
| 5538 | | <pre><code class="zig">@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) []const u8</code></pre> |
| 5538 | <pre>{#syntax#}@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) []const u8{#endsyntax#}</pre> |
| 5539 | 5539 | <p> |
| 5540 | 5540 | Creates a symbol in the output object file. |
| 5541 | 5541 | </p> |
| 5542 | 5542 | {#header_close#} |
| 5543 | 5543 | |
| 5544 | 5544 | {#header_open|@fence#} |
| 5545 | | <pre><code class="zig">@fence(order: AtomicOrder)</code></pre> |
| 5545 | <pre>{#syntax#}@fence(order: AtomicOrder){#endsyntax#}</pre> |
| 5546 | 5546 | <p> |
| 5547 | | The <code>fence</code> function is used to introduce happens-before edges between operations. |
| 5547 | The {#syntax#}fence{#endsyntax#} function is used to introduce happens-before edges between operations. |
| 5548 | 5548 | </p> |
| 5549 | 5549 | <p> |
| 5550 | | <code>AtomicOrder</code> can be found with <code>@import("builtin").AtomicOrder</code>. |
| 5550 | {#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("builtin").AtomicOrder{#endsyntax#}. |
| 5551 | 5551 | </p> |
| 5552 | 5552 | {#see_also|Compile Variables#} |
| 5553 | 5553 | {#header_close#} |
| 5554 | 5554 | |
| 5555 | 5555 | {#header_open|@field#} |
| 5556 | | <pre><code class="zig">@field(lhs: var, comptime field_name: []const u8) (field)</code></pre> |
| 5557 | | <p>Preforms field access equivalent to <code>lhs.-&gtfield_name-&lt</code>.</p> |
| 5556 | <pre>{#syntax#}@field(lhs: var, comptime field_name: []const u8) (field){#endsyntax#}</pre> |
| 5557 | <p>Preforms field access equivalent to {#syntax#}lhs.field_name{#endsyntax#}, except instead |
| 5558 | of the field {#syntax#}"field_name"{#endsyntax#}, it accesses the field named by the string |
| 5559 | value of {#syntax#}field_name{#endsyntax#}. |
| 5560 | </p> |
| 5558 | 5561 | {#header_close#} |
| 5559 | 5562 | |
| 5560 | 5563 | {#header_open|@fieldParentPtr#} |
| 5561 | | <pre><code class="zig">@fieldParentPtr(comptime ParentType: type, comptime field_name: []const u8, |
| 5562 | | field_ptr: *T) *ParentType</code></pre> |
| 5564 | <pre>{#syntax#}@fieldParentPtr(comptime ParentType: type, comptime field_name: []const u8, |
| 5565 | field_ptr: *T) *ParentType{#endsyntax#}</pre> |
| 5563 | 5566 | <p> |
| 5564 | 5567 | Given a pointer to a field, returns the base pointer of a struct. |
| 5565 | 5568 | </p> |
| 5566 | 5569 | {#header_close#} |
| 5567 | 5570 | |
| 5568 | 5571 | {#header_open|@floatCast#} |
| 5569 | | <pre><code class="zig">@floatCast(comptime DestType: type, value: var) DestType</code></pre> |
| 5572 | <pre>{#syntax#}@floatCast(comptime DestType: type, value: var) DestType{#endsyntax#}</pre> |
| 5570 | 5573 | <p> |
| 5571 | 5574 | Convert from one float type to another. This cast is safe, but may cause the |
| 5572 | 5575 | numeric value to lose precision. |
| ... | ... | @@ -5574,7 +5577,7 @@ test "main" { |
| 5574 | 5577 | {#header_close#} |
| 5575 | 5578 | |
| 5576 | 5579 | {#header_open|@floatToInt#} |
| 5577 | | <pre><code class="zig">@floatToInt(comptime DestType: type, float: var) DestType</code></pre> |
| 5580 | <pre>{#syntax#}@floatToInt(comptime DestType: type, float: var) DestType{#endsyntax#}</pre> |
| 5578 | 5581 | <p> |
| 5579 | 5582 | Converts the integer part of a floating point number to the destination type. |
| 5580 | 5583 | </p> |
| ... | ... | @@ -5586,7 +5589,7 @@ test "main" { |
| 5586 | 5589 | {#header_close#} |
| 5587 | 5590 | |
| 5588 | 5591 | {#header_open|@frameAddress#} |
| 5589 | | <pre><code class="zig">@frameAddress()</code></pre> |
| 5592 | <pre>{#syntax#}@frameAddress(){#endsyntax#}</pre> |
| 5590 | 5593 | <p> |
| 5591 | 5594 | This function returns the base pointer of the current stack frame. |
| 5592 | 5595 | </p> |
| ... | ... | @@ -5600,9 +5603,9 @@ test "main" { |
| 5600 | 5603 | </p> |
| 5601 | 5604 | {#header_close#} |
| 5602 | 5605 | {#header_open|@handle#} |
| 5603 | | <pre><code class="zig">@handle()</code></pre> |
| 5606 | <pre>{#syntax#}@handle(){#endsyntax#}</pre> |
| 5604 | 5607 | <p> |
| 5605 | | This function returns a <code>promise->T</code> type, where <code>T</code> |
| 5608 | This function returns a {#syntax#}promise->T{#endsyntax#} type, where {#syntax#}T{#endsyntax#} |
| 5606 | 5609 | is the return type of the async function in scope. |
| 5607 | 5610 | </p> |
| 5608 | 5611 | <p> |
| ... | ... | @@ -5610,27 +5613,27 @@ test "main" { |
| 5610 | 5613 | </p> |
| 5611 | 5614 | {#header_close#} |
| 5612 | 5615 | {#header_open|@import#} |
| 5613 | | <pre><code class="zig">@import(comptime path: []u8) (namespace)</code></pre> |
| 5616 | <pre>{#syntax#}@import(comptime path: []u8) (namespace){#endsyntax#}</pre> |
| 5614 | 5617 | <p> |
| 5615 | | This function finds a zig file corresponding to <code>path</code> and imports all the |
| 5618 | This function finds a zig file corresponding to {#syntax#}path{#endsyntax#} and imports all the |
| 5616 | 5619 | public top level declarations into the resulting namespace. |
| 5617 | 5620 | </p> |
| 5618 | 5621 | <p> |
| 5619 | | <code>path</code> can be a relative or absolute path, or it can be the name of a package. |
| 5620 | | If it is a relative path, it is relative to the file that contains the <code>@import</code> |
| 5622 | {#syntax#}path{#endsyntax#} can be a relative or absolute path, or it can be the name of a package. |
| 5623 | If it is a relative path, it is relative to the file that contains the {#syntax#}@import{#endsyntax#} |
| 5621 | 5624 | function call. |
| 5622 | 5625 | </p> |
| 5623 | 5626 | <p> |
| 5624 | 5627 | The following packages are always available: |
| 5625 | 5628 | </p> |
| 5626 | 5629 | <ul> |
| 5627 | | <li><code>@import("std")</code> - Zig Standard Library</li> |
| 5628 | | <li><code>@import("builtin")</code> - Compiler-provided types and variables</li> |
| 5630 | <li>{#syntax#}@import("std"){#endsyntax#} - Zig Standard Library</li> |
| 5631 | <li>{#syntax#}@import("builtin"){#endsyntax#} - Compiler-provided types and variables</li> |
| 5629 | 5632 | </ul> |
| 5630 | 5633 | {#see_also|Compile Variables|@embedFile#} |
| 5631 | 5634 | {#header_close#} |
| 5632 | 5635 | {#header_open|@inlineCall#} |
| 5633 | | <pre><code class="zig">@inlineCall(function: X, args: ...) Y</code></pre> |
| 5636 | <pre>{#syntax#}@inlineCall(function: X, args: ...) Y{#endsyntax#}</pre> |
| 5634 | 5637 | <p> |
| 5635 | 5638 | This calls a function, in the same way that invoking an expression with parentheses does: |
| 5636 | 5639 | </p> |
| ... | ... | @@ -5644,14 +5647,14 @@ test "inline function call" { |
| 5644 | 5647 | fn add(a: i32, b: i32) i32 { return a + b; } |
| 5645 | 5648 | {#code_end#} |
| 5646 | 5649 | <p> |
| 5647 | | Unlike a normal function call, however, <code>@inlineCall</code> guarantees that the call |
| 5650 | Unlike a normal function call, however, {#syntax#}@inlineCall{#endsyntax#} guarantees that the call |
| 5648 | 5651 | will be inlined. If the call cannot be inlined, a compile error is emitted. |
| 5649 | 5652 | </p> |
| 5650 | 5653 | {#see_also|@noInlineCall#} |
| 5651 | 5654 | {#header_close#} |
| 5652 | 5655 | |
| 5653 | 5656 | {#header_open|@intCast#} |
| 5654 | | <pre><code class="zig">@intCast(comptime DestType: type, int: var) DestType</code></pre> |
| 5657 | <pre>{#syntax#}@intCast(comptime DestType: type, int: var) DestType{#endsyntax#}</pre> |
| 5655 | 5658 | <p> |
| 5656 | 5659 | Converts an integer to another integer while keeping the same numerical value. |
| 5657 | 5660 | Attempting to convert a number which is out of range of the destination type results in |
| ... | ... | @@ -5660,7 +5663,7 @@ fn add(a: i32, b: i32) i32 { return a + b; } |
| 5660 | 5663 | {#header_close#} |
| 5661 | 5664 | |
| 5662 | 5665 | {#header_open|@intToEnum#} |
| 5663 | | <pre><code class="zig">@intToEnum(comptime DestType: type, int_value: @TagType(DestType)) DestType</code></pre> |
| 5666 | <pre>{#syntax#}@intToEnum(comptime DestType: type, int_value: @TagType(DestType)) DestType{#endsyntax#}</pre> |
| 5664 | 5667 | <p> |
| 5665 | 5668 | Converts an integer into an {#link|enum#} value. |
| 5666 | 5669 | </p> |
| ... | ... | @@ -5672,7 +5675,7 @@ fn add(a: i32, b: i32) i32 { return a + b; } |
| 5672 | 5675 | {#header_close#} |
| 5673 | 5676 | |
| 5674 | 5677 | {#header_open|@intToError#} |
| 5675 | | <pre><code class="zig">@intToError(value: @IntType(false, @sizeOf(error) * 8)) error</code></pre> |
| 5678 | <pre>{#syntax#}@intToError(value: @IntType(false, @sizeOf(error) * 8)) error{#endsyntax#}</pre> |
| 5676 | 5679 | <p> |
| 5677 | 5680 | Converts from the integer representation of an error into the global error set type. |
| 5678 | 5681 | </p> |
| ... | ... | @@ -5688,36 +5691,36 @@ fn add(a: i32, b: i32) i32 { return a + b; } |
| 5688 | 5691 | {#header_close#} |
| 5689 | 5692 | |
| 5690 | 5693 | {#header_open|@intToFloat#} |
| 5691 | | <pre><code class="zig">@intToFloat(comptime DestType: type, int: var) DestType</code></pre> |
| 5694 | <pre>{#syntax#}@intToFloat(comptime DestType: type, int: var) DestType{#endsyntax#}</pre> |
| 5692 | 5695 | <p> |
| 5693 | 5696 | Converts an integer to the closest floating point representation. To convert the other way, use {#link|@floatToInt#}. This cast is always safe. |
| 5694 | 5697 | </p> |
| 5695 | 5698 | {#header_close#} |
| 5696 | 5699 | |
| 5697 | 5700 | {#header_open|@intToPtr#} |
| 5698 | | <pre><code class="zig">@intToPtr(comptime DestType: type, int: usize) DestType</code></pre> |
| 5701 | <pre>{#syntax#}@intToPtr(comptime DestType: type, int: usize) DestType{#endsyntax#}</pre> |
| 5699 | 5702 | <p> |
| 5700 | 5703 | Converts an integer to a pointer. To convert the other way, use {#link|@ptrToInt#}. |
| 5701 | 5704 | </p> |
| 5702 | 5705 | {#header_close#} |
| 5703 | 5706 | |
| 5704 | 5707 | {#header_open|@IntType#} |
| 5705 | | <pre><code class="zig">@IntType(comptime is_signed: bool, comptime bit_count: u32) type</code></pre> |
| 5708 | <pre>{#syntax#}@IntType(comptime is_signed: bool, comptime bit_count: u32) type{#endsyntax#}</pre> |
| 5706 | 5709 | <p> |
| 5707 | 5710 | This function returns an integer type with the given signness and bit count. |
| 5708 | 5711 | </p> |
| 5709 | 5712 | {#header_close#} |
| 5710 | 5713 | {#header_open|@maxValue#} |
| 5711 | | <pre><code class="zig">@maxValue(comptime T: type) comptime_int</code></pre> |
| 5714 | <pre>{#syntax#}@maxValue(comptime T: type) comptime_int{#endsyntax#}</pre> |
| 5712 | 5715 | <p> |
| 5713 | | This function returns the maximum value of the integer type <code>T</code>. |
| 5716 | This function returns the maximum value of the integer type {#syntax#}T{#endsyntax#}. |
| 5714 | 5717 | </p> |
| 5715 | 5718 | <p> |
| 5716 | 5719 | The result is a compile time constant. |
| 5717 | 5720 | </p> |
| 5718 | 5721 | {#header_close#} |
| 5719 | 5722 | {#header_open|@memberCount#} |
| 5720 | | <pre><code class="zig">@memberCount(comptime T: type) comptime_int</code></pre> |
| 5723 | <pre>{#syntax#}@memberCount(comptime T: type) comptime_int{#endsyntax#}</pre> |
| 5721 | 5724 | <p> |
| 5722 | 5725 | This function returns the number of members in a struct, enum, or union type. |
| 5723 | 5726 | </p> |
| ... | ... | @@ -5729,7 +5732,7 @@ fn add(a: i32, b: i32) i32 { return a + b; } |
| 5729 | 5732 | </p> |
| 5730 | 5733 | {#header_close#} |
| 5731 | 5734 | {#header_open|@memberName#} |
| 5732 | | <pre><code class="zig">@memberName(comptime T: type, comptime index: usize) [N]u8</code></pre> |
| 5735 | <pre>{#syntax#}@memberName(comptime T: type, comptime index: usize) [N]u8{#endsyntax#}</pre> |
| 5733 | 5736 | <p>Returns the field name of a struct, union, or enum.</p> |
| 5734 | 5737 | <p> |
| 5735 | 5738 | The result is a compile time constant. |
| ... | ... | @@ -5739,46 +5742,46 @@ fn add(a: i32, b: i32) i32 { return a + b; } |
| 5739 | 5742 | </p> |
| 5740 | 5743 | {#header_close#} |
| 5741 | 5744 | {#header_open|@memberType#} |
| 5742 | | <pre><code class="zig">@memberType(comptime T: type, comptime index: usize) type</code></pre> |
| 5745 | <pre>{#syntax#}@memberType(comptime T: type, comptime index: usize) type{#endsyntax#}</pre> |
| 5743 | 5746 | <p>Returns the field type of a struct or union.</p> |
| 5744 | 5747 | {#header_close#} |
| 5745 | 5748 | {#header_open|@memcpy#} |
| 5746 | | <pre><code class="zig">@memcpy(noalias dest: [*]u8, noalias source: [*]const u8, byte_count: usize)</code></pre> |
| 5749 | <pre>{#syntax#}@memcpy(noalias dest: [*]u8, noalias source: [*]const u8, byte_count: usize){#endsyntax#}</pre> |
| 5747 | 5750 | <p> |
| 5748 | | This function copies bytes from one region of memory to another. <code>dest</code> and |
| 5749 | | <code>source</code> are both pointers and must not overlap. |
| 5751 | This function copies bytes from one region of memory to another. {#syntax#}dest{#endsyntax#} and |
| 5752 | {#syntax#}source{#endsyntax#} are both pointers and must not overlap. |
| 5750 | 5753 | </p> |
| 5751 | 5754 | <p> |
| 5752 | 5755 | This function is a low level intrinsic with no safety mechanisms. Most code |
| 5753 | 5756 | should not use this function, instead using something like this: |
| 5754 | 5757 | </p> |
| 5755 | | <pre><code class="zig">for (source[0...byte_count]) |b, i| dest[i] = b;</code></pre> |
| 5758 | <pre>{#syntax#}for (source[0...byte_count]) |b, i| dest[i] = b;{#endsyntax#}</pre> |
| 5756 | 5759 | <p> |
| 5757 | 5760 | The optimizer is intelligent enough to turn the above snippet into a memcpy. |
| 5758 | 5761 | </p> |
| 5759 | 5762 | <p>There is also a standard library function for this:</p> |
| 5760 | | <pre><code class="zig">const mem = @import("std").mem; |
| 5761 | | mem.copy(u8, dest[0...byte_count], source[0...byte_count]);</code></pre> |
| 5763 | <pre>{#syntax#}const mem = @import("std").mem; |
| 5764 | mem.copy(u8, dest[0...byte_count], source[0...byte_count]);{#endsyntax#}</pre> |
| 5762 | 5765 | {#header_close#} |
| 5763 | 5766 | {#header_open|@memset#} |
| 5764 | | <pre><code class="zig">@memset(dest: [*]u8, c: u8, byte_count: usize)</code></pre> |
| 5767 | <pre>{#syntax#}@memset(dest: [*]u8, c: u8, byte_count: usize){#endsyntax#}</pre> |
| 5765 | 5768 | <p> |
| 5766 | | This function sets a region of memory to <code>c</code>. <code>dest</code> is a pointer. |
| 5769 | This function sets a region of memory to {#syntax#}c{#endsyntax#}. {#syntax#}dest{#endsyntax#} is a pointer. |
| 5767 | 5770 | </p> |
| 5768 | 5771 | <p> |
| 5769 | 5772 | This function is a low level intrinsic with no safety mechanisms. Most |
| 5770 | 5773 | code should not use this function, instead using something like this: |
| 5771 | 5774 | </p> |
| 5772 | | <pre><code class="zig">for (dest[0...byte_count]) |*b| b.* = c;</code></pre> |
| 5775 | <pre>{#syntax#}for (dest[0...byte_count]) |*b| b.* = c;{#endsyntax#}</pre> |
| 5773 | 5776 | <p> |
| 5774 | 5777 | The optimizer is intelligent enough to turn the above snippet into a memset. |
| 5775 | 5778 | </p> |
| 5776 | 5779 | <p>There is also a standard library function for this:</p> |
| 5777 | | <pre><code>const mem = @import("std").mem; |
| 5778 | | mem.set(u8, dest, c);</code></pre> |
| 5780 | <pre>{#syntax#}const mem = @import("std").mem; |
| 5781 | mem.set(u8, dest, c);{#endsyntax#}</pre> |
| 5779 | 5782 | {#header_close#} |
| 5780 | 5783 | {#header_open|@minValue#} |
| 5781 | | <pre><code class="zig">@minValue(comptime T: type) comptime_int</code></pre> |
| 5784 | <pre>{#syntax#}@minValue(comptime T: type) comptime_int{#endsyntax#}</pre> |
| 5782 | 5785 | <p> |
| 5783 | 5786 | This function returns the minimum value of the integer type T. |
| 5784 | 5787 | </p> |
| ... | ... | @@ -5787,31 +5790,31 @@ mem.set(u8, dest, c);</code></pre> |
| 5787 | 5790 | </p> |
| 5788 | 5791 | {#header_close#} |
| 5789 | 5792 | {#header_open|@mod#} |
| 5790 | | <pre><code class="zig">@mod(numerator: T, denominator: T) T</code></pre> |
| 5793 | <pre>{#syntax#}@mod(numerator: T, denominator: T) T{#endsyntax#}</pre> |
| 5791 | 5794 | <p> |
| 5792 | 5795 | Modulus division. For unsigned integers this is the same as |
| 5793 | | <code>numerator % denominator</code>. Caller guarantees <code>denominator &gt; 0</code>. |
| 5796 | {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator &gt; 0{#endsyntax#}. |
| 5794 | 5797 | </p> |
| 5795 | 5798 | <ul> |
| 5796 | | <li><code>@mod(-5, 3) == 1</code></li> |
| 5797 | | <li><code>@divFloor(a, b) + @mod(a, b) == a</code></li> |
| 5799 | <li>{#syntax#}@mod(-5, 3) == 1{#endsyntax#}</li> |
| 5800 | <li>{#syntax#}@divFloor(a, b) + @mod(a, b) == a{#endsyntax#}</li> |
| 5798 | 5801 | </ul> |
| 5799 | | <p>For a function that returns an error code, see <code>@import("std").math.mod</code>.</p> |
| 5802 | <p>For a function that returns an error code, see {#syntax#}@import("std").math.mod{#endsyntax#}.</p> |
| 5800 | 5803 | {#see_also|@rem#} |
| 5801 | 5804 | {#header_close#} |
| 5802 | 5805 | {#header_open|@mulWithOverflow#} |
| 5803 | | <pre><code class="zig">@mulWithOverflow(comptime T: type, a: T, b: T, result: *T) bool</code></pre> |
| 5806 | <pre>{#syntax#}@mulWithOverflow(comptime T: type, a: T, b: T, result: *T) bool{#endsyntax#}</pre> |
| 5804 | 5807 | <p> |
| 5805 | | Performs <code>result.* = a * b</code>. If overflow or underflow occurs, |
| 5806 | | stores the overflowed bits in <code>result</code> and returns <code>true</code>. |
| 5807 | | If no overflow or underflow occurs, returns <code>false</code>. |
| 5808 | Performs {#syntax#}result.* = a * b{#endsyntax#}. If overflow or underflow occurs, |
| 5809 | stores the overflowed bits in {#syntax#}result{#endsyntax#} and returns {#syntax#}true{#endsyntax#}. |
| 5810 | If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}. |
| 5808 | 5811 | </p> |
| 5809 | 5812 | {#header_close#} |
| 5810 | 5813 | {#header_open|@newStackCall#} |
| 5811 | | <pre><code class="zig">@newStackCall(new_stack: []u8, function: var, args: ...) var</code></pre> |
| 5814 | <pre>{#syntax#}@newStackCall(new_stack: []u8, function: var, args: ...) var{#endsyntax#}</pre> |
| 5812 | 5815 | <p> |
| 5813 | 5816 | This calls a function, in the same way that invoking an expression with parentheses does. However, |
| 5814 | | instead of using the same stack as the caller, the function uses the stack provided in the <code>new_stack</code> |
| 5817 | instead of using the same stack as the caller, the function uses the stack provided in the {#syntax#}new_stack{#endsyntax#} |
| 5815 | 5818 | parameter. |
| 5816 | 5819 | </p> |
| 5817 | 5820 | {#code_begin|test#} |
| ... | ... | @@ -5844,7 +5847,7 @@ fn targetFunction(x: i32) usize { |
| 5844 | 5847 | {#code_end#} |
| 5845 | 5848 | {#header_close#} |
| 5846 | 5849 | {#header_open|@noInlineCall#} |
| 5847 | | <pre><code class="zig">@noInlineCall(function: var, args: ...) var</code></pre> |
| 5850 | <pre>{#syntax#}@noInlineCall(function: var, args: ...) var{#endsyntax#}</pre> |
| 5848 | 5851 | <p> |
| 5849 | 5852 | This calls a function, in the same way that invoking an expression with parentheses does: |
| 5850 | 5853 | </p> |
| ... | ... | @@ -5860,19 +5863,19 @@ fn add(a: i32, b: i32) i32 { |
| 5860 | 5863 | } |
| 5861 | 5864 | {#code_end#} |
| 5862 | 5865 | <p> |
| 5863 | | Unlike a normal function call, however, <code>@noInlineCall</code> guarantees that the call |
| 5866 | Unlike a normal function call, however, {#syntax#}@noInlineCall{#endsyntax#} guarantees that the call |
| 5864 | 5867 | will not be inlined. If the call must be inlined, a compile error is emitted. |
| 5865 | 5868 | </p> |
| 5866 | 5869 | {#see_also|@inlineCall#} |
| 5867 | 5870 | {#header_close#} |
| 5868 | 5871 | {#header_open|@offsetOf#} |
| 5869 | | <pre><code class="zig">@offsetOf(comptime T: type, comptime field_name: [] const u8) comptime_int</code></pre> |
| 5872 | <pre>{#syntax#}@offsetOf(comptime T: type, comptime field_name: [] const u8) comptime_int{#endsyntax#}</pre> |
| 5870 | 5873 | <p> |
| 5871 | 5874 | This function returns the byte offset of a field relative to its containing struct. |
| 5872 | 5875 | </p> |
| 5873 | 5876 | {#header_close#} |
| 5874 | 5877 | {#header_open|@OpaqueType#} |
| 5875 | | <pre><code class="zig">@OpaqueType() type</code></pre> |
| 5878 | <pre>{#syntax#}@OpaqueType() type{#endsyntax#}</pre> |
| 5876 | 5879 | <p> |
| 5877 | 5880 | Creates a new type with an unknown size and alignment. |
| 5878 | 5881 | </p> |
| ... | ... | @@ -5895,14 +5898,14 @@ test "call foo" { |
| 5895 | 5898 | {#code_end#} |
| 5896 | 5899 | {#header_close#} |
| 5897 | 5900 | {#header_open|@panic#} |
| 5898 | | <pre><code class="zig">@panic(message: []const u8) noreturn</code></pre> |
| 5901 | <pre>{#syntax#}@panic(message: []const u8) noreturn{#endsyntax#}</pre> |
| 5899 | 5902 | <p> |
| 5900 | 5903 | Invokes the panic handler function. By default the panic handler function |
| 5901 | | calls the public <code>panic</code> function exposed in the root source file, or |
| 5902 | | if there is not one specified, invokes the one provided in <code>std/special/panic.zig</code>. |
| 5904 | calls the public {#syntax#}panic{#endsyntax#} function exposed in the root source file, or |
| 5905 | if there is not one specified, invokes the one provided in {#syntax#}std/special/panic.zig{#endsyntax#}. |
| 5903 | 5906 | </p> |
| 5904 | | <p>Generally it is better to use <code>@import("std").debug.panic</code>. |
| 5905 | | However, <code>@panic</code> can be useful for 2 scenarios: |
| 5907 | <p>Generally it is better to use {#syntax#}@import("std").debug.panic{#endsyntax#}. |
| 5908 | However, {#syntax#}@panic{#endsyntax#} can be useful for 2 scenarios: |
| 5906 | 5909 | </p> |
| 5907 | 5910 | <ul> |
| 5908 | 5911 | <li>From library code, calling the programmer's panic function if they exposed one in the root source file.</li> |
| ... | ... | @@ -5911,50 +5914,50 @@ test "call foo" { |
| 5911 | 5914 | {#see_also|Root Source File#} |
| 5912 | 5915 | {#header_close#} |
| 5913 | 5916 | {#header_open|@popCount#} |
| 5914 | | <pre><code class="zig">@popCount(integer: var) var</code></pre> |
| 5917 | <pre>{#syntax#}@popCount(integer: var) var{#endsyntax#}</pre> |
| 5915 | 5918 | <p>Counts the number of bits set in an integer.</p> |
| 5916 | 5919 | <p> |
| 5917 | | If <code>integer</code> is known at {#link|comptime#}, the return type is <code>comptime_int</code>. |
| 5920 | If {#syntax#}integer{#endsyntax#} is known at {#link|comptime#}, the return type is {#syntax#}comptime_int{#endsyntax#}. |
| 5918 | 5921 | Otherwise, the return type is an unsigned integer with the minimum number |
| 5919 | 5922 | of bits that can represent the bit count of the integer type. |
| 5920 | 5923 | </p> |
| 5921 | 5924 | {#see_also|@ctz|@clz#} |
| 5922 | 5925 | {#header_close#} |
| 5923 | 5926 | {#header_open|@ptrCast#} |
| 5924 | | <pre><code class="zig">@ptrCast(comptime DestType: type, value: var) DestType</code></pre> |
| 5927 | <pre>{#syntax#}@ptrCast(comptime DestType: type, value: var) DestType{#endsyntax#}</pre> |
| 5925 | 5928 | <p> |
| 5926 | 5929 | Converts a pointer of one type to a pointer of another type. |
| 5927 | 5930 | </p> |
| 5928 | 5931 | {#header_close#} |
| 5929 | 5932 | {#header_open|@ptrToInt#} |
| 5930 | | <pre><code class="zig">@ptrToInt(value: var) usize</code></pre> |
| 5933 | <pre>{#syntax#}@ptrToInt(value: var) usize{#endsyntax#}</pre> |
| 5931 | 5934 | <p> |
| 5932 | | Converts <code>value</code> to a <code>usize</code> which is the address of the pointer. <code>value</code> can be one of these types: |
| 5935 | Converts {#syntax#}value{#endsyntax#} to a {#syntax#}usize{#endsyntax#} which is the address of the pointer. {#syntax#}value{#endsyntax#} can be one of these types: |
| 5933 | 5936 | </p> |
| 5934 | 5937 | <ul> |
| 5935 | | <li><code>*T</code></li> |
| 5936 | | <li><code>?*T</code></li> |
| 5937 | | <li><code>fn()</code></li> |
| 5938 | | <li><code>?fn()</code></li> |
| 5938 | <li>{#syntax#}*T{#endsyntax#}</li> |
| 5939 | <li>{#syntax#}?*T{#endsyntax#}</li> |
| 5940 | <li>{#syntax#}fn(){#endsyntax#}</li> |
| 5941 | <li>{#syntax#}?fn(){#endsyntax#}</li> |
| 5939 | 5942 | </ul> |
| 5940 | 5943 | <p>To convert the other way, use {#link|@intToPtr#}</p> |
| 5941 | 5944 | |
| 5942 | 5945 | {#header_close#} |
| 5943 | 5946 | {#header_open|@rem#} |
| 5944 | | <pre><code class="zig">@rem(numerator: T, denominator: T) T</code></pre> |
| 5947 | <pre>{#syntax#}@rem(numerator: T, denominator: T) T{#endsyntax#}</pre> |
| 5945 | 5948 | <p> |
| 5946 | 5949 | Remainder division. For unsigned integers this is the same as |
| 5947 | | <code>numerator % denominator</code>. Caller guarantees <code>denominator &gt; 0</code>. |
| 5950 | {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}. |
| 5948 | 5951 | </p> |
| 5949 | 5952 | <ul> |
| 5950 | | <li><code>@rem(-5, 3) == -2</code></li> |
| 5951 | | <li><code>@divTrunc(a, b) + @rem(a, b) == a</code></li> |
| 5953 | <li>{#syntax#}@rem(-5, 3) == -2{#endsyntax#}</li> |
| 5954 | <li>{#syntax#}@divTrunc(a, b) + @rem(a, b) == a{#endsyntax#}</li> |
| 5952 | 5955 | </ul> |
| 5953 | | <p>For a function that returns an error code, see <code>@import("std").math.rem</code>.</p> |
| 5956 | <p>For a function that returns an error code, see {#syntax#}@import("std").math.rem{#endsyntax#}.</p> |
| 5954 | 5957 | {#see_also|@mod#} |
| 5955 | 5958 | {#header_close#} |
| 5956 | 5959 | {#header_open|@returnAddress#} |
| 5957 | | <pre><code class="zig">@returnAddress()</code></pre> |
| 5960 | <pre>{#syntax#}@returnAddress(){#endsyntax#}</pre> |
| 5958 | 5961 | <p> |
| 5959 | 5962 | This function returns a pointer to the return address of the current stack |
| 5960 | 5963 | frame. |
| ... | ... | @@ -5968,32 +5971,32 @@ test "call foo" { |
| 5968 | 5971 | </p> |
| 5969 | 5972 | {#header_close#} |
| 5970 | 5973 | {#header_open|@setAlignStack#} |
| 5971 | | <pre><code class="zig">@setAlignStack(comptime alignment: u29)</code></pre> |
| 5974 | <pre>{#syntax#}@setAlignStack(comptime alignment: u29){#endsyntax#}</pre> |
| 5972 | 5975 | <p> |
| 5973 | | Ensures that a function will have a stack alignment of at least <code>alignment</code> bytes. |
| 5976 | Ensures that a function will have a stack alignment of at least {#syntax#}alignment{#endsyntax#} bytes. |
| 5974 | 5977 | </p> |
| 5975 | 5978 | {#header_close#} |
| 5976 | 5979 | {#header_open|@setCold#} |
| 5977 | | <pre><code class="zig">@setCold(is_cold: bool)</code></pre> |
| 5980 | <pre>{#syntax#}@setCold(is_cold: bool){#endsyntax#}</pre> |
| 5978 | 5981 | <p> |
| 5979 | 5982 | Tells the optimizer that a function is rarely called. |
| 5980 | 5983 | </p> |
| 5981 | 5984 | {#header_close#} |
| 5982 | 5985 | {#header_open|@setRuntimeSafety#} |
| 5983 | | <pre><code class="zig">@setRuntimeSafety(safety_on: bool)</code></pre> |
| 5986 | <pre>{#syntax#}@setRuntimeSafety(safety_on: bool){#endsyntax#}</pre> |
| 5984 | 5987 | <p> |
| 5985 | 5988 | Sets whether runtime safety checks are on for the scope that contains the function call. |
| 5986 | 5989 | </p> |
| 5987 | 5990 | |
| 5988 | 5991 | {#header_close#} |
| 5989 | 5992 | {#header_open|@setEvalBranchQuota#} |
| 5990 | | <pre><code class="zig">@setEvalBranchQuota(new_quota: usize)</code></pre> |
| 5993 | <pre>{#syntax#}@setEvalBranchQuota(new_quota: usize){#endsyntax#}</pre> |
| 5991 | 5994 | <p> |
| 5992 | 5995 | Changes the maximum number of backwards branches that compile-time code |
| 5993 | 5996 | execution can use before giving up and making a compile error. |
| 5994 | 5997 | </p> |
| 5995 | 5998 | <p> |
| 5996 | | If the <code>new_quota</code> is smaller than the default quota (<code>1000</code>) or |
| 5999 | If the {#syntax#}new_quota{#endsyntax#} is smaller than the default quota ({#syntax#}1000{#endsyntax#}) or |
| 5997 | 6000 | a previously explicitly set quota, it is ignored. |
| 5998 | 6001 | </p> |
| 5999 | 6002 | <p> |
| ... | ... | @@ -6007,7 +6010,7 @@ test "foo" { |
| 6007 | 6010 | } |
| 6008 | 6011 | } |
| 6009 | 6012 | {#code_end#} |
| 6010 | | <p>Now we use <code class="zig">@setEvalBranchQuota</code>:</p> |
| 6013 | <p>Now we use {#syntax#}@setEvalBranchQuota{#endsyntax#}:</p> |
| 6011 | 6014 | {#code_begin|test#} |
| 6012 | 6015 | test "foo" { |
| 6013 | 6016 | comptime { |
| ... | ... | @@ -6021,7 +6024,7 @@ test "foo" { |
| 6021 | 6024 | {#see_also|comptime#} |
| 6022 | 6025 | {#header_close#} |
| 6023 | 6026 | {#header_open|@setFloatMode#} |
| 6024 | | <pre><code class="zig">@setFloatMode(mode: @import("builtin").FloatMode)</code></pre> |
| 6027 | <pre>{#syntax#}@setFloatMode(mode: @import("builtin").FloatMode){#endsyntax#}</pre> |
| 6025 | 6028 | <p> |
| 6026 | 6029 | Sets the floating point mode of the current scope. Possible values are: |
| 6027 | 6030 | </p> |
| ... | ... | @@ -6033,10 +6036,10 @@ pub const FloatMode = enum { |
| 6033 | 6036 | {#code_end#} |
| 6034 | 6037 | <ul> |
| 6035 | 6038 | <li> |
| 6036 | | <code>Strict</code> (default) - Floating point operations follow strict IEEE compliance. |
| 6039 | {#syntax#}Strict{#endsyntax#} (default) - Floating point operations follow strict IEEE compliance. |
| 6037 | 6040 | </li> |
| 6038 | 6041 | <li> |
| 6039 | | <code>Optimized</code> - Floating point operations may do all of the following: |
| 6042 | {#syntax#}Optimized{#endsyntax#} - Floating point operations may do all of the following: |
| 6040 | 6043 | <ul> |
| 6041 | 6044 | <li>Assume the arguments and result are not NaN. Optimizations are required to retain defined behavior over NaNs, but the value of the result is undefined.</li> |
| 6042 | 6045 | <li>Assume the arguments and result are not +/-Inf. Optimizations are required to retain defined behavior over +/-Inf, but the value of the result is undefined.</li> |
| ... | ... | @@ -6055,54 +6058,54 @@ pub const FloatMode = enum { |
| 6055 | 6058 | {#see_also|Floating Point Operations#} |
| 6056 | 6059 | {#header_close#} |
| 6057 | 6060 | {#header_open|@setGlobalLinkage#} |
| 6058 | | <pre><code class="zig">@setGlobalLinkage(global_variable_name, comptime linkage: GlobalLinkage)</code></pre> |
| 6061 | <pre>{#syntax#}@setGlobalLinkage(global_variable_name, comptime linkage: GlobalLinkage){#endsyntax#}</pre> |
| 6059 | 6062 | <p> |
| 6060 | | <code>GlobalLinkage</code> can be found with <code>@import("builtin").GlobalLinkage</code>. |
| 6063 | {#syntax#}GlobalLinkage{#endsyntax#} can be found with {#syntax#}@import("builtin").GlobalLinkage{#endsyntax#}. |
| 6061 | 6064 | </p> |
| 6062 | 6065 | {#see_also|Compile Variables#} |
| 6063 | 6066 | {#header_close#} |
| 6064 | 6067 | {#header_open|@shlExact#} |
| 6065 | | <pre><code class="zig">@shlExact(value: T, shift_amt: Log2T) T</code></pre> |
| 6068 | <pre>{#syntax#}@shlExact(value: T, shift_amt: Log2T) T{#endsyntax#}</pre> |
| 6066 | 6069 | <p> |
| 6067 | | Performs the left shift operation (<code>&lt;&lt;</code>). Caller guarantees |
| 6070 | Performs the left shift operation ({#syntax#}<<{#endsyntax#}). Caller guarantees |
| 6068 | 6071 | that the shift will not shift any 1 bits out. |
| 6069 | 6072 | </p> |
| 6070 | 6073 | <p> |
| 6071 | | The type of <code>shift_amt</code> is an unsigned integer with <code>log2(T.bit_count)</code> bits. |
| 6072 | | This is because <code>shift_amt &gt;= T.bit_count</code> is undefined behavior. |
| 6074 | The type of {#syntax#}shift_amt{#endsyntax#} is an unsigned integer with {#syntax#}log2(T.bit_count){#endsyntax#} bits. |
| 6075 | This is because {#syntax#}shift_amt >= T.bit_count{#endsyntax#} is undefined behavior. |
| 6073 | 6076 | </p> |
| 6074 | 6077 | {#see_also|@shrExact|@shlWithOverflow#} |
| 6075 | 6078 | {#header_close#} |
| 6076 | 6079 | {#header_open|@shlWithOverflow#} |
| 6077 | | <pre><code class="zig">@shlWithOverflow(comptime T: type, a: T, shift_amt: Log2T, result: *T) bool</code></pre> |
| 6080 | <pre>{#syntax#}@shlWithOverflow(comptime T: type, a: T, shift_amt: Log2T, result: *T) bool{#endsyntax#}</pre> |
| 6078 | 6081 | <p> |
| 6079 | | Performs <code>result.* = a &lt;&lt; b</code>. If overflow or underflow occurs, |
| 6080 | | stores the overflowed bits in <code>result</code> and returns <code>true</code>. |
| 6081 | | If no overflow or underflow occurs, returns <code>false</code>. |
| 6082 | Performs {#syntax#}result.* = a << b{#endsyntax#}. If overflow or underflow occurs, |
| 6083 | stores the overflowed bits in {#syntax#}result{#endsyntax#} and returns {#syntax#}true{#endsyntax#}. |
| 6084 | If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}. |
| 6082 | 6085 | </p> |
| 6083 | 6086 | <p> |
| 6084 | | The type of <code>shift_amt</code> is an unsigned integer with <code>log2(T.bit_count)</code> bits. |
| 6085 | | This is because <code>shift_amt &gt;= T.bit_count</code> is undefined behavior. |
| 6087 | The type of {#syntax#}shift_amt{#endsyntax#} is an unsigned integer with {#syntax#}log2(T.bit_count){#endsyntax#} bits. |
| 6088 | This is because {#syntax#}shift_amt >= T.bit_count{#endsyntax#} is undefined behavior. |
| 6086 | 6089 | </p> |
| 6087 | 6090 | {#see_also|@shlExact|@shrExact#} |
| 6088 | 6091 | {#header_close#} |
| 6089 | 6092 | {#header_open|@shrExact#} |
| 6090 | | <pre><code class="zig">@shrExact(value: T, shift_amt: Log2T) T</code></pre> |
| 6093 | <pre>{#syntax#}@shrExact(value: T, shift_amt: Log2T) T{#endsyntax#}</pre> |
| 6091 | 6094 | <p> |
| 6092 | | Performs the right shift operation (<code>&gt;&gt;</code>). Caller guarantees |
| 6095 | Performs the right shift operation ({#syntax#}>>{#endsyntax#}). Caller guarantees |
| 6093 | 6096 | that the shift will not shift any 1 bits out. |
| 6094 | 6097 | </p> |
| 6095 | 6098 | <p> |
| 6096 | | The type of <code>shift_amt</code> is an unsigned integer with <code>log2(T.bit_count)</code> bits. |
| 6097 | | This is because <code>shift_amt &gt;= T.bit_count</code> is undefined behavior. |
| 6099 | The type of {#syntax#}shift_amt{#endsyntax#} is an unsigned integer with {#syntax#}log2(T.bit_count){#endsyntax#} bits. |
| 6100 | This is because {#syntax#}shift_amt >= T.bit_count{#endsyntax#} is undefined behavior. |
| 6098 | 6101 | </p> |
| 6099 | 6102 | {#see_also|@shlExact|@shlWithOverflow#} |
| 6100 | 6103 | {#header_close#} |
| 6101 | 6104 | |
| 6102 | 6105 | {#header_open|@sizeOf#} |
| 6103 | | <pre><code class="zig">@sizeOf(comptime T: type) comptime_int</code></pre> |
| 6106 | <pre>{#syntax#}@sizeOf(comptime T: type) comptime_int{#endsyntax#}</pre> |
| 6104 | 6107 | <p> |
| 6105 | | This function returns the number of bytes it takes to store <code>T</code> in memory. |
| 6108 | This function returns the number of bytes it takes to store {#syntax#}T{#endsyntax#} in memory. |
| 6106 | 6109 | </p> |
| 6107 | 6110 | <p> |
| 6108 | 6111 | The result is a target-specific compile time constant. |
| ... | ... | @@ -6110,39 +6113,39 @@ pub const FloatMode = enum { |
| 6110 | 6113 | {#header_close#} |
| 6111 | 6114 | |
| 6112 | 6115 | {#header_open|@sliceToBytes#} |
| 6113 | | <pre><code class="zig">@sliceToBytes(value: var) []u8</code></pre> |
| 6116 | <pre>{#syntax#}@sliceToBytes(value: var) []u8{#endsyntax#}</pre> |
| 6114 | 6117 | <p> |
| 6115 | | Converts a slice or array to a slice of <code>u8</code>. The resulting slice has the same |
| 6118 | Converts a slice or array to a slice of {#syntax#}u8{#endsyntax#}. The resulting slice has the same |
| 6116 | 6119 | {#link|pointer|Pointers#} properties as the parameter. |
| 6117 | 6120 | </p> |
| 6118 | 6121 | {#header_close#} |
| 6119 | 6122 | |
| 6120 | 6123 | {#header_open|@sqrt#} |
| 6121 | | <pre><code class="zig">@sqrt(comptime T: type, value: T) T</code></pre> |
| 6124 | <pre>{#syntax#}@sqrt(comptime T: type, value: T) T{#endsyntax#}</pre> |
| 6122 | 6125 | <p> |
| 6123 | 6126 | Performs the square root of a floating point number. Uses a dedicated hardware instruction |
| 6124 | 6127 | when available. Currently only supports f32 and f64 at runtime. f128 at runtime is TODO. |
| 6125 | 6128 | </p> |
| 6126 | 6129 | <p> |
| 6127 | | This is a low-level intrinsic. Most code can use <code>std.math.sqrt</code> instead. |
| 6130 | This is a low-level intrinsic. Most code can use {#syntax#}std.math.sqrt{#endsyntax#} instead. |
| 6128 | 6131 | </p> |
| 6129 | 6132 | {#header_close#} |
| 6130 | 6133 | {#header_open|@subWithOverflow#} |
| 6131 | | <pre><code class="zig">@subWithOverflow(comptime T: type, a: T, b: T, result: *T) bool</code></pre> |
| 6134 | <pre>{#syntax#}@subWithOverflow(comptime T: type, a: T, b: T, result: *T) bool{#endsyntax#}</pre> |
| 6132 | 6135 | <p> |
| 6133 | | Performs <code>result.* = a - b</code>. If overflow or underflow occurs, |
| 6134 | | stores the overflowed bits in <code>result</code> and returns <code>true</code>. |
| 6135 | | If no overflow or underflow occurs, returns <code>false</code>. |
| 6136 | Performs {#syntax#}result.* = a - b{#endsyntax#}. If overflow or underflow occurs, |
| 6137 | stores the overflowed bits in {#syntax#}result{#endsyntax#} and returns {#syntax#}true{#endsyntax#}. |
| 6138 | If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}. |
| 6136 | 6139 | </p> |
| 6137 | 6140 | {#header_close#} |
| 6138 | 6141 | {#header_open|@tagName#} |
| 6139 | | <pre><code class="zig">@tagName(value: var) []const u8</code></pre> |
| 6142 | <pre>{#syntax#}@tagName(value: var) []const u8{#endsyntax#}</pre> |
| 6140 | 6143 | <p> |
| 6141 | 6144 | Converts an enum value or union value to a slice of bytes representing the name. |
| 6142 | 6145 | </p> |
| 6143 | 6146 | {#header_close#} |
| 6144 | 6147 | {#header_open|@TagType#} |
| 6145 | | <pre><code class="zig">@TagType(T: type) type</code></pre> |
| 6148 | <pre>{#syntax#}@TagType(T: type) type{#endsyntax#}</pre> |
| 6146 | 6149 | <p> |
| 6147 | 6150 | For an enum, returns the integer type that is used to store the enumeration value. |
| 6148 | 6151 | </p> |
| ... | ... | @@ -6151,7 +6154,7 @@ pub const FloatMode = enum { |
| 6151 | 6154 | </p> |
| 6152 | 6155 | {#header_close#} |
| 6153 | 6156 | {#header_open|@truncate#} |
| 6154 | | <pre><code class="zig">@truncate(comptime T: type, integer) T</code></pre> |
| 6157 | <pre>{#syntax#}@truncate(comptime T: type, integer) T{#endsyntax#}</pre> |
| 6155 | 6158 | <p> |
| 6156 | 6159 | This function truncates bits from an integer type, resulting in a smaller |
| 6157 | 6160 | integer type. |
| ... | ... | @@ -6160,14 +6163,14 @@ pub const FloatMode = enum { |
| 6160 | 6163 | The following produces a crash in debug mode and undefined behavior in |
| 6161 | 6164 | release mode: |
| 6162 | 6165 | </p> |
| 6163 | | <pre><code class="zig">const a: u16 = 0xabcd; |
| 6164 | | const b: u8 = u8(a);</code></pre> |
| 6166 | <pre>{#syntax#}const a: u16 = 0xabcd; |
| 6167 | const b: u8 = u8(a);{#endsyntax#}</pre> |
| 6165 | 6168 | <p> |
| 6166 | 6169 | However this is well defined and working code: |
| 6167 | 6170 | </p> |
| 6168 | | <pre><code class="zig">const a: u16 = 0xabcd; |
| 6171 | <pre>{#syntax#}const a: u16 = 0xabcd; |
| 6169 | 6172 | const b: u8 = @truncate(u8, a); |
| 6170 | | // b is now 0xcd</code></pre> |
| 6173 | // b is now 0xcd{#endsyntax#}</pre> |
| 6171 | 6174 | <p> |
| 6172 | 6175 | This function always truncates the significant bits of the integer, regardless |
| 6173 | 6176 | of endianness on the target platform. |
| ... | ... | @@ -6175,7 +6178,7 @@ const b: u8 = @truncate(u8, a); |
| 6175 | 6178 | |
| 6176 | 6179 | {#header_close#} |
| 6177 | 6180 | {#header_open|@typeId#} |
| 6178 | | <pre><code class="zig">@typeId(comptime T: type) @import("builtin").TypeId</code></pre> |
| 6181 | <pre>{#syntax#}@typeId(comptime T: type) @import("builtin").TypeId{#endsyntax#}</pre> |
| 6179 | 6182 | <p> |
| 6180 | 6183 | Returns which kind of type something is. Possible values: |
| 6181 | 6184 | </p> |
| ... | ... | @@ -6209,7 +6212,7 @@ pub const TypeId = enum { |
| 6209 | 6212 | {#code_end#} |
| 6210 | 6213 | {#header_close#} |
| 6211 | 6214 | {#header_open|@typeInfo#} |
| 6212 | | <pre><code class="zig">@typeInfo(comptime T: type) @import("builtin").TypeInfo</code></pre> |
| 6215 | <pre>{#syntax#}@typeInfo(comptime T: type) @import("builtin").TypeInfo{#endsyntax#}</pre> |
| 6213 | 6216 | <p> |
| 6214 | 6217 | Returns information on the type. Returns a value of the following union: |
| 6215 | 6218 | </p> |
| ... | ... | @@ -6392,14 +6395,14 @@ pub const TypeInfo = union(TypeId) { |
| 6392 | 6395 | {#code_end#} |
| 6393 | 6396 | {#header_close#} |
| 6394 | 6397 | {#header_open|@typeName#} |
| 6395 | | <pre><code class="zig">@typeName(T: type) []u8</code></pre> |
| 6398 | <pre>{#syntax#}@typeName(T: type) []u8{#endsyntax#}</pre> |
| 6396 | 6399 | <p> |
| 6397 | 6400 | This function returns the string representation of a type. |
| 6398 | 6401 | </p> |
| 6399 | 6402 | |
| 6400 | 6403 | {#header_close#} |
| 6401 | 6404 | {#header_open|@typeOf#} |
| 6402 | | <pre><code class="zig">@typeOf(expression) type</code></pre> |
| 6405 | <pre>{#syntax#}@typeOf(expression) type{#endsyntax#}</pre> |
| 6403 | 6406 | <p> |
| 6404 | 6407 | This function returns a compile-time constant, which is the type of the |
| 6405 | 6408 | expression passed as an argument. The expression is evaluated. |
| ... | ... | @@ -6576,11 +6579,11 @@ pub fn main() void { |
| 6576 | 6579 | {#header_open|Default Operations#} |
| 6577 | 6580 | <p>The following operators can cause integer overflow:</p> |
| 6578 | 6581 | <ul> |
| 6579 | | <li><code>+</code> (addition)</li> |
| 6580 | | <li><code>-</code> (subtraction)</li> |
| 6581 | | <li><code>-</code> (negation)</li> |
| 6582 | | <li><code>*</code> (multiplication)</li> |
| 6583 | | <li><code>/</code> (division)</li> |
| 6582 | <li>{#syntax#}+{#endsyntax#} (addition)</li> |
| 6583 | <li>{#syntax#}-{#endsyntax#} (subtraction)</li> |
| 6584 | <li>{#syntax#}-{#endsyntax#} (negation)</li> |
| 6585 | <li>{#syntax#}*{#endsyntax#} (multiplication)</li> |
| 6586 | <li>{#syntax#}/{#endsyntax#} (division)</li> |
| 6584 | 6587 | <li>{#link|@divTrunc#} (division)</li> |
| 6585 | 6588 | <li>{#link|@divFloor#} (division)</li> |
| 6586 | 6589 | <li>{#link|@divExact#} (division)</li> |
| ... | ... | @@ -6606,13 +6609,13 @@ pub fn main() void { |
| 6606 | 6609 | {#header_open|Standard Library Math Functions#} |
| 6607 | 6610 | <p>These functions provided by the standard library return possible errors.</p> |
| 6608 | 6611 | <ul> |
| 6609 | | <li><code>@import("std").math.add</code></li> |
| 6610 | | <li><code>@import("std").math.sub</code></li> |
| 6611 | | <li><code>@import("std").math.mul</code></li> |
| 6612 | | <li><code>@import("std").math.divTrunc</code></li> |
| 6613 | | <li><code>@import("std").math.divFloor</code></li> |
| 6614 | | <li><code>@import("std").math.divExact</code></li> |
| 6615 | | <li><code>@import("std").math.shl</code></li> |
| 6612 | <li>{#syntax#}@import("std").math.add{#endsyntax#}</li> |
| 6613 | <li>{#syntax#}@import("std").math.sub{#endsyntax#}</li> |
| 6614 | <li>{#syntax#}@import("std").math.mul{#endsyntax#}</li> |
| 6615 | <li>{#syntax#}@import("std").math.divTrunc{#endsyntax#}</li> |
| 6616 | <li>{#syntax#}@import("std").math.divFloor{#endsyntax#}</li> |
| 6617 | <li>{#syntax#}@import("std").math.divExact{#endsyntax#}</li> |
| 6618 | <li>{#syntax#}@import("std").math.shl{#endsyntax#}</li> |
| 6616 | 6619 | </ul> |
| 6617 | 6620 | <p>Example of catching an overflow for addition:</p> |
| 6618 | 6621 | {#code_begin|exe_err#} |
| ... | ... | @@ -6632,7 +6635,7 @@ pub fn main() !void { |
| 6632 | 6635 | {#header_close#} |
| 6633 | 6636 | {#header_open|Builtin Overflow Functions#} |
| 6634 | 6637 | <p> |
| 6635 | | These builtins return a <code>bool</code> of whether or not overflow |
| 6638 | These builtins return a {#syntax#}bool{#endsyntax#} of whether or not overflow |
| 6636 | 6639 | occurred, as well as returning the overflowed bits: |
| 6637 | 6640 | </p> |
| 6638 | 6641 | <ul> |
| ... | ... | @@ -6663,10 +6666,10 @@ pub fn main() void { |
| 6663 | 6666 | These operations have guaranteed wraparound semantics. |
| 6664 | 6667 | </p> |
| 6665 | 6668 | <ul> |
| 6666 | | <li><code>+%</code> (wraparound addition)</li> |
| 6667 | | <li><code>-%</code> (wraparound subtraction)</li> |
| 6668 | | <li><code>-%</code> (wraparound negation)</li> |
| 6669 | | <li><code>*%</code> (wraparound multiplication)</li> |
| 6669 | <li>{#syntax#}+%{#endsyntax#} (wraparound addition)</li> |
| 6670 | <li>{#syntax#}-%{#endsyntax#} (wraparound subtraction)</li> |
| 6671 | <li>{#syntax#}-%{#endsyntax#} (wraparound negation)</li> |
| 6672 | <li>{#syntax#}*%{#endsyntax#} (wraparound multiplication)</li> |
| 6670 | 6673 | </ul> |
| 6671 | 6674 | {#code_begin|test#} |
| 6672 | 6675 | const assert = @import("std").debug.assert; |
| ... | ... | @@ -6818,7 +6821,7 @@ pub fn main() void { |
| 6818 | 6821 | } |
| 6819 | 6822 | {#code_end#} |
| 6820 | 6823 | <p>One way to avoid this crash is to test for null instead of assuming non-null, with |
| 6821 | | the <code>if</code> expression:</p> |
| 6824 | the {#syntax#}if{#endsyntax#} expression:</p> |
| 6822 | 6825 | {#code_begin|exe|test#} |
| 6823 | 6826 | const warn = @import("std").debug.warn; |
| 6824 | 6827 | pub fn main() void { |
| ... | ... | @@ -6858,7 +6861,7 @@ fn getNumberOrFail() !i32 { |
| 6858 | 6861 | } |
| 6859 | 6862 | {#code_end#} |
| 6860 | 6863 | <p>One way to avoid this crash is to test for an error instead of assuming a successful result, with |
| 6861 | | the <code>if</code> expression:</p> |
| 6864 | the {#syntax#}if{#endsyntax#} expression:</p> |
| 6862 | 6865 | {#code_begin|exe#} |
| 6863 | 6866 | const warn = @import("std").debug.warn; |
| 6864 | 6867 | |
| ... | ... | @@ -7022,7 +7025,7 @@ fn bar(f: *Foo) void { |
| 7022 | 7025 | } |
| 7023 | 7026 | {#code_end#} |
| 7024 | 7027 | <p> |
| 7025 | | This safety is not available for <code>extern</code> or <code>packed</code> unions. |
| 7028 | This safety is not available for {#syntax#}extern{#endsyntax#} or {#syntax#}packed{#endsyntax#} unions. |
| 7026 | 7029 | </p> |
| 7027 | 7030 | <p> |
| 7028 | 7031 | To change the active field of a union, assign the entire union, like this: |
| ... | ... | @@ -7087,7 +7090,7 @@ fn bar(f: *Foo) void { |
| 7087 | 7090 | {#header_close#} |
| 7088 | 7091 | {#header_open|Compile Variables#} |
| 7089 | 7092 | <p> |
| 7090 | | Compile variables are accessible by importing the <code>"builtin"</code> package, |
| 7093 | Compile variables are accessible by importing the {#syntax#}"builtin"{#endsyntax#} package, |
| 7091 | 7094 | which the compiler makes available to every Zig source file. It contains |
| 7092 | 7095 | compile-time constants such as the current target, endianness, and release mode. |
| 7093 | 7096 | </p> |
| ... | ... | @@ -7096,7 +7099,7 @@ const builtin = @import("builtin"); |
| 7096 | 7099 | const separator = if (builtin.os == builtin.Os.windows) '\\' else '/'; |
| 7097 | 7100 | {#code_end#} |
| 7098 | 7101 | <p> |
| 7099 | | Example of what is imported with <code>@import("builtin")</code>: |
| 7102 | Example of what is imported with {#syntax#}@import("builtin"){#endsyntax#}: |
| 7100 | 7103 | </p> |
| 7101 | 7104 | {#builtin#} |
| 7102 | 7105 | {#see_also|Build Mode#} |
| ... | ... | @@ -7135,16 +7138,16 @@ const separator = if (builtin.os == builtin.Os.windows) '\\' else '/'; |
| 7135 | 7138 | These have guaranteed C ABI compatibility and can be used like any other type. |
| 7136 | 7139 | </p> |
| 7137 | 7140 | <ul> |
| 7138 | | <li><code>c_short</code></li> |
| 7139 | | <li><code>c_ushort</code></li> |
| 7140 | | <li><code>c_int</code></li> |
| 7141 | | <li><code>c_uint</code></li> |
| 7142 | | <li><code>c_long</code></li> |
| 7143 | | <li><code>c_ulong</code></li> |
| 7144 | | <li><code>c_longlong</code></li> |
| 7145 | | <li><code>c_ulonglong</code></li> |
| 7146 | | <li><code>c_longdouble</code></li> |
| 7147 | | <li><code>c_void</code></li> |
| 7141 | <li>{#syntax#}c_short{#endsyntax#}</li> |
| 7142 | <li>{#syntax#}c_ushort{#endsyntax#}</li> |
| 7143 | <li>{#syntax#}c_int{#endsyntax#}</li> |
| 7144 | <li>{#syntax#}c_uint{#endsyntax#}</li> |
| 7145 | <li>{#syntax#}c_long{#endsyntax#}</li> |
| 7146 | <li>{#syntax#}c_ulong{#endsyntax#}</li> |
| 7147 | <li>{#syntax#}c_longlong{#endsyntax#}</li> |
| 7148 | <li>{#syntax#}c_ulonglong{#endsyntax#}</li> |
| 7149 | <li>{#syntax#}c_longdouble{#endsyntax#}</li> |
| 7150 | <li>{#syntax#}c_void{#endsyntax#}</li> |
| 7148 | 7151 | </ul> |
| 7149 | 7152 | {#see_also|Primitive Types#} |
| 7150 | 7153 | {#header_close#} |
| ... | ... | @@ -7166,7 +7169,7 @@ pub fn main() void { |
| 7166 | 7169 | {#header_close#} |
| 7167 | 7170 | {#header_open|Import from C Header File#} |
| 7168 | 7171 | <p> |
| 7169 | | The <code>@cImport</code> builtin function can be used |
| 7172 | The {#syntax#}@cImport{#endsyntax#} builtin function can be used |
| 7170 | 7173 | to directly import symbols from .h files: |
| 7171 | 7174 | </p> |
| 7172 | 7175 | {#code_begin|exe#} |
| ... | ... | @@ -7181,7 +7184,7 @@ pub fn main() void { |
| 7181 | 7184 | } |
| 7182 | 7185 | {#code_end#} |
| 7183 | 7186 | <p> |
| 7184 | | The <code>@cImport</code> function takes an expression as a parameter. |
| 7187 | The {#syntax#}@cImport{#endsyntax#} function takes an expression as a parameter. |
| 7185 | 7188 | This expression is evaluated at compile-time and is used to control |
| 7186 | 7189 | preprocessor directives and include multiple .h files: |
| 7187 | 7190 | </p> |
| ... | ... | @@ -7205,7 +7208,7 @@ const c = @cImport({ |
| 7205 | 7208 | {#header_open|Exporting a C Library#} |
| 7206 | 7209 | <p> |
| 7207 | 7210 | One of the primary use cases for Zig is exporting a library with the C ABI for other programming languages |
| 7208 | | to call into. The <code>export</code> keyword in front of functions, variables, and types causes them to |
| 7211 | to call into. The {#syntax#}export{#endsyntax#} keyword in front of functions, variables, and types causes them to |
| 7209 | 7212 | be part of the library API: |
| 7210 | 7213 | </p> |
| 7211 | 7214 | <p class="file">mathtest.zig</p> |
| ... | ... | @@ -7454,7 +7457,7 @@ Environments: |
| 7454 | 7457 | coreclr |
| 7455 | 7458 | opencl</code></pre> |
| 7456 | 7459 | <p> |
| 7457 | | The Zig Standard Library (<code>@import("std")</code>) has architecture, environment, and operating sytsem |
| 7460 | The Zig Standard Library ({#syntax#}@import("std"){#endsyntax#}) has architecture, environment, and operating sytsem |
| 7458 | 7461 | abstractions, and thus takes additional work to support more platforms. |
| 7459 | 7462 | Not all standard library code requires operating system abstractions, however, |
| 7460 | 7463 | so things such as generic data structures work an all above platforms. |
| ... | ... | @@ -7491,25 +7494,25 @@ coding style. |
| 7491 | 7494 | {#header_close#} |
| 7492 | 7495 | {#header_open|Names#} |
| 7493 | 7496 | <p> |
| 7494 | | Roughly speaking: <code>camelCaseFunctionName</code>, <code>TitleCaseTypeName</code>, |
| 7495 | | <code>snake_case_variable_name</code>. More precisely: |
| 7497 | Roughly speaking: {#syntax#}camelCaseFunctionName{#endsyntax#}, {#syntax#}TitleCaseTypeName{#endsyntax#}, |
| 7498 | {#syntax#}snake_case_variable_name{#endsyntax#}. More precisely: |
| 7496 | 7499 | </p> |
| 7497 | 7500 | <ul> |
| 7498 | 7501 | <li> |
| 7499 | | If <code>x</code> is a <code>struct</code> (or an alias of a <code>struct</code>), |
| 7500 | | then <code>x</code> should be <code>TitleCase</code>. |
| 7502 | If {#syntax#}x{#endsyntax#} is a {#syntax#}struct{#endsyntax#} (or an alias of a {#syntax#}struct{#endsyntax#}), |
| 7503 | then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}. |
| 7501 | 7504 | </li> |
| 7502 | 7505 | <li> |
| 7503 | | If <code>x</code> otherwise identifies a type, <code>x</code> should have <code>snake_case</code>. |
| 7506 | If {#syntax#}x{#endsyntax#} otherwise identifies a type, {#syntax#}x{#endsyntax#} should have {#syntax#}snake_case{#endsyntax#}. |
| 7504 | 7507 | </li> |
| 7505 | 7508 | <li> |
| 7506 | | If <code>x</code> is callable, and <code>x</code>'s return type is <code>type</code>, then <code>x</code> should be <code>TitleCase</code>. |
| 7509 | If {#syntax#}x{#endsyntax#} is callable, and {#syntax#}x{#endsyntax#}'s return type is {#syntax#}type{#endsyntax#}, then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}. |
| 7507 | 7510 | </li> |
| 7508 | 7511 | <li> |
| 7509 | | If <code>x</code> is otherwise callable, then <code>x</code> should be <code>camelCase</code>. |
| 7512 | If {#syntax#}x{#endsyntax#} is otherwise callable, then {#syntax#}x{#endsyntax#} should be {#syntax#}camelCase{#endsyntax#}. |
| 7510 | 7513 | </li> |
| 7511 | 7514 | <li> |
| 7512 | | Otherwise, <code>x</code> should be <code>snake_case</code>. |
| 7515 | Otherwise, {#syntax#}x{#endsyntax#} should be {#syntax#}snake_case{#endsyntax#}. |
| 7513 | 7516 | </li> |
| 7514 | 7517 | </ul> |
| 7515 | 7518 | <p> |
| ... | ... | @@ -7521,7 +7524,7 @@ coding style. |
| 7521 | 7524 | <p> |
| 7522 | 7525 | These are general rules of thumb; if it makes sense to do something different, |
| 7523 | 7526 | do what makes sense. For example, if there is an established convention such as |
| 7524 | | <code>ENOENT</code>, follow the established convention. |
| 7527 | {#syntax#}ENOENT{#endsyntax#}, follow the established convention. |
| 7525 | 7528 | </p> |
| 7526 | 7529 | {#header_close#} |
| 7527 | 7530 | {#header_open|Examples#} |