Why can’t I perform this cast?
How do I perform this cast?
test "double pointer cast to anyopaque" {
var my_strings: []const []const u8 = &.{ "hello", "world" };
const my_ptr: ?*anyopaque = &my_strings;
_ = my_ptr;
}
src/argparser/test.zig:288:33: error: expected type '?*anyopaque', found '*[]const []const u8'
const my_ptr: ?*anyopaque = &my_strings;
^~~~~~~~~~~
src/argparser/test.zig:288:33: note: cannot implicitly cast double pointer '*[]const []const u8' to anyopaque pointer '?*anyopaque'
I need to do this because I am attempting to build a struct that has a default value for a []const []const u8 field using @Struct:
pub const Argument = struct {
field: std.builtin.Type.StructField,
count: Count,
help: []const u8 = "",
short: ?u8 = null,
pub const Count = enum { single, unlimited };
/// For `.count = .unlimited` arguments, the provided `T` must be a slice.
pub fn init(
comptime T: type,
options: struct {
name: [:0]const u8,
count: Count = .single,
help: []const u8 = "",
default_value: ?T = null,
short: ?u8 = null,
},
) Argument {
switch (options.count) {
.single => {},
.unlimited => {
if (@typeInfo(T) != .pointer and @typeInfo(T).pointer.size != .slice) {
@compileError("multi-arguments must be a slice.");
}
},
}
return .{
.field = .{
.name = options.name,
.type = T,
.default_value_ptr = if (options.default_value) |value| &value else null,
.alignment = null,
.is_comptime = false,
},
.count = options.count,
.help = options.help,
.short = options.short,
};
}
};
src/argparser/root.zig:38:73: error: expected type '?*const anyopaque', found '*const []const []const u8'
.default_value_ptr = if (options.default_value) |value| &value else null,
^~~~~~
src/argparser/root.zig:38:73: note: cannot implicitly cast double pointer '*const []const []const u8' to anyopaque pointer '?*const anyopaque'
src/argparser/test.zig:230:26: note: called at comptime here
.init([]const []const u8, .{ .name = "files", .count = .unlimited, .default_value = &[_][]const u8{ "README.md", "build.zig" } }),
~~~