M
Modular•15mo ago
Orwink

Mojo have a HashMap or Dict?

:mojo:
23 Replies
TeamPuzel
TeamPuzel•15mo ago
No, it is impossible to do properly without traits. You need a way to constrain the Key type to just hashable types, and that's just not implemented yet.
Moosems / Three chickens
@TeamPuzel Has anyone even made a proper hash function yet?
TeamPuzel
TeamPuzel•15mo ago
I don't know, I doubt it You could maybe implement a dictionary assuming the Key type can only be a string
TeamPuzel
TeamPuzel•15mo ago
Yeah that seems to be avoiding the problem by limiting it to strings keys Cool, not sure what the object thing does
Moosems / Three chickens
Its a type thats supposedly easier to use than AnyType
TeamPuzel
TeamPuzel•15mo ago
Oh, yeah, it looks like a runtime dynamic type feature
Orwink
OrwinkOP•15mo ago
Thanks for your answers
Stole
Stole•15mo ago
IIRC the object type is just a Variant over some internal structures, like _RefCountedListRef and _ImmutableString. Definitely wouldn't recommend messing with them too much.
Moosems / Three chickens
I thought the object type was supposed to be the nicer AnyType...
Stole
Stole•15mo ago
I'm not sure what AnyType actually is TBH. When Mojo spits MLIR errors about it, it seems to be of !kgen.mlirtype which doesn't tell us all that much.
Moosems / Three chickens
Is object safe to use?
Stole
Stole•15mo ago
Hmm, that's a hard question. I think as long as you're using the basic Bool and Float values it is, but I really don't know how to deal with the _RefCountedListRef, _Function, and similar values. Probably best to stay away from them. (It also looks like they don't necessarily have attached / visible properties [or if they do have some there's very few], so messing with them at all might be hard. Maybe a couple MLIR ops are dedicated to them, I wouldn't know.) Just for reference, this is what the LSP lists o._value.value as (if o is of type object).
(field) var value: Variant[_NoneMarker, Bool, SIMD[si64, 1], SIMD[f64, 1], _ImmutableString, _RefCountedListRef, _Function, _RefCountedAttrsDictRef]
(field) var value: Variant[_NoneMarker, Bool, SIMD[si64, 1], SIMD[f64, 1], _ImmutableString, _RefCountedListRef, _Function, _RefCountedAttrsDictRef]
Moosems / Three chickens
I just have so many issues come up with using AnyType.
PriNova
PriNova•15mo ago
The interesting part is:
let o: object
let val = o._value
let o: object
let val = o._value
the underlying _ObjectImpl type, which can be used for conversion purposes and other stuff. The underlying type is the Variant type (MLIR type) This seems similar to what is used as DodgyType in a community repo I saw. The documentation misses a lot of functions, which can only be seen by the completion in a IDE.
Moosems / Three chickens
I think that would be my repo with the DodgyString 😄.
PriNova
PriNova•15mo ago
So maybe you can substitute the Dodgy types with an 'object' as a test.
Moosems / Three chickens
The DodgyType was due to Strings not being passable so a special type was needed to be created, though maybe I'm misunderstanding your statement.
PriNova
PriNova•15mo ago
Ok, understand.
ModularBot
ModularBot•15mo ago
Congrats @PriNova, you just advanced to level 10!
TeamPuzel
TeamPuzel•15mo ago
AnyType looks a lot like the most generic of traits/protocols Its syntax also looks like a trait It and Object have nothing in common, not sure where you heard that Object is a dynamic runtime object and AnyType is for constraining generics I guess AnyType might feel a bit like boilerplate until you can also use other traits in its place 🤔
PriNova
PriNova•15mo ago
The !kgen seems to be a Fortran Kernel generator and is used for AST generation and manipulation.
Stole
Stole•15mo ago
Yeah, the kgen dialect seems to contain lots of special types for use in the AST or other. I was sort of surprised by the fact they moved !pop.pointer to !kgen.pointer, and there's probably more examples of types that might not belong. I'd guess the organization / categorization isn't perfect. There's also examples like !kgen.declref that definitely make sense
Want results from more Discord servers?
Add your server