authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-21 16:11:20-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-21 20:31:13-07:00
log559bbf1cc669501ff1f1f59d75577691d96450a0
tree4f7bbbd767186afaa7532fc3ef8d4d7ecf274042
parentfdb4eb3056ee85709f8d1af6c11641481de5e653

langref: explicitly mention inline combined with multiple cases

closes #18524

1 files changed, 16 insertions(+), 5 deletions(-)

doc/langref.html.in+16-5
......@@ -4311,10 +4311,11 @@ test "enum literals with switch" {
43114311 {#code_end#}
43124312 {#header_close#}
43134313
4314 {#header_open|Inline switch#}
4314 {#header_open|Inline Switch Prongs#}
43154315 <p>
43164316 Switch prongs can be marked as {#syntax#}inline{#endsyntax#} to generate
4317 the prong's body for each possible value it could have:
4317 the prong's body for each possible value it could have, making the
4318 captured value {#link|comptime#}.
43184319 </p>
43194320 {#code_begin|test|test_inline_switch#}
43204321const std = @import("std");
......@@ -4324,9 +4325,9 @@ const expectError = std.testing.expectError;
43244325fn isFieldOptional(comptime T: type, field_index: usize) !bool {
43254326 const fields = @typeInfo(T).Struct.fields;
43264327 return switch (field_index) {
4327 // This prong is analyzed `fields.len - 1` times with `idx` being a
4328 // unique comptime-known value each time.
4329 inline 0...fields.len - 1 => |idx| @typeInfo(fields[idx].type) == .Optional,
4328 // This prong is analyzed twice with `idx` being a
4329 // comptime-known value each time.
4330 inline 0, 1 => |idx| @typeInfo(fields[idx].type) == .Optional,
43304331 else => return error.IndexOutOfBounds,
43314332 };
43324333}
......@@ -4350,6 +4351,16 @@ fn isFieldOptionalUnrolled(field_index: usize) !bool {
43504351 1 => true,
43514352 else => return error.IndexOutOfBounds,
43524353 };
4354}
4355 {#code_end#}
4356 <p>The {#syntax#}inline{#endsyntax#} keyword may also be combined with ranges:</p>
4357 {#code_begin|syntax|inline_prong_range#}
4358fn isFieldOptional(comptime T: type, field_index: usize) !bool {
4359 const fields = @typeInfo(T).Struct.fields;
4360 return switch (field_index) {
4361 inline 0...fields.len - 1 => |idx| @typeInfo(fields[idx].type) == .Optional,
4362 else => return error.IndexOutOfBounds,
4363 };
43534364}
43544365 {#code_end#}
43554366 <p>