diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-06-13 22:40:38 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-06-16 19:36:33 -0400 |
| commit | 59b3dc8907f76b93caa689732e878a5bfa2f65c2 (patch) | |
| tree | ced7464c4d054ebeaf413e46454b8dc820b1249e /doc | |
| parent | a7d59086b49b0ae11a4830d2ea72b63be05fab94 (diff) | |
| download | zig-59b3dc8907f76b93caa689732e878a5bfa2f65c2.tar.gz zig-59b3dc8907f76b93caa689732e878a5bfa2f65c2.zip | |
allow passing by non-copying value
closes #733
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/langref.html.in | 37 |
1 files changed, 14 insertions, 23 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in index 814de721a6..b32c8165e2 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2797,39 +2797,30 @@ fn foo() void { } {#code_end#} {#header_open|Pass-by-value Parameters#} <p> - In Zig, structs, unions, and enums with payloads cannot be passed by value - to a function. + In Zig, structs, unions, and enums with payloads can be passed directly to a function: </p> - {#code_begin|test_err|not copyable; cannot pass by value#} -const Foo = struct { + {#code_begin|test#} +const Point = struct { x: i32, + y: i32, }; -fn bar(foo: Foo) void {} - -test "pass aggregate type by value to function" { - bar(Foo {.x = 12,}); +fn foo(point: Point) i32 { + return point.x + point.y; } - {#code_end#} - <p> - Instead, one must use <code>*const</code>. Zig allows implicitly casting something - to a const pointer to it: - </p> - {#code_begin|test#} -const Foo = struct { - x: i32, -}; -fn bar(foo: *const Foo) void {} +const assert = @import("std").debug.assert; -test "implicitly cast to const pointer" { - bar(Foo {.x = 12,}); +test "pass aggregate type by non-copy value to function" { + assert(foo(Point{ .x = 1, .y = 2 }) == 3); } {#code_end#} <p> - However, - the C ABI does allow passing structs and unions by value. So functions which - use the C calling convention may pass structs and unions by value. + In this case, the value may be passed by reference, or by value, whichever way + Zig decides will be faster. + </p> + <p> + For extern functions, Zig follows the C ABI for passing structs and unions by value. </p> {#header_close#} {#header_open|Function Reflection#} |
