ModularM
Modular3y ago
5 replies
Danny Go

Pointer to a custom struct?

Is it possible to create pointers to a custom struct instance, and be able to load the instance from the pointer?

According to the AI bot, "Mojo's Pointer struct is designed to work with basic types like Int, and it's not clear if it can directly handle custom structs".

Here is my latest attempt (a linked list):
@register_passable("trivial")
struct ListNode:
    var val: Int
    var next: Pointer[ListNode]
    
    @always_inline
    fn __init__(val: Int) -> Self:
        return ListNode {val: val, next: Pointer[ListNode].get_null()}

let node1 = ListNode(1)

Which got an error:
error: Expression [91]:33:25: LLVM Translation failed for operation: builtin.unrealized_conversion_cast
        return ListNode {val: val, next: Pointer[ListNode].get_null()}
                        ^
Was this page helpful?