I would like to use a variant as a key in my HashMap, for example
let Country = {
#germany;
#france;
#england;
...
}
func countryEqual(c1: Country, c2: Country): bool { c1 == c2 };
let countryMap: HashMap<Country, Text> = HashMap<Country, Text>(
countries.size(),
countryEqual,
//what could/should I use here as a hash?
);
I have a few questions based off of this example:
- Is this possible/recomended usage of a variant, or should I be using a type union of country strings, or even a record and just not have my HashMap key be typed?
- If this is possible, is my variant equals function valid in Motoko or would I need a
toText()
function? I’m assuming this works based on how a variant is used in pattern matching, but don’t understand the underlying representation of a variant. - As mentioned in the comment above, is a variant hashable as is or would I need to
toText()
it first and hash the Text?