| ... | @@ -1295,13 +1295,39 @@ test "expectError demo" { | ... | @@ -1295,13 +1295,39 @@ test "expectError demo" { |
| 1295 | A variable is a unit of {#link|Memory#} storage. | 1295 | A variable is a unit of {#link|Memory#} storage. |
| 1296 | </p> | 1296 | </p> |
| 1297 | <p> | 1297 | <p> |
| 1298 | Variables are never allowed to shadow identifiers from an outer scope. | | |
| 1299 | </p> | | |
| 1300 | <p> | | |
| 1301 | It is generally preferable to use {#syntax#}const{#endsyntax#} rather than | 1298 | It is generally preferable to use {#syntax#}const{#endsyntax#} rather than |
| 1302 | {#syntax#}var{#endsyntax#} when declaring a variable. This causes less work for both | 1299 | {#syntax#}var{#endsyntax#} when declaring a variable. This causes less work for both |
| 1303 | humans and computers to do when reading code, and creates more optimization opportunities. | 1300 | humans and computers to do when reading code, and creates more optimization opportunities. |
| 1304 | </p> | 1301 | </p> |
| | 1302 | |
| | 1303 | {#header_open|Identifiers#} |
| | 1304 | <p> |
| | 1305 | Variable identifiers are never allowed to shadow identifiers from an outer scope. |
| | 1306 | </p> |
| | 1307 | <p> |
| | 1308 | Identifiers must start with an alphabetic character or underscore and may be followed |
| | 1309 | by any number of alphanumeric characters or underscores. |
| | 1310 | They must not overlap with any keywords. See {#link|Keyword Reference#}. |
| | 1311 | </p> |
| | 1312 | <p> |
| | 1313 | If a name that does not fit these requirements is needed, such as for linking with external libraries, the {#syntax#}@""{#endsyntax#} syntax may be used. |
| | 1314 | </p> |
| | 1315 | {#code_begin|syntax#} |
| | 1316 | const @"identifier with spaces in it" = 0xff; |
| | 1317 | const @"1SmallStep4Man" = 112358; |
| | 1318 | |
| | 1319 | const c = @import("std").c; |
| | 1320 | pub extern "c" fn @"error"() anyopaque; |
| | 1321 | pub extern "c" fn @"fstat$INODE64"(fd: c.fd_t, buf: *c.Stat) c_int; |
| | 1322 | |
| | 1323 | const Color = enum { |
| | 1324 | red, |
| | 1325 | @"really red", |
| | 1326 | }; |
| | 1327 | const color: Color = .@"really red"; |
| | 1328 | {#code_end#} |
| | 1329 | {#header_close#} |
| | 1330 | |
| 1305 | {#header_open|Container Level Variables#} | 1331 | {#header_open|Container Level Variables#} |
| 1306 | <p> | 1332 | <p> |
| 1307 | Container level variables have static lifetime and are order-independent and lazily analyzed. | 1333 | Container level variables have static lifetime and are order-independent and lazily analyzed. |
| ... | @@ -1486,7 +1512,7 @@ fn divide(a: i32, b: i32) i32 { | ... | @@ -1486,7 +1512,7 @@ fn divide(a: i32, b: i32) i32 { |
| 1486 | </p> | 1512 | </p> |
| 1487 | <p> | 1513 | <p> |
| 1488 | Operators such as {#syntax#}+{#endsyntax#} and {#syntax#}-{#endsyntax#} cause undefined behavior on | 1514 | Operators such as {#syntax#}+{#endsyntax#} and {#syntax#}-{#endsyntax#} cause undefined behavior on |
| 1489 | integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets. | 1515 | integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets. |
| 1490 | {#syntax#}+%{#endsyntax#} and {#syntax#}-%{#endsyntax#} perform wrapping arithmetic | 1516 | {#syntax#}+%{#endsyntax#} and {#syntax#}-%{#endsyntax#} perform wrapping arithmetic |
| 1491 | while {#syntax#}+|{#endsyntax#} and {#syntax#}-|{#endsyntax#} perform saturating arithmetic. | 1517 | while {#syntax#}+|{#endsyntax#} and {#syntax#}-|{#endsyntax#} perform saturating arithmetic. |
| 1492 | </p> | 1518 | </p> |
| ... | @@ -2494,32 +2520,32 @@ test "null terminated array" { | ... | @@ -2494,32 +2520,32 @@ test "null terminated array" { |
| 2494 | or using the shorthand function {#syntax#}std.meta.Vector{#endsyntax#}. | 2520 | or using the shorthand function {#syntax#}std.meta.Vector{#endsyntax#}. |
| 2495 | </p> | 2521 | </p> |
| 2496 | <p> | 2522 | <p> |
| 2497 | Vectors support the same builtin operators as their underlying base types. These operations are performed | 2523 | Vectors support the same builtin operators as their underlying base types. These operations are performed |
| 2498 | element-wise, and return a vector of the same length as the input vectors. This includes: | 2524 | element-wise, and return a vector of the same length as the input vectors. This includes: |
| 2499 | </p> | 2525 | </p> |
| 2500 | <ul> | 2526 | <ul> |
| 2501 | <li>Arithmetic ({#syntax#}+{#endsyntax#}, {#syntax#}-{#endsyntax#}, {#syntax#}/{#endsyntax#}, {#syntax#}*{#endsyntax#}, | 2527 | <li>Arithmetic ({#syntax#}+{#endsyntax#}, {#syntax#}-{#endsyntax#}, {#syntax#}/{#endsyntax#}, {#syntax#}*{#endsyntax#}, |
| 2502 | {#syntax#}@divFloor{#endsyntax#}, {#syntax#}@sqrt{#endsyntax#}, {#syntax#}@ceil{#endsyntax#}, | 2528 | {#syntax#}@divFloor{#endsyntax#}, {#syntax#}@sqrt{#endsyntax#}, {#syntax#}@ceil{#endsyntax#}, |
| 2503 | {#syntax#}@log{#endsyntax#}, etc.)</li> | 2529 | {#syntax#}@log{#endsyntax#}, etc.)</li> |
| 2504 | <li>Bitwise operators ({#syntax#}>>{#endsyntax#}, {#syntax#}<<{#endsyntax#}, {#syntax#}&{#endsyntax#}, | 2530 | <li>Bitwise operators ({#syntax#}>>{#endsyntax#}, {#syntax#}<<{#endsyntax#}, {#syntax#}&{#endsyntax#}, |
| 2505 | {#syntax#}|{#endsyntax#}, {#syntax#}~{#endsyntax#}, etc.)</li> | 2531 | {#syntax#}|{#endsyntax#}, {#syntax#}~{#endsyntax#}, etc.)</li> |
| 2506 | <li>Comparison operators ({#syntax#}<{#endsyntax#}, {#syntax#}>{#endsyntax#}, {#syntax#}=={#endsyntax#}, etc.)</li> | 2532 | <li>Comparison operators ({#syntax#}<{#endsyntax#}, {#syntax#}>{#endsyntax#}, {#syntax#}=={#endsyntax#}, etc.)</li> |
| 2507 | </ul> | 2533 | </ul> |
| 2508 | <p> | 2534 | <p> |
| 2509 | It is prohibited to use a math operator on a mixture of scalars (individual numbers) and vectors. | 2535 | It is prohibited to use a math operator on a mixture of scalars (individual numbers) and vectors. |
| 2510 | Zig provides the {#link|@splat#} builtin to easily convert from scalars to vectors, and it supports {#link|@reduce#} | 2536 | Zig provides the {#link|@splat#} builtin to easily convert from scalars to vectors, and it supports {#link|@reduce#} |
| 2511 | and array indexing syntax to convert from vectors to scalars. Vectors also support assignment to and from | 2537 | and array indexing syntax to convert from vectors to scalars. Vectors also support assignment to and from |
| 2512 | fixed-length arrays with comptime known length. | 2538 | fixed-length arrays with comptime known length. |
| 2513 | </p> | 2539 | </p> |
| 2514 | <p> | 2540 | <p> |
| 2515 | For rearranging elements within and between vectors, Zig provides the {#link|@shuffle#} and {#link|@select#} functions. | 2541 | For rearranging elements within and between vectors, Zig provides the {#link|@shuffle#} and {#link|@select#} functions. |
| 2516 | </p> | 2542 | </p> |
| 2517 | <p> | 2543 | <p> |
| 2518 | Operations on vectors shorter than the target machine's native SIMD size will typically compile to single SIMD | 2544 | Operations on vectors shorter than the target machine's native SIMD size will typically compile to single SIMD |
| 2519 | instructions, while vectors longer than the target machine's native SIMD size will compile to multiple SIMD | 2545 | instructions, while vectors longer than the target machine's native SIMD size will compile to multiple SIMD |
| 2520 | instructions. If a given operation doesn't have SIMD support on the target architecture, the compiler will default | 2546 | instructions. If a given operation doesn't have SIMD support on the target architecture, the compiler will default |
| 2521 | to operating on each vector element one at a time. Zig supports any comptime-known vector length up to 2^32-1, | 2547 | to operating on each vector element one at a time. Zig supports any comptime-known vector length up to 2^32-1, |
| 2522 | although small powers of two (2-64) are most typical. Note that excessively long vector lengths (e.g. 2^20) may | 2548 | although small powers of two (2-64) are most typical. Note that excessively long vector lengths (e.g. 2^20) may |
| 2523 | result in compiler crashes on current versions of Zig. | 2549 | result in compiler crashes on current versions of Zig. |
| 2524 | </p> | 2550 | </p> |
| 2525 | {#code_begin|test|vector_example#} | 2551 | {#code_begin|test|vector_example#} |
| ... | @@ -2569,7 +2595,7 @@ test "Conversion between vectors, arrays, and slices" { | ... | @@ -2569,7 +2595,7 @@ test "Conversion between vectors, arrays, and slices" { |
| 2569 | TODO consider suggesting std.MultiArrayList | 2595 | TODO consider suggesting std.MultiArrayList |
| 2570 | </p> | 2596 | </p> |
| 2571 | {#see_also|@splat|@shuffle|@select|@reduce#} | 2597 | {#see_also|@splat|@shuffle|@select|@reduce#} |
| 2572 | | 2598 | |
| 2573 | {#header_close#} | 2599 | {#header_close#} |
| 2574 | | 2600 | |
| 2575 | {#header_open|Pointers#} | 2601 | {#header_open|Pointers#} |
| ... | @@ -2987,8 +3013,8 @@ test "null terminated slice" { | ... | @@ -2987,8 +3013,8 @@ test "null terminated slice" { |
| 2987 | } | 3013 | } |
| 2988 | {#code_end#} | 3014 | {#code_end#} |
| 2989 | <p> | 3015 | <p> |
| 2990 | Sentinel-terminated slices can also be created using a variation of the slice syntax | 3016 | Sentinel-terminated slices can also be created using a variation of the slice syntax |
| 2991 | {#syntax#}data[start..end :x]{#endsyntax#}, where {#syntax#}data{#endsyntax#} is a many-item pointer, | 3017 | {#syntax#}data[start..end :x]{#endsyntax#}, where {#syntax#}data{#endsyntax#} is a many-item pointer, |
| 2992 | array or slice and {#syntax#}x{#endsyntax#} is the sentinel value. | 3018 | array or slice and {#syntax#}x{#endsyntax#} is the sentinel value. |
| 2993 | </p> | 3019 | </p> |
| 2994 | {#code_begin|test|null_terminated_slicing#} | 3020 | {#code_begin|test|null_terminated_slicing#} |
| ... | @@ -3005,7 +3031,7 @@ test "null terminated slicing" { | ... | @@ -3005,7 +3031,7 @@ test "null terminated slicing" { |
| 3005 | } | 3031 | } |
| 3006 | {#code_end#} | 3032 | {#code_end#} |
| 3007 | <p> | 3033 | <p> |
| 3008 | Sentinel-terminated slicing asserts that the element in the sentinel position of the backing data is | 3034 | Sentinel-terminated slicing asserts that the element in the sentinel position of the backing data is |
| 3009 | actually the sentinel value. If this is not the case, safety-protected {#link|Undefined Behavior#} results. | 3035 | actually the sentinel value. If this is not the case, safety-protected {#link|Undefined Behavior#} results. |
| 3010 | </p> | 3036 | </p> |
| 3011 | {#code_begin|test_safety|sentinel mismatch#} | 3037 | {#code_begin|test_safety|sentinel mismatch#} |
| ... | @@ -3014,10 +3040,10 @@ const expect = std.testing.expect; | ... | @@ -3014,10 +3040,10 @@ const expect = std.testing.expect; |
| 3014 | | 3040 | |
| 3015 | test "sentinel mismatch" { | 3041 | test "sentinel mismatch" { |
| 3016 | var array = [_]u8{ 3, 2, 1, 0 }; | 3042 | var array = [_]u8{ 3, 2, 1, 0 }; |
| 3017 | | 3043 | |
| 3018 | // Creating a sentinel-terminated slice from the array with a length of 2 | 3044 | // Creating a sentinel-terminated slice from the array with a length of 2 |
| 3019 | // will result in the value `1` occupying the sentinel element position. | 3045 | // will result in the value `1` occupying the sentinel element position. |
| 3020 | // This does not match the indicated sentinel value of `0` and will lead | 3046 | // This does not match the indicated sentinel value of `0` and will lead |
| 3021 | // to a runtime panic. | 3047 | // to a runtime panic. |
| 3022 | var runtime_length: usize = 2; | 3048 | var runtime_length: usize = 2; |
| 3023 | const slice = array[0..runtime_length :0]; | 3049 | const slice = array[0..runtime_length :0]; |
| ... | @@ -3165,7 +3191,7 @@ test "linked list" { | ... | @@ -3165,7 +3191,7 @@ test "linked list" { |
| 3165 | .last = &node, | 3191 | .last = &node, |
| 3166 | .len = 1, | 3192 | .len = 1, |
| 3167 | }; | 3193 | }; |
| 3168 | | 3194 | |
| 3169 | // When using a pointer to a struct, fields can be accessed directly, | 3195 | // When using a pointer to a struct, fields can be accessed directly, |
| 3170 | // without explicitly dereferencing the pointer. | 3196 | // without explicitly dereferencing the pointer. |
| 3171 | // So you can do | 3197 | // So you can do |
| ... | @@ -3497,7 +3523,7 @@ fn dump(args: anytype) !void { | ... | @@ -3497,7 +3523,7 @@ fn dump(args: anytype) !void { |
| 3497 | </p> | 3523 | </p> |
| 3498 | <p> | 3524 | <p> |
| 3499 | The fields are implicitly named using numbers starting from 0. Because their names are integers, | 3525 | The fields are implicitly named using numbers starting from 0. Because their names are integers, |
| 3500 | the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as identifiers. | 3526 | the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as {#link|identifiers|Identifiers#}. |
| 3501 | </p> | 3527 | </p> |
| 3502 | <p> | 3528 | <p> |
| 3503 | Like arrays, tuples have a .len field, can be indexed and work with the ++ and ** operators. They can also be iterated over with {#link|inline for#}. | 3529 | Like arrays, tuples have a .len field, can be indexed and work with the ++ and ** operators. They can also be iterated over with {#link|inline for#}. |
| ... | @@ -3986,7 +4012,7 @@ test "labeled break from labeled block expression" { | ... | @@ -3986,7 +4012,7 @@ test "labeled break from labeled block expression" { |
| 3986 | {#see_also|Labeled while|Labeled for#} | 4012 | {#see_also|Labeled while|Labeled for#} |
| 3987 | | 4013 | |
| 3988 | {#header_open|Shadowing#} | 4014 | {#header_open|Shadowing#} |
| 3989 | <p>Identifiers are never allowed to "hide" other identifiers by using the same name:</p> | 4015 | <p>{#link|Identifiers#} are never allowed to "hide" other identifiers by using the same name:</p> |
| 3990 | {#code_begin|test_err|local shadows declaration#} | 4016 | {#code_begin|test_err|local shadows declaration#} |
| 3991 | const pi = 3.14; | 4017 | const pi = 3.14; |
| 3992 | | 4018 | |
| ... | @@ -3998,8 +4024,8 @@ test "inside test block" { | ... | @@ -3998,8 +4024,8 @@ test "inside test block" { |
| 3998 | } | 4024 | } |
| 3999 | {#code_end#} | 4025 | {#code_end#} |
| 4000 | <p> | 4026 | <p> |
| 4001 | Because of this, when you read Zig code you can always rely on an identifier to consistently mean | 4027 | Because of this, when you read Zig code you can always rely on an identifier to consistently mean |
| 4002 | the same thing within the scope it is defined. Note that you can, however, use the same name if | 4028 | the same thing within the scope it is defined. Note that you can, however, use the same name if |
| 4003 | the scopes are separate: | 4029 | the scopes are separate: |
| 4004 | </p> | 4030 | </p> |
| 4005 | {#code_begin|test|test_scopes#} | 4031 | {#code_begin|test|test_scopes#} |
| ... | @@ -4037,7 +4063,7 @@ test "switch simple" { | ... | @@ -4037,7 +4063,7 @@ test "switch simple" { |
| 4037 | 1, 2, 3 => 0, | 4063 | 1, 2, 3 => 0, |
| 4038 | | 4064 | |
| 4039 | // Ranges can be specified using the ... syntax. These are inclusive | 4065 | // Ranges can be specified using the ... syntax. These are inclusive |
| 4040 | // both ends. | 4066 | // of both ends. |
| 4041 | 5...100 => 1, | 4067 | 5...100 => 1, |
| 4042 | | 4068 | |
| 4043 | // Branches can be arbitrarily complex. | 4069 | // Branches can be arbitrarily complex. |
| ... | @@ -4809,7 +4835,7 @@ test "errdefer unwinding" { | ... | @@ -4809,7 +4835,7 @@ test "errdefer unwinding" { |
| 4809 | </p> | 4835 | </p> |
| 4810 | {#header_open|Basics#} | 4836 | {#header_open|Basics#} |
| 4811 | {#code_begin|test|test_unreachable#} | 4837 | {#code_begin|test|test_unreachable#} |
| 4812 | // unreachable is used to assert that control flow will never happen upon a | 4838 | // unreachable is used to assert that control flow will never reach a |
| 4813 | // particular location: | 4839 | // particular location: |
| 4814 | test "basic math" { | 4840 | test "basic math" { |
| 4815 | const x = 1; | 4841 | const x = 1; |
| ... | @@ -6777,8 +6803,7 @@ test "variable values" { | ... | @@ -6777,8 +6803,7 @@ test "variable values" { |
| 6777 | generic data structure. | 6803 | generic data structure. |
| 6778 | </p> | 6804 | </p> |
| 6779 | <p> | 6805 | <p> |
| 6780 | Here is an example of a generic {#syntax#}List{#endsyntax#} data structure, that we will instantiate with | 6806 | 			Here is an example of a generic {#syntax#}List{#endsyntax#} data structure. |
| 6781 | the type {#syntax#}i32{#endsyntax#}. In Zig we refer to the type as {#syntax#}List(i32){#endsyntax#}. | | |
| 6782 | </p> | 6807 | </p> |
| 6783 | {#code_begin|syntax#} | 6808 | {#code_begin|syntax#} |
| 6784 | fn List(comptime T: type) type { | 6809 | fn List(comptime T: type) type { |
| ... | @@ -6787,27 +6812,46 @@ fn List(comptime T: type) type { | ... | @@ -6787,27 +6812,46 @@ fn List(comptime T: type) type { |
| 6787 | len: usize, | 6812 | len: usize, |
| 6788 | }; | 6813 | }; |
| 6789 | } | 6814 | } |
| | 6815 | |
| | 6816 | // The generic List data structure can be instantiated by passing in a type: |
| | 6817 | var buffer: [10]i32 = undefined; |
| | 6818 | var list = List(i32){ |
| | 6819 | .items = &buffer, |
| | 6820 | .len = 0, |
| | 6821 | }; |
| 6790 | {#code_end#} | 6822 | {#code_end#} |
| 6791 | <p> | 6823 | <p> |
| 6792 | That's it. It's a function that returns an anonymous {#syntax#}struct{#endsyntax#}. For the purposes of error messages | 6824 | That's it. It's a function that returns an anonymous {#syntax#}struct{#endsyntax#}. |
| 6793 | and debugging, Zig infers the name {#syntax#}"List(i32)"{#endsyntax#} from the function name and parameters invoked when creating | 6825 | To keep the language small and uniform, all aggregate types in Zig are anonymous. |
| | 6826 | For the purposes of error messages and debugging, Zig infers the name |
| | 6827 | {#syntax#}"List(i32)"{#endsyntax#} from the function name and parameters invoked when creating |
| 6794 | the anonymous struct. | 6828 | the anonymous struct. |
| 6795 | </p> | 6829 | </p> |
| 6796 | <p> | 6830 | <p> |
| 6797 | To keep the language small and uniform, all aggregate types in Zig are anonymous. To give a type | 6831 | To explicitly give a type a name, we assign it to a constant. |
| 6798 | a name, we assign it to a constant: | | |
| 6799 | </p> | 6832 | </p> |
| 6800 | {#code_begin|syntax#} | 6833 | {#code_begin|syntax#} |
| 6801 | const Node = struct { | 6834 | const Node = struct { |
| 6802 | next: *Node, | 6835 | next: ?*Node, |
| 6803 | name: []u8, | 6836 | name: []const u8, |
| | 6837 | }; |
| | 6838 | |
| | 6839 | var node_a = Node{ |
| | 6840 | .next = null, |
| | 6841 | .name = &"Node A", |
| | 6842 | }; |
| | 6843 | |
| | 6844 | var node_b = Node{ |
| | 6845 | .next = &node_a, |
| | 6846 | .name = &"Node B", |
| 6804 | }; | 6847 | }; |
| 6805 | {#code_end#} | 6848 | {#code_end#} |
| 6806 | <p> | 6849 | <p> |
| 6807 | This works because all top level declarations are order-independent, and as long as there isn't | 6850 | In this example, the {#syntax#}Node{#endsyntax#} struct refers to itself. |
| 6808 | an actual infinite regression, values can refer to themselves, directly or indirectly. In this case, | 6851 | This works because all top level declarations are order-independent. |
| 6809 | {#syntax#}Node{#endsyntax#} refers to itself as a pointer, which is not actually an infinite regression, so | 6852 | As long as the compiler can determine the size of the struct, it is free to refer to itself. |
| 6810 | it works fine. | 6853 | In this case, {#syntax#}Node{#endsyntax#} refers to itself as a pointer, which has a |
| | 6854 | well-defined size at compile time, so it works fine. |
| 6811 | </p> | 6855 | </p> |
| 6812 | {#header_close#} | 6856 | {#header_close#} |
| 6813 | {#header_open|Case Study: print in Zig#} | 6857 | {#header_open|Case Study: print in Zig#} |
| ... | @@ -7220,10 +7264,10 @@ test "global assembly" { | ... | @@ -7220,10 +7264,10 @@ test "global assembly" { |
| 7220 | provided explicitly by the caller, and it can be suspended and resumed any number of times. | 7264 | provided explicitly by the caller, and it can be suspended and resumed any number of times. |
| 7221 | </p> | 7265 | </p> |
| 7222 | <p> | 7266 | <p> |
| 7223 | The code following the {#syntax#}async{#endsyntax#} callsite runs immediately after the async | 7267 | The code following the {#syntax#}async{#endsyntax#} callsite runs immediately after the async |
| 7224 | function first suspends. When the return value of the async function is needed, | 7268 | function first suspends. When the return value of the async function is needed, |
| 7225 | the calling code can {#syntax#}await{#endsyntax#} on the async function frame. | 7269 | the calling code can {#syntax#}await{#endsyntax#} on the async function frame. |
| 7226 | This will suspend the calling code until the async function completes, at which point | 7270 | This will suspend the calling code until the async function completes, at which point |
| 7227 | execution resumes just after the {#syntax#}await{#endsyntax#} callsite. | 7271 | execution resumes just after the {#syntax#}await{#endsyntax#} callsite. |
| 7228 | </p> | 7272 | </p> |
| 7229 | <p> | 7273 | <p> |
| ... | @@ -7333,8 +7377,8 @@ fn testResumeFromSuspend(my_result: *i32) void { | ... | @@ -7333,8 +7377,8 @@ fn testResumeFromSuspend(my_result: *i32) void { |
| 7333 | in standard code. | 7377 | in standard code. |
| 7334 | </p> | 7378 | </p> |
| 7335 | <p> | 7379 | <p> |
| 7336 | However, it is possible to have an {#syntax#}async{#endsyntax#} call | 7380 | However, it is possible to have an {#syntax#}async{#endsyntax#} call |
| 7337 | without a matching {#syntax#}await{#endsyntax#}. Upon completion of the async function, | 7381 | without a matching {#syntax#}await{#endsyntax#}. Upon completion of the async function, |
| 7338 | execution would continue at the most recent {#syntax#}async{#endsyntax#} callsite or {#syntax#}resume{#endsyntax#} callsite, | 7382 | execution would continue at the most recent {#syntax#}async{#endsyntax#} callsite or {#syntax#}resume{#endsyntax#} callsite, |
| 7339 | and the return value of the async function would be lost. | 7383 | and the return value of the async function would be lost. |
| 7340 | </p> | 7384 | </p> |
| ... | @@ -7371,8 +7415,8 @@ fn func() void { | ... | @@ -7371,8 +7415,8 @@ fn func() void { |
| 7371 | </p> | 7415 | </p> |
| 7372 | <p> | 7416 | <p> |
| 7373 | {#syntax#}await{#endsyntax#} is a suspend point, and takes as an operand anything that | 7417 | {#syntax#}await{#endsyntax#} is a suspend point, and takes as an operand anything that |
| 7374 | coerces to {#syntax#}anyframe->T{#endsyntax#}. Calling {#syntax#}await{#endsyntax#} on | 7418 | coerces to {#syntax#}anyframe->T{#endsyntax#}. Calling {#syntax#}await{#endsyntax#} on |
| 7375 | the frame of an async function will cause execution to continue at the | 7419 | the frame of an async function will cause execution to continue at the |
| 7376 | {#syntax#}await{#endsyntax#} callsite once the target function completes. | 7420 | {#syntax#}await{#endsyntax#} callsite once the target function completes. |
| 7377 | </p> | 7421 | </p> |
| 7378 | <p> | 7422 | <p> |
| ... | @@ -8297,8 +8341,8 @@ fn internalName() callconv(.C) void {} | ... | @@ -8297,8 +8341,8 @@ fn internalName() callconv(.C) void {} |
| 8297 | {#code_begin|obj#} | 8341 | {#code_begin|obj#} |
| 8298 | export fn foo() void {} | 8342 | export fn foo() void {} |
| 8299 | {#code_end#} | 8343 | {#code_end#} |
| 8300 | <p>Note that even when using {#syntax#}export{#endsyntax#}, {#syntax#}@"foo"{#endsyntax#} syntax can | 8344 | <p>Note that even when using {#syntax#}export{#endsyntax#}, the {#syntax#}@"foo"{#endsyntax#} syntax for |
| 8301 | be used to choose any string for the symbol name:</p> | 8345 | {#link|identifiers|Identifiers#} can be used to choose any string for the symbol name:</p> |
| 8302 | {#code_begin|obj#} | 8346 | {#code_begin|obj#} |
| 8303 | export fn @"A function name that is a complete sentence."() void {} | 8347 | export fn @"A function name that is a complete sentence."() void {} |
| 8304 | {#code_end#} | 8348 | {#code_end#} |
| ... | @@ -8597,7 +8641,9 @@ test "integer cast panic" { | ... | @@ -8597,7 +8641,9 @@ test "integer cast panic" { |
| 8597 | {#header_open|@intToPtr#} | 8641 | {#header_open|@intToPtr#} |
| 8598 | <pre>{#syntax#}@intToPtr(comptime DestType: type, address: usize) DestType{#endsyntax#}</pre> | 8642 | <pre>{#syntax#}@intToPtr(comptime DestType: type, address: usize) DestType{#endsyntax#}</pre> |
| 8599 | <p> | 8643 | <p> |
| 8600 | Converts an integer to a {#link|pointer|Pointers#}. To convert the other way, use {#link|@ptrToInt#}. | 8644 | Converts an integer to a {#link|pointer|Pointers#}. To convert the other way, use {#link|@ptrToInt#}. Casting an address of 0 to a destination type |
| | 8645 | which in not {#link|optional|Optional Pointers#} and does not have the {#syntax#}allowzero{#endsyntax#} attribute will result in a |
| | 8646 | {#link|Pointer Cast Invalid Null#} panic when runtime safety checks are enabled. |
| 8601 | </p> | 8647 | </p> |
| 8602 | <p> | 8648 | <p> |
| 8603 | If the destination pointer type does not allow address zero and {#syntax#}address{#endsyntax#} | 8649 | If the destination pointer type does not allow address zero and {#syntax#}address{#endsyntax#} |
| ... | @@ -8711,7 +8757,8 @@ test "@wasmMemoryGrow" { | ... | @@ -8711,7 +8757,8 @@ test "@wasmMemoryGrow" { |
| 8711 | <pre>{#syntax#}@mod(numerator: T, denominator: T) T{#endsyntax#}</pre> | 8757 | <pre>{#syntax#}@mod(numerator: T, denominator: T) T{#endsyntax#}</pre> |
| 8712 | <p> | 8758 | <p> |
| 8713 | Modulus division. For unsigned integers this is the same as | 8759 | Modulus division. For unsigned integers this is the same as |
| 8714 | {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}. | 8760 | {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}, otherwise the |
| | 8761 | operation will result in a {#link|Remainder Division by Zero#} when runtime safety checks are enabled. |
| 8715 | </p> | 8762 | </p> |
| 8716 | <ul> | 8763 | <ul> |
| 8717 | <li>{#syntax#}@mod(-5, 3) == 1{#endsyntax#}</li> | 8764 | <li>{#syntax#}@mod(-5, 3) == 1{#endsyntax#}</li> |
| ... | @@ -8729,7 +8776,7 @@ test "@wasmMemoryGrow" { | ... | @@ -8729,7 +8776,7 @@ test "@wasmMemoryGrow" { |
| 8729 | If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}. | 8776 | If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}. |
| 8730 | </p> | 8777 | </p> |
| 8731 | {#header_close#} | 8778 | {#header_close#} |
| 8732 | | 8779 | |
| 8733 | {#header_open|@panic#} | 8780 | {#header_open|@panic#} |
| 8734 | <pre>{#syntax#}@panic(message: []const u8) noreturn{#endsyntax#}</pre> | 8781 | <pre>{#syntax#}@panic(message: []const u8) noreturn{#endsyntax#}</pre> |
| 8735 | <p> | 8782 | <p> |
| ... | @@ -8836,7 +8883,8 @@ pub const PrefetchOptions = struct { | ... | @@ -8836,7 +8883,8 @@ pub const PrefetchOptions = struct { |
| 8836 | <pre>{#syntax#}@rem(numerator: T, denominator: T) T{#endsyntax#}</pre> | 8883 | <pre>{#syntax#}@rem(numerator: T, denominator: T) T{#endsyntax#}</pre> |
| 8837 | <p> | 8884 | <p> |
| 8838 | Remainder division. For unsigned integers this is the same as | 8885 | Remainder division. For unsigned integers this is the same as |
| 8839 | {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}. | 8886 | {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}, otherwise the |
| | 8887 | operation will result in a {#link|Remainder Division by Zero#} when runtime safety checks are enabled. |
| 8840 | </p> | 8888 | </p> |
| 8841 | <ul> | 8889 | <ul> |
| 8842 | <li>{#syntax#}@rem(-5, 3) == -2{#endsyntax#}</li> | 8890 | <li>{#syntax#}@rem(-5, 3) == -2{#endsyntax#}</li> |
| ... | @@ -8878,14 +8926,14 @@ pub const PrefetchOptions = struct { | ... | @@ -8878,14 +8926,14 @@ pub const PrefetchOptions = struct { |
| 8878 | {#header_close#} | 8926 | {#header_close#} |
| 8879 | | 8927 | |
| 8880 | {#header_open|@setCold#} | 8928 | {#header_open|@setCold#} |
| 8881 | <pre>{#syntax#}@setCold(is_cold: bool){#endsyntax#}</pre> | 8929 | <pre>{#syntax#}@setCold(comptime is_cold: bool){#endsyntax#}</pre> |
| 8882 | <p> | 8930 | <p> |
| 8883 | Tells the optimizer that a function is rarely called. | 8931 | Tells the optimizer that a function is rarely called. |
| 8884 | </p> | 8932 | </p> |
| 8885 | {#header_close#} | 8933 | {#header_close#} |
| 8886 | | 8934 | |
| 8887 | {#header_open|@setEvalBranchQuota#} | 8935 | {#header_open|@setEvalBranchQuota#} |
| 8888 | <pre>{#syntax#}@setEvalBranchQuota(new_quota: u32){#endsyntax#}</pre> | 8936 | <pre>{#syntax#}@setEvalBranchQuota(comptime new_quota: u32){#endsyntax#}</pre> |
| 8889 | <p> | 8937 | <p> |
| 8890 | Changes the maximum number of backwards branches that compile-time code | 8938 | Changes the maximum number of backwards branches that compile-time code |
| 8891 | execution can use before giving up and making a compile error. | 8939 | execution can use before giving up and making a compile error. |
| ... | @@ -8920,7 +8968,7 @@ test "foo" { | ... | @@ -8920,7 +8968,7 @@ test "foo" { |
| 8920 | {#header_close#} | 8968 | {#header_close#} |
| 8921 | | 8969 | |
| 8922 | {#header_open|@setFloatMode#} | 8970 | {#header_open|@setFloatMode#} |
| 8923 | <pre>{#syntax#}@setFloatMode(mode: @import("std").builtin.FloatMode){#endsyntax#}</pre> | 8971 | <pre>{#syntax#}@setFloatMode(comptime mode: @import("std").builtin.FloatMode){#endsyntax#}</pre> |
| 8924 | <p> | 8972 | <p> |
| 8925 | Sets the floating point mode of the current scope. Possible values are: | 8973 | Sets the floating point mode of the current scope. Possible values are: |
| 8926 | </p> | 8974 | </p> |
| ... | @@ -8955,7 +9003,7 @@ pub const FloatMode = enum { | ... | @@ -8955,7 +9003,7 @@ pub const FloatMode = enum { |
| 8955 | {#header_close#} | 9003 | {#header_close#} |
| 8956 | | 9004 | |
| 8957 | {#header_open|@setRuntimeSafety#} | 9005 | {#header_open|@setRuntimeSafety#} |
| 8958 | <pre>{#syntax#}@setRuntimeSafety(safety_on: bool) void{#endsyntax#}</pre> | 9006 | <pre>{#syntax#}@setRuntimeSafety(comptime safety_on: bool) void{#endsyntax#}</pre> |
| 8959 | <p> | 9007 | <p> |
| 8960 | Sets whether runtime safety checks are enabled for the scope that contains the function call. | 9008 | Sets whether runtime safety checks are enabled for the scope that contains the function call. |
| 8961 | </p> | 9009 | </p> |
| ... | @@ -9016,7 +9064,7 @@ test "@setRuntimeSafety" { | ... | @@ -9016,7 +9064,7 @@ test "@setRuntimeSafety" { |
| 9016 | </p> | 9064 | </p> |
| 9017 | {#see_also|@shlExact|@shrExact#} | 9065 | {#see_also|@shlExact|@shrExact#} |
| 9018 | {#header_close#} | 9066 | {#header_close#} |
| 9019 | | 9067 | |
| 9020 | {#header_open|@shrExact#} | 9068 | {#header_open|@shrExact#} |
| 9021 | <pre>{#syntax#}@shrExact(value: T, shift_amt: Log2T) T{#endsyntax#}</pre> | 9069 | <pre>{#syntax#}@shrExact(value: T, shift_amt: Log2T) T{#endsyntax#}</pre> |
| 9022 | <p> | 9070 | <p> |
| ... | @@ -9347,7 +9395,7 @@ fn doTheTest() !void { | ... | @@ -9347,7 +9395,7 @@ fn doTheTest() !void { |
| 9347 | If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}. | 9395 | If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}. |
| 9348 | </p> | 9396 | </p> |
| 9349 | {#header_close#} | 9397 | {#header_close#} |
| 9350 | | 9398 | |
| 9351 | {#header_open|@tagName#} | 9399 | {#header_open|@tagName#} |
| 9352 | <pre>{#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}</pre> | 9400 | <pre>{#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}</pre> |
| 9353 | <p> | 9401 | <p> |