Reputation: 1369
I have an algorithm that has two arrays with their respective indices. Only, it would be very easy to mix both indices and access one of the arrays with the wrong index. For example:
var array1 = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var index1 = 7; // the 8th element in array1, fine because array1 has a length of 10
var array2 = [_]u8{ 1, 2, 3, 4, 5 };
var index2 = 2; // the 3rd element in array2, fine because array2 has a length of 5
array2[index1] // woops, I used the wrong index with the wrong array
Is there a way to make a custom index type to associate an array with a specific type of index such that indexing an array with the wrong type of index will result in a compile error? Such as:
var array1 = [_:Index1]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // with index type 'Index1'
var index1 : Index1 = 7; // index of type 'Index1'
var array2 = [_:Index2]u8{ 1, 2, 3, 4, 5 }; // with index type 'Index2'
var index2 : Index2 = 2; // index of type 'Index2'
array2[index1] // ERROR: array2 expects Index2 type index but Index1 type is used
Upvotes: 0
Views: 688
Reputation: 46
const std = @import("std");
fn PickyArray(comptime T: type, n: usize, extra: anytype) type {
_ = extra;
return struct {
const Self = @This();
pub const Index = struct {
idx: usize,
};
unsafe_items: [n]T,
pub fn init(xs: [n]T) Self {
return .{.unsafe_items = xs};
}
pub fn get(self: Self, idx: Index) T {
return self.unsafe_items[idx.idx];
}
pub fn set(self: *Self, idx: Index, v: T) void {
self.unsafe_items[idx.idx] = v;
}
pub fn getPtr(self: Self, idx: Index) *T {
return &self.unsafe_items[idx.idx];
}
pub fn getSlice(self: Self, lo: Index, hi: Index) []T {
return self.unsafe_items[lo.idx..hi.idx];
}
};
}
const Array1 = PickyArray(u8, 10, "Index1");
const Index1 = Array1.Index;
const Array2 = PickyArray(u8, 5, "Index2");
const Index2 = Array2.Index;
pub fn main() void {
var array1 = Array1.init([_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
var index1 : Index1 = .{.idx=7}; // index of type 'Index1'
var array2 = Array2.init([_]u8{ 1, 2, 3, 4, 5 }); // with index type 'Index2'
var index2 : Index2 = .{.idx=2}; // index of type 'Index2'
_ = array2.get(index1); // ERROR: array2 expects Index2 type index but Index1 type is used
_ = array1;
_ = index1;
_ = array2;
_ = index2;
}
For extra credit, the type returned by getSlice
should not be a normal slice and should refuse to accept integer indices.
Please do not do this.
Upvotes: 3