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 was hoping for a different answer...this seemed like an elegant solution. 😀 I'd like to store any type that adheres to the trait, even ones that haven't been written yet. I'll play with the Variant and see how it goes. Thanks Michael!
9 replies
MModular
Created by EzRyder on 3/14/2024 in #questions
Mojo Dict: Store struct instances based on a common trait as CollectionElement.
I just removed StringKey and synced the latest to GitHub. Only works for MyPet, not YourPet.
9 replies
MModular
Created by EzRyder on 3/14/2024 in #questions
Mojo Dict: Store struct instances based on a common trait as CollectionElement.
var my_pet = MyPet(cats)
var your_pet = YourPet(dogs)
var my_pet = MyPet(cats)
var your_pet = YourPet(dogs)
This only works for MyPet (i.e. cats), but not for YourPet (i.e. dogs). Both MyPet and YourPet structs use the trait TPet defined previously in the file. So I'm trying to assign the Dict with TPet instead of MyPet: var d = Dict[StringKey, Pet[TPet]]() # Error here But get this error:
'Pet' parameter #0 has 'TPet' type, but value has type 'AnyRegType'mojo
dict_playground.mojo(92, 1): 'Pet' declared here
(trait) trait TPet
'Pet' parameter #0 has 'TPet' type, but value has type 'AnyRegType'mojo
dict_playground.mojo(92, 1): 'Pet' declared here
(trait) trait TPet
9 replies
MModular
Created by EzRyder on 3/14/2024 in #questions
Mojo Dict: Store struct instances based on a common trait as CollectionElement.
I just made one more change in the main function to use 'CollectionElement' instead of 'Pet' to define the dictionary (see below). The VS Code IDE is happy with this change, but the compiler throws this error:
<unknown>:0: error: failed to legalize operation 'kgen.param.constant' that was explicitly marked illegal
<unknown>:0: note: see current operation: %5 = "kgen.param.constant"() {value = #kgen.unknown : !kgen.variant<struct<() memoryOnly>, struct<(index, struct<(struct<(struct<(pointer<none>, index, index) memoryOnly>) memoryOnly>) memoryOnly>, type) memoryOnly>>} : () -> !kgen.variant<struct<() memoryOnly>, struct<(index, struct<(struct<(struct<(pointer<none>, index, index) memoryOnly>) memoryOnly>) memoryOnly>, type) memoryOnly>>
/home/johnsoe1/.modular/pkg/packages.modular.com_max/bin/mojo: error: Failed to materialize symbols: { (exec, { KGEN_EE_JIT_GlobalDestructor, main, KGEN_EE_JIT_GlobalConstructor }) } (from the layer: failed to lower module to LLVM IR for archive compilation)
<unknown>:0: error: failed to legalize operation 'kgen.param.constant' that was explicitly marked illegal
<unknown>:0: note: see current operation: %5 = "kgen.param.constant"() {value = #kgen.unknown : !kgen.variant<struct<() memoryOnly>, struct<(index, struct<(struct<(struct<(pointer<none>, index, index) memoryOnly>) memoryOnly>) memoryOnly>, type) memoryOnly>>} : () -> !kgen.variant<struct<() memoryOnly>, struct<(index, struct<(struct<(struct<(pointer<none>, index, index) memoryOnly>) memoryOnly>) memoryOnly>, type) memoryOnly>>
/home/johnsoe1/.modular/pkg/packages.modular.com_max/bin/mojo: error: Failed to materialize symbols: { (exec, { KGEN_EE_JIT_GlobalDestructor, main, KGEN_EE_JIT_GlobalConstructor }) } (from the layer: failed to lower module to LLVM IR for archive compilation)
Alternate var d = DictStringKey, CollectionElement # Compiler crashes
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 3/14/2024 in #questions
Mojo Dict: Store struct instances based on a common trait as CollectionElement.
Thanks for the suggestion Michael! The code below is the Pet struct from my 'playground' example. As you can see, it already matched your suggestion, but Mojo flags it with the error described in my initial post. I just added the @ value with no discernible effect. Here's a link to my 'playground' file with all the code. GitHub: https://github.com/johnsoez4/dict
@value # <-- Added 2024-03-14 EzRyder
struct Pet[T: TPet](CollectionElement):
var name: String
var pet: T

fn __init__(inout self, name: String, pet: T):
self.name = name
self.pet = pet

fn __copyinit__(inout self, existing: Self):
self.name = existing.name
self.pet = existing.pet

fn __moveinit__(inout self, owned existing: Self):
self.name = existing.name
self.pet = existing.pet

fn __del__(owned self):
pass

fn start(self, edible: String):
self.pet.start(edible)
@value # <-- Added 2024-03-14 EzRyder
struct Pet[T: TPet](CollectionElement):
var name: String
var pet: T

fn __init__(inout self, name: String, pet: T):
self.name = name
self.pet = pet

fn __copyinit__(inout self, existing: Self):
self.name = existing.name
self.pet = existing.pet

fn __moveinit__(inout self, owned existing: Self):
self.name = existing.name
self.pet = existing.pet

fn __del__(owned self):
pass

fn start(self, edible: String):
self.pet.start(edible)
9 replies
MModular
Created by EzRyder on 2/1/2024 in #questions
error: use of unknown declaration '_ZeroStartingRange'
Thanks @Michael K! I hoped it was something fairly simple. Still learning! 😀
4 replies
MModular
Created by EzRyder on 1/21/2024 in #questions
Jupyter - Import Custom Mojo Modules?
So I think I have some VS Code weirdness going on, and maybe an issue with Mojo version 0.6.1. With the Jupyter notebook open, VS Code highlighted the import statement saying it was unable to locate module 'my_name', so I prefixed it with the current folder shared.my_name which made the type checker happy but gave an error when I tried to run the Mojo code. I removed the extra shared. and it now runs as expected. Perhaps also important...I updated to Mojo version 0.6.3 Hope this helps anyone else trying to import custom modules in a Jupyter notebook. Cheers...EzRyder Edit: In VS Code, I found that I needed to restart the kernel in order for changes in a Mojo module to propagate into the open Jupyter file. Module: my_name.mojo
struct MyName(Stringable):
var name: String

fn __init__(inout self, name: String):
self.name = name

fn __str__(self) -> String:
return str(self.name)
struct MyName(Stringable):
var name: String

fn __init__(inout self, name: String):
self.name = name

fn __str__(self) -> String:
return str(self.name)
Jupyter
from my_name import MyName

let username = "Bob"
let name = MyName(username)
print("Hi " + str(name) + "!")
from my_name import MyName

let username = "Bob"
let name = MyName(username)
print("Hi " + str(name) + "!")
Output: Hi Bob!
3 replies
MModular
Created by MPloppy on 9/11/2023 in #questions
Why the crappy system-breaking script for installation?
I use OpenSUSE Tumbleweed on my laptop, and would appreciate an rpm repo compatible with Yast to provide easy installation and updates to Mojo. TIA! 😀
10 replies