Is there a Null value that we can assign to any type? #69
-
Hi, I'm trying to create a node struct, and for that I need to put the next value as Null. I tried using None for that matter but it turns out AnyType can't be converted to Node type or something. How may I do it? I'm trying to do something like this and I need to use something as Null in place of None struct Node:
var val : Int
var nxt: Node
fn __init__(self&):
self.val = 0
self.nxt = None Thanks for your time |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Note that structs are passed around by value, so nxt: Node is not a pointer to a Node. Also relevant information from the roadmap (https://docs.modular.com/mojo/roadmap.html#recursive-structs-dont-work): struct Node: A workaround for this limitation is to manually perform type erasure: struct Node:
""" |
Beta Was this translation helpful? Give feedback.
-
I have no way to test it, but a forward assignment comes to mind. That is to say, declaring the variable and perhaps assigning a type to the variable, without actually giving a value to the variable.
--- Edit: I just realized since you already have "var nxt" earlier in the struct, what I suggested probably makes no sense at all. Also, I used Python type "int" while you had already assigned it type Node. So, scratch that. I wonder if you would make a PreNode or BaseNode struct and have Node inherit from that, so that you could perhaps use that type at the start and update it to Node when it's first given a value. Just brainstorming. Trying to help, but... failing at it, I think. DAK |
Beta Was this translation helpful? Give feedback.
-
Per the roadmap doc, we don't currently have an Optional type and proper universal nullability. This is really important to me and others, but we need to build out the right features before going there. |
Beta Was this translation helpful? Give feedback.
Per the roadmap doc, we don't currently have an Optional type and proper universal nullability. This is really important to me and others, but we need to build out the right features before going there.