M
Modular•12mo ago
Orwink

Mojo have a HashMap or Dict?

:mojo:
23 Replies
TeamPuzel
TeamPuzel•12mo 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.
Three chickens in the green bag
@TeamPuzel Has anyone even made a proper hash function yet?
TeamPuzel
TeamPuzel•12mo 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•12mo ago
Yeah that seems to be avoiding the problem by limiting it to strings keys Cool, not sure what the object thing does
Three chickens in the green bag
Its a type thats supposedly easier to use than AnyType
TeamPuzel
TeamPuzel•12mo ago
Oh, yeah, it looks like a runtime dynamic type feature
Orwink
Orwink•12mo ago
Thanks for your answers
Stole
Stole•12mo 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.
Three chickens in the green bag
I thought the object type was supposed to be the nicer AnyType...
Stole
Stole•12mo 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.
Three chickens in the green bag
Is object safe to use?
Stole
Stole•12mo 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]
Three chickens in the green bag
I just have so many issues come up with using AnyType.
PriNova
PriNova•12mo 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.
Want results from more Discord servers?
Add your server