single pointer slice syntax added

This commit is contained in:
ssmid 2024-07-17 16:06:34 +02:00 committed by Andrew Kelley
parent 01dc0d5a72
commit 9232425b8f
2 changed files with 15 additions and 0 deletions

View file

@ -1935,6 +1935,7 @@ or
<li>{#syntax#}*T{#endsyntax#} - single-item pointer to exactly one item. <li>{#syntax#}*T{#endsyntax#} - single-item pointer to exactly one item.
<ul> <ul>
<li>Supports deref syntax: {#syntax#}ptr.*{#endsyntax#}</li> <li>Supports deref syntax: {#syntax#}ptr.*{#endsyntax#}</li>
<li>Supports slice syntax: {#syntax#}ptr[0..1]{#endsyntax#}</li>
<li>Supports pointer subtraction: {#syntax#}ptr - ptr{#endsyntax#}</li> <li>Supports pointer subtraction: {#syntax#}ptr - ptr{#endsyntax#}</li>
</ul> </ul>
</li> </li>

View file

@ -32,4 +32,18 @@ test "pointer array access" {
try expect(array[2] == 4); try expect(array[2] == 4);
} }
test "slice syntax" {
// Get a pointer to a variable:
var x: i32 = 1234;
const x_ptr = &x;
// Convert to array pointer using slice syntax:
const x_array_ptr = x_ptr[0..1];
try expect(@TypeOf(x_array_ptr) == *[1]i32);
// Coerce to many-item pointer:
const x_many_ptr: [*]i32 = x_array_ptr;
try expect(x_many_ptr[0] == 1234);
}
// test // test