How to do recursive structs?

I'm trying to implement a json decoder, but I can't even make a struct that will hold the result. Here is the code I'm trying to run:
from stdlib_extensions.builtins import list, dict, HashableStr
from collections.optional import Optional

@value
struct JsonObbject(CollectionElement):
var possible_list: Optional[list[JsonObbject]]
var possible_dict: Optional[dict[HashableStr, JsonObbject]]
var possible_int: Optional[Int]
var possible_string: Optional[String]
from stdlib_extensions.builtins import list, dict, HashableStr
from collections.optional import Optional

@value
struct JsonObbject(CollectionElement):
var possible_list: Optional[list[JsonObbject]]
var possible_dict: Optional[dict[HashableStr, JsonObbject]]
var possible_int: Optional[Int]
var possible_string: Optional[String]
I get the error:
/projects/open_source/mojo-stdlib-extensions/trying_stuff.mojo:6:38: error: 'list' parameter #0 has 'CollectionElement' type, but value has type 'JsonObbject'
var possible_list: Optional[list[JsonObbject]]
^~~~~~~~~~~
Included from /projects/open_source/mojo-stdlib-extensions/stdlib_extensions/builtins/__init__.mojo:1:
/projects/open_source/mojo-stdlib-extensions/stdlib_extensions/builtins/_generic_list.mojo:21:1: note: 'list' declared here
struct list[T: CollectionElement](Sized, Movable):
^
/projects/open_source/mojo-stdlib-extensions/trying_stuff.mojo:7:51: error: 'dict' parameter #1 has 'CollectionElement' type, but value has type 'JsonObbject'
var possible_dict: Optional[dict[HashableStr, JsonObbject]]
^~~~~~~~~~~
Included from /projects/open_source/mojo-stdlib-extensions/stdlib_extensions/builtins/__init__.mojo:5:
/projects/open_source/mojo-stdlib-extensions/stdlib_extensions/builtins/_dict.mojo:60:1: note: 'dict' declared here
struct dict[K: HashableCollectionElement, V: CollectionElement](Sized):
^
mojo: error: failed to parse the provided Mojo
/projects/open_source/mojo-stdlib-extensions/trying_stuff.mojo:6:38: error: 'list' parameter #0 has 'CollectionElement' type, but value has type 'JsonObbject'
var possible_list: Optional[list[JsonObbject]]
^~~~~~~~~~~
Included from /projects/open_source/mojo-stdlib-extensions/stdlib_extensions/builtins/__init__.mojo:1:
/projects/open_source/mojo-stdlib-extensions/stdlib_extensions/builtins/_generic_list.mojo:21:1: note: 'list' declared here
struct list[T: CollectionElement](Sized, Movable):
^
/projects/open_source/mojo-stdlib-extensions/trying_stuff.mojo:7:51: error: 'dict' parameter #1 has 'CollectionElement' type, but value has type 'JsonObbject'
var possible_dict: Optional[dict[HashableStr, JsonObbject]]
^~~~~~~~~~~
Included from /projects/open_source/mojo-stdlib-extensions/stdlib_extensions/builtins/__init__.mojo:5:
/projects/open_source/mojo-stdlib-extensions/stdlib_extensions/builtins/_dict.mojo:60:1: note: 'dict' declared here
struct dict[K: HashableCollectionElement, V: CollectionElement](Sized):
^
mojo: error: failed to parse the provided Mojo
It Seems that Mojo doesn't like the recursivity of my struct. It it something that should be reported or am I misunderstanding something?
2 Replies
gabrieldemarmiesse
gabrieldemarmiesseOP12mo ago
To get a smaller reproducer with types from the stdlib:
from collections.vector import DynamicVector

@value
struct MyNewType(CollectionElement):
var possible_list: DynamicVector[MyNewType]
from collections.vector import DynamicVector

@value
struct MyNewType(CollectionElement):
var possible_list: DynamicVector[MyNewType]
/projects/open_source/mojo-stdlib-extensions/trying_stuff.mojo:5:38: error: 'DynamicVector' parameter #0 has 'CollectionElement' type, but value has type 'MyNewType'
var possible_list: DynamicVector[MyNewType]
^~~~~~~~~
/projects/open_source/mojo-stdlib-extensions/trying_stuff.mojo:1:1: note: 'DynamicVector' declared here
from collections.vector import DynamicVector
^
mojo: error: failed to parse the provided Mojo
/projects/open_source/mojo-stdlib-extensions/trying_stuff.mojo:5:38: error: 'DynamicVector' parameter #0 has 'CollectionElement' type, but value has type 'MyNewType'
var possible_list: DynamicVector[MyNewType]
^~~~~~~~~
/projects/open_source/mojo-stdlib-extensions/trying_stuff.mojo:1:1: note: 'DynamicVector' declared here
from collections.vector import DynamicVector
^
mojo: error: failed to parse the provided Mojo
toasty
toasty11mo ago
I'm seeing the same for my self referential structs. I'm guessing the language server is overwhelmed because it blows up with a different error for me, but if I pare it down to just the self referential attribute I get the same error.
Want results from more Discord servers?
Add your server