#Nested struct method call error

1 messages · Page 1 of 1 (latest)

slow gull
#

I'm trying to make my own implementation of singly linked list, but getting an error while trying to initialize new node object via init method:

const std = @import("std");

pub fn SingleLinkedList(comptime T: type, allocator: std.mem.Allocator) type {
    return struct {
        head_node: ?*Node,

        // List node
        pub const Node = struct {
            value: T,
            next: ?*Node,

            fn init(value: T) Node {
                return Node{ .value = value, .next = null };
            }
        };

        // List methods
        pub fn init() @This() {
            return .{ .head_node = null };
        }

        pub fn append(self: *@This(), value: T) !void {

            // Init new node
            var new_node = try allocator.create(Node);
            new_node.init(value);

            // If list doesn't have any nodes
            if (self.head_node == null) {
                self.head_node = new_node;
                return;
            }

            // Appending new node to existing one
            var current_node = self.head_node.?;
            while (current_node.next) |node| {
                current_node = node;
            }
        }
    };
}
#
    main: src\main.zig:8:13
PS C:\Zig\structs> zig build run
run
└─ run structs
   └─ install
      └─ install structs
         └─ zig build-exe structs Debug native 1 errors
src\structs.zig:26:21: error: no field or member function named 'init' in 'structs.SingleLinkedList(i32,.{.ptr = undefined, .vtable = .{.alloc = (decl 'alloc'), .resize = (decl 'resize'), .free = (decl 'free')}}).Node'
            new_node.init(value);
            ~~~~~~~~^~~~~
src\structs.zig:8:26: note: struct declared here
        pub const Node = struct {
                         ^~~~~~
C:\ZigCompiler\zig.exe build-exe -ODebug -Mroot=C:\Zig\structs\src\main.zig --cache-dir C:\Zig\structs\zig-cache --global-cache-dir C:\Users\amary\AppData\Local\zig --name structs --listen=-   
Build Summary: 2/7 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run structs transitive failure
   ├─ zig build-exe structs Debug native 1 errors
   └─ install transitive failure
      └─ install structs transitive failure
         └─ zig build-exe structs Debug native (reused)
error: the following build command failed with exit code 1:
C:\Zig\structs\zig-cache\o\363b84ee7c95ed80d73aa20e1dddd840\build.exe C:\ZigCompiler\zig.exe C:\Zig\structs C:\Zig\structs\zig-cache C:\Users\amary\AppData\Local\zig --seed 0xac667e87 -Z6cade00cc1eb300e run
PS C:\Zig\structs>
#

What's wrong?

tired hound
#

youre trying to call it as a member function but it doesnt take @This() as the first parameter

#

you have to do new_node = Node.init(value)

slow gull
tired hound
#

np

#

btw you shouldnt take allocator as a parameter for the type, it will only work with some allocators

#

i believe the only reason it works in any case is because the compiler has a special case where it doesnt enforce comptime parameters if the function is only called at comptime

slow gull
tired hound
#

yeah thats exactly where id do it

#

and it should be a field

slow gull
#

Ok, thank you a lot!

tired hound
#

also id probably remove the init function on Node since its so simple

#
pub const Node = struct {
    value: T,
    next: ?*Node = null,
};

this is all you need imo, then youd change the new_node to
new_node.* = .{ .value = value };