4 ms·
Couldn't we have a function to return the data like this? pub const SinglyLinkedList(comptime T: type) type { return struct { first: ?*Node = null,
by kajika91 1y ago
Couldn't we have a function to return the data like this?
pub const SinglyLinkedList(comptime T: type) type {
return struct {
first: ?*Node = null,
pub const Node = struct {
next: ?*Node = null,
};
const Self = @This();
pub fn data(self: Self) *T {
return @fieldParentPtr("node", self);
}
};
- deleted 1y ago[deleted]
- nh1996 1y agoThis would require the SinglyLinkedList to be generic and would require that the data struct use "node" as the field name. Also, as some comments have pointed out, this type of linked list can be useful when a data struct needs to be in multiple lists, in which case there is no single "node" field. const some_data = struct { // Some data fields // ... bar_node: SinglyLinkedList.Node, baz_node: SinglyLinkedList.Node, };
- throwawaymaths 1y agosure, just make T == void fall back to the existing implementation.