slice and array re-work plus some misc. changes
* `@truncate` builtin allows casting to the same size integer. It also performs two's complement casting between signed and unsigned integers. * The idiomatic way to convert between bytes and numbers is now `mem.readInt` and `mem.writeInt` instead of an unsafe cast. It works at compile time, is safer, and looks cleaner. * Implicitly casting an array to a slice is allowed only if the slice is const. * Constant pointer values know if their memory is from a compile- time constant value or a compile-time variable. * Cast from [N]u8 to []T no longer allowed, but [N]u8 to []const T still allowed. * Fix inability to pass a mutable pointer to comptime variable at compile-time to a function and have the function modify the memory pointed to by the pointer. * Add the `comptime T: type` parameter back to mem.eql. Prevents accidentally creating instantiations for arrays.
This commit is contained in:
@@ -23,7 +23,7 @@ fn arrays() {
|
||||
assert(accumulator == 15);
|
||||
assert(getArrayLen(array) == 5);
|
||||
}
|
||||
fn getArrayLen(a: []u32) -> usize {
|
||||
fn getArrayLen(a: []const u32) -> usize {
|
||||
a.len
|
||||
}
|
||||
|
||||
@@ -61,12 +61,12 @@ const some_array = []u8 {0, 1, 2, 3};
|
||||
fn nestedArrays() {
|
||||
@setFnTest(this);
|
||||
|
||||
const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
|
||||
const array_of_strings = [][]const u8 {"hello", "this", "is", "my", "thing"};
|
||||
for (array_of_strings) |s, i| {
|
||||
if (i == 0) assert(mem.eql(s, "hello"));
|
||||
if (i == 1) assert(mem.eql(s, "this"));
|
||||
if (i == 2) assert(mem.eql(s, "is"));
|
||||
if (i == 3) assert(mem.eql(s, "my"));
|
||||
if (i == 4) assert(mem.eql(s, "thing"));
|
||||
if (i == 0) assert(mem.eql(u8, s, "hello"));
|
||||
if (i == 1) assert(mem.eql(u8, s, "this"));
|
||||
if (i == 2) assert(mem.eql(u8, s, "is"));
|
||||
if (i == 3) assert(mem.eql(u8, s, "my"));
|
||||
if (i == 4) assert(mem.eql(u8, s, "thing"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user