Skip to content

Commit 559bbf1

Browse files
committed
langref: explicitly mention inline combined with multiple cases
closes #18524
1 parent fdb4eb3 commit 559bbf1

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

doc/langref.html.in

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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#}
43204321
const std = @import("std");
@@ -4324,9 +4325,9 @@ const expectError = std.testing.expectError;
43244325
fn 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#}
4358+
fn 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>

0 commit comments

Comments
 (0)