Skip to content

Instantly share code, notes, and snippets.

@ul
Created September 16, 2019 07:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ul/8dc892c1fe37c4889bbde8526200ce15 to your computer and use it in GitHub Desktop.
Save ul/8dc892c1fe37c4889bbde8526200ce15 to your computer and use it in GitHub Desktop.
const std = @import("std");
const mem = std.mem;
fn LinkedList(comptime T: type) type {
const Self = struct {
const Node = struct {
next: ?*Node,
data: T,
};
first: ?*Node,
allocator: *mem.Allocator,
pub fn init(allocator: *mem.Allocator) !*Self {
var self = try allocator.create(Self);
self.first = null;
self.allocator = allocator;
return self;
}
};
return Self;
}
test "LinkedList" {
var arena = std.heap.ArenaAllocator.init(std.heap.direct_allocator);
defer arena.deinit();
const allocator = &arena.allocator;
var list = try LinkedList(i32).init(allocator);
}
list.zig:14:50: error: use of undeclared identifier 'Self'
list.zig:32:35: note: referenced here
Copy link

ghost commented Sep 16, 2019

Do it this way, you need to return the struct expression instead.

fn LinkedList(comptime T: type) type {
    return struct {
        const Self = @This();

        const Node = struct {
            next: ?*Node,
            data: T,
        };

        first: ?*Node,
        allocator: *mem.Allocator,

        pub fn init(allocator: *mem.Allocator) !*Self {
            var self = try allocator.create(Self);
            self.first = null;
            self.allocator = allocator;
            return self;
        }
    };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment