Hi, me again, (sorry if I post way too much) This time it’s about an error that I can’t solve, I’ve looked into the documentation on error inference, and comptime, I’ve looked at the std code, but I just can’t find what I’m doing wrong here, so basically when I compile my code I get hit with
main.zig:42:46: error: unable to resolve comptime value
const list: *Self = try allocator.create(@TypeOf(Self));
~~~~~~~~~^~~~~~~
main.zig:42:46: note: argument to function being called at comptime must be comptime-known
/Users/plgol/zig/0.12.0-dev.3160+aa7d16aba/files/lib/std/mem/Allocator.zig:103:55: note: expression is evaluated at comptime because the generic function was instantiated with a comptime-only return type
pub fn create(self: Allocator, comptime T: type) Error!*T {
~~~~~^~~
But unless I’m missing something very obvious it seems like I’m doing things right (probably not tho I’m just copping) but here is the code :
const std = @import("std");
pub fn ListUnmanaged(comptime T: type) type {
return struct {
const Self = @This();
maybe_head: ?*Node,
len: usize,
pub const Node = struct {
maybe_next: ?*Node,
data: T,
pub fn create(allocator: std.mem.Allocator, data: T) !*Node {
var node: *Node = try allocator.create(@TypeOf(Node));
node.maybe_next = null;
node.data = data;
return (node);
}
pub fn destroy(node: *Node, allocator: std.mem.Allocator) void {
allocator.free(node);
}
pub fn insertAfter(node: *Node, next_node: *Node) void {
next_node.maybe_next = node.maybe_next;
node.maybe_next = next_node;
}
pub fn removeAfter(node: *Node) ?*Node {
const removed = node.maybe_next orelse return null;
node.maybe_next = removed.maybe_next;
removed.maybe_next = null;
return (removed);
}
pub fn next(node: *Node) ?*Node {
return (node.maybe_next);
}
};
pub fn create(allocator: std.mem.Allocator) !*Self {
const list: *Self = try allocator.create(@TypeOf(Self));
list.maybe_head = null;
list.len = 0;
return (list);
}
pub fn destroy(list: *Self, allocator: std.mem.Allocator) void {
var temp: *Node = undefined;
while (list.maybe_head) |node| {
temp = node;
list.maybe_head = node.next();
list.len -= 1;
temp.destroy(allocator);
}
list.destroy(allocator);
}
pub fn insertFront(list: *Self, node: *Node) void {
if (list.maybe_head) |head| {
node.maybe_next = head;
list.maybe_head = node;
} else {
list.maybe_head = node;
}
list.len += 1;
}
pub fn print(list: *Self) void {
while (list.maybe_head) |node| {
std.debug.print("[{any}]->", .{node.data});
}
std.debug.print("\n", .{});
}
};
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var list = try ListUnmanaged(usize).create(allocator);
defer list.destroy(allocator);
}