EzRyder
EzRyder
MModular
Created by EzRyder on 3/14/2024 in #questions
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]
'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
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
9 replies
MModular
Created by EzRyder on 2/1/2024 in #questions
error: use of unknown declaration '_ZeroStartingRange'
In a Mojo function, how can I use the built in type _ZeroStartingRange as a return type? I received the following code snippet from kapa.ai on Modular's Discord server, but it has the same issue. https://discord.com/channels/1087530497313357884/1119100298456215572/1202422957947879524 I'm currently using Mojo SDK 0.7.0. Thanks!
fn createRange(end: Int) -> _ZeroStartingRange:
return range**end** (end)
fn createRange(end: Int) -> _ZeroStartingRange:
return range**end** (end)
4 replies
MModular
Created by EzRyder on 1/21/2024 in #questions
Jupyter - Import Custom Mojo Modules?
How can we import and use a custom Mojo module from a Jupyter notebook? I added details of what I've tried so far by replying to an existing post started by @hogepodge. Thanks! Here's a link to that post: https://discord.com/channels/1087530497313357884/1119100298456215572/1198684128091058198
3 replies