Add @enum_from_value

This commit is contained in:
Christoffer Lerno
2024-12-27 23:21:36 +01:00
parent 008274cda5
commit d20d957881
2 changed files with 19 additions and 0 deletions

View File

@@ -235,6 +235,24 @@ macro enum_by_name($Type, String enum_name) @builtin
return SearchResult.MISSING?;
}
<*
@param $Type `The type of the enum`
@require $Type.kindof == ENUM `Only enums may be used`
@require $defined($Type.#value1) `Expected '#value' to match an enum associated value`
@require $assignable(value, $typeof($Type{}.#value)) `Expected the value to match the type of the associated value`
@ensure @typeis(return, $Type)
@return! SearchResult.MISSING
*>
macro @enum_from_value($Type, #value, value) @builtin
{
usz elements = $Type.elements;
foreach (e : $Type.values)
{
if (e.#value == value) return e;
}
return SearchResult.MISSING?;
}
<*
Mark an expression as likely to be true

View File

@@ -36,6 +36,7 @@
- Added `iter()` `value_iter()` and `key_iter()` to HashMap.
- Add "tokenizer" to String.
- Add "skip_empty" to split methods. Add split_to_buffer method.
- Add `@enum_from_value`.
## 0.6.5 Change list