Maps with all types as keys #11927
Replies: 10 comments
-
How would this work? What would be the use-case to have a single key be an array of ints, for example? |
Beta Was this translation helpful? Give feedback.
-
I can't think of any use-cases for arrays, but structs are useful sometimes. What do you mean by "how would this work"? It would work exactly the same as with other types. |
Beta Was this translation helpful? Give feedback.
-
I'm just having a hard time wrapping my head around why anyone would need to have a struct as a key in a map. There are so many things that could make it work incorrectly, depending on instances, pointers, etc. But as you say, it does use hashes, so it would probably work at least some of the time. :-\ |
Beta Was this translation helpful? Give feedback.
-
For example, when writing plugins for some kind of service, maps with struct keys can be a replacement for fields that you can't declare in the plugin, because the struct is declared inside the main service. |
Beta Was this translation helpful? Give feedback.
-
I would never do plugins that way... I would only want to load a plugin once. The At any rate... to each his own, I guess. I'll stop worrying about it now. It's up to someone else whether or not to implement this. |
Beta Was this translation helpful? Give feedback.
-
I meant that e.g. your service passes a user object to the plugin. The plugin then wants to do something if the user earlier turned on a feature of the plugin in their personal settings. if user_feature_map[user] == true{ (This design is of course only practical if one user only has one object assigned ever, so basically, the service has to store everything in the RAM all the time...) Anyway, I'm sure there are some scenarios where this is useful, so I agree with you to just leave this open so someone can someday implement this. |
Beta Was this translation helpful? Give feedback.
-
For reference, Rust's HashMaps allow any type to be a key as long as it satisfies two constrainst:
In Rust these are the The equivalent in V would be a |
Beta Was this translation helpful? Give feedback.
-
By the way, I have written a hashmap implementation that works like Rust's HashMaps, as described above: https://github.com/Wertzui123/HashMap |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
This is also related to the feature request of "Array as a map key". |
Beta Was this translation helpful? Give feedback.
-
V currently only supports a few primitive types as keys in maps.
It would be useful if structs, arrays, ... could also be keys.
Beta Was this translation helpful? Give feedback.
All reactions