cheezebubba 6 days ago

How can you have elements of different types? I don't understand how you would know which type a given node was in?

1
throwawaymaths 6 days ago

it was maybe easier to understand when @fieldParentPtr took the type as an argument, but if you look at this:

   var x: *T = @fieldParentPtr("node", node_ptr);
@fieldParentPtr knows that its result must have type pointer T; and then the builtin function "looks" inside of T and notices there is a "node" field. Then it can backtrack the pointer of T given the pointer to the node field, since the layout of T is well-defined at compile time.

So you could have a mixed-type linked list, and at the callsite zig can at compile-time back out the parent type. There obviously needs to be outside logic (and an outside mechanism to track) this with branches for each type that you could be putting into the linked list. And yes, this is more than a little bit type-unsafe because of the potential for type erasure.

You could probably pretty trivially write a typesafe wrapper for this, as suggested in one of the comments here.

cheezebubba 6 days ago

Thanks. I still don't understand where the bits live that tell you which type T is for a given node.

Is this something like "list starts with type A, and that will tell you if the next node is type A or B"?

Is there a way to stuff those bits in with the node somehow so you always know the type you expect back?

throwawaymaths 6 days ago

> Thanks. I still don't understand where the bits live that tell you which type T is for a given node.

Yup. You'd have to set that up yourself. It could be anywhere. It could be in a separate array, it could be in the previous node-data, etc.

you wouldn't put it in the Node (capital N) datastructure though, that is privileged.

Personally, I would say doing something like this is "bad idea bears" unless you are absolutely certain you need it and you should explore using, say, data payload type with a union field instead, if possible (though know that that comes with a potential memory penalty)

cheezebubba 6 days ago

Aha Thanks!