if (enum_node->scope == NULL) {
enum_node->scope = scope_create(context.ast_alloc, current_scope, enum_node->token->pos);
+ symbol_raw_introduce(enum_node->scope, "__backing_type", enum_node->token->pos, (AstNode *) enum_node->backing);
+
type_build_from_ast(context.ast_alloc, (AstType *) enum_node);
}
package core.hash
+use core.intrinsics.types {type_is_enum}
+
//
// DEPRECATED: to_u32 is the old name that kind of
// makes sense, but is a little unintuitive. Use
(key: type_expr) -> u32 { return hash(cast(u32) key); },
(key: bool) -> u32 { return 1 if key else 0; },
+ (key: $T/type_is_enum) -> u32 {
+ return hash(cast(T.__backing_type) key);
+ },
+
#order 10000
macro (key: $T/HasHashMethod) => key->hash()
}
--- /dev/null
+(/Users/brendan/.onyx/core/hash/hash.onyx:46,41) Failed to satisfy constraint where t is of type 'Fruit'.
+ 46 | macro (key: $T/type_is_enum) -> u32 {
+ ^
+(/Users/brendan/.onyx/core/container/map.onyx:19,66) Here is where the interface was used.
+ 19 | Map :: struct (Key_Type: type_expr, Value_Type: type_expr) where ValidKey(Key_Type) {
+ ^~~~~~~~
+(/Users/brendan/dev/onyx/tests/map_enum_key.onyx:18,18) Here is the code that caused this constraint to be checked.
+ 18 | m := make(Map(Fruit, str));
+ ^
--- /dev/null
+package main
+
+use core {*}
+
+Fruit :: enum {
+ Apple;
+ Banana;
+ Orange;
+}
+
+Fruit_64 :: enum (i64) {
+ Apple;
+ Banana;
+ Orange;
+}
+
+main :: () {
+ m := make(Map(Fruit, str));
+
+ m->put(.Apple, "This is apple.");
+ m->put(.Banana, "This is banana.");
+ m->put(.Orange, "This is orange.");
+
+ println(m);
+
+ m2 := make(Map(Fruit, str));
+
+ m2->put(.Apple, "This is apple 64.");
+ m2->put(.Banana, "This is banana 64.");
+ m2->put(.Orange, "This is orange 64.");
+
+ println(m2);
+}