Skip to content

Commit 696ef0b

Browse files
committedJul 10, 2018
langref: docs for union safety
·
0.15.20.3.0
1 parent 28f9230 commit 696ef0b

File tree

1 file changed

+84
-4
lines changed

1 file changed

+84
-4
lines changed
 

‎doc/langref.html.in‎

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6665,6 +6665,8 @@ comptime {
66656665
{#code_end#}
66666666
<p>At runtime:</p>
66676667
{#code_begin|exe_err#}
6668+
const std = @import("std");
6669+
66686670
const Set1 = error{
66696671
A,
66706672
B,
@@ -6674,10 +6676,11 @@ const Set2 = error{
66746676
C,
66756677
};
66766678
pub fn main() void {
6677-
_ = foo(Set1.B);
6679+
foo(Set1.B);
66786680
}
6679-
fn foo(set1: Set1) Set2 {
6680-
return @errSetCast(Set2, set1);
6681+
fn foo(set1: Set1) void {
6682+
const x = @errSetCast(Set2, set1);
6683+
std.debug.warn("value: {}\n", x);
66816684
}
66826685
{#code_end#}
66836686
{#header_close#}
@@ -6705,7 +6708,84 @@ fn foo(bytes: []u8) u32 {
67056708
{#code_end#}
67066709
{#header_close#}
67076710
{#header_open|Wrong Union Field Access#}
6708-
<p>TODO</p>
6711+
<p>At compile-time:</p>
6712+
{#code_begin|test_err|accessing union field 'float' while field 'int' is set#}
6713+
comptime {
6714+
var f = Foo{ .int = 42 };
6715+
f.float = 12.34;
6716+
}
6717+
6718+
const Foo = union {
6719+
float: f32,
6720+
int: u32,
6721+
};
6722+
{#code_end#}
6723+
<p>At runtime:</p>
6724+
{#code_begin|exe_err#}
6725+
const std = @import("std");
6726+
6727+
const Foo = union {
6728+
float: f32,
6729+
int: u32,
6730+
};
6731+
6732+
pub fn main() void {
6733+
var f = Foo{ .int = 42 };
6734+
bar(&f);
6735+
}
6736+
6737+
fn bar(f: *Foo) void {
6738+
f.float = 12.34;
6739+
std.debug.warn("value: {}\n", f.float);
6740+
}
6741+
{#code_end#}
6742+
<p>
6743+
This safety is not available for <code>extern</code> or <code>packed</code> unions.
6744+
</p>
6745+
<p>
6746+
To change the active field of a union, assign the entire union, like this:
6747+
</p>
6748+
{#code_begin|exe#}
6749+
const std = @import("std");
6750+
6751+
const Foo = union {
6752+
float: f32,
6753+
int: u32,
6754+
};
6755+
6756+
pub fn main() void {
6757+
var f = Foo{ .int = 42 };
6758+
bar(&f);
6759+
}
6760+
6761+
fn bar(f: *Foo) void {
6762+
f.* = Foo{ .float = 12.34 };
6763+
std.debug.warn("value: {}\n", f.float);
6764+
}
6765+
{#code_end#}
6766+
<p>
6767+
To change the active field of a union when a meaningful value for the field is not known,
6768+
use {#link|undefined#}, like this:
6769+
</p>
6770+
{#code_begin|exe#}
6771+
const std = @import("std");
6772+
6773+
const Foo = union {
6774+
float: f32,
6775+
int: u32,
6776+
};
6777+
6778+
pub fn main() void {
6779+
var f = Foo{ .int = 42 };
6780+
f = Foo{ .float = undefined };
6781+
bar(&f);
6782+
std.debug.warn("value: {}\n", f.float);
6783+
}
6784+
6785+
fn bar(f: *Foo) void {
6786+
f.float = 12.34;
6787+
}
6788+
{#code_end#}
67096789
{#header_close#}
67106790

67116791
{#header_open|Out of Bounds Float To Integer Cast#}

0 commit comments

Comments
 (0)
Please sign in to comment.