ModularM
Modular2y ago
8 replies
ezRyder

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?

GitHub: https://github.com/johnsoez4/dict

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[?]'mojo
dict_playground.mojo(1, 1): 'Dict' declared here
struct 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
GitHub
Contribute to johnsoez4/dict development by creating an account on GitHub.
GitHub - johnsoez4/dict
Was this page helpful?