Mojo Dict: Store struct instances based on a common trait as CollectionElement.
I'm trying to use a Mojo Dict to store different instances of structs which conform to a common trait. I've created a 'playground' file to work out a solution, but I am stumped. Feels like I'm really close, but no joy yet. Any suggestions?
The code snippet below uses the structs defined earlier in the file. The last line below causes the error:
'Dict' parameter #1 has 'CollectionElement' type, but value has type 'Pet[?]'mojodict_playground.mojo(1, 1): 'Dict' declared herestruct Pet[T: TPet]
'Dict' parameter #1 has 'CollectionElement' type, but value has type 'Pet[?]'mojodict_playground.mojo(1, 1): 'Dict' declared herestruct Pet[T: TPet]
fn main() raises: alias cats = "Cats" alias dogs = "Dogs" var my_pet = MyPet(cats) var your_pet = YourPet(dogs) var my_cats = Pet(cats, my_pet) my_cats.start("champagne.") var my_dogs = Pet(dogs, your_pet) my_dogs.start("blueberries.") # var d = Dict[StringKey, CollectionElement]() # Compiler crashes var d = Dict[StringKey, Pet]() # Error here
fn main() raises: alias cats = "Cats" alias dogs = "Dogs" var my_pet = MyPet(cats) var your_pet = YourPet(dogs) var my_cats = Pet(cats, my_pet) my_cats.start("champagne.") var my_dogs = Pet(dogs, your_pet) my_dogs.start("blueberries.") # var d = Dict[StringKey, CollectionElement]() # Compiler crashes var d = Dict[StringKey, Pet]() # Error here