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:
I get the error:
It Seems that Mojo doesn't like the recursivity of my struct. It it something that should be reported or am I misunderstanding something?
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 MojoIt Seems that Mojo doesn't like the recursivity of my struct. It it something that should be reported or am I misunderstanding something?
