suggestions
suggestions copied to clipboard
Built in int -> enum
This could exist in the language and I am missing it, but it seems there isn't a way to directly map a number to a type. One example that I find this useful in is Days of the week, where 1-7 could represent a day. What is currently needed is case statements to achieve mappings between custom types (correct me if I am wrong).
pub type Weekday {
Monday
Tuesday
}
pub create_date(d: Int) {
case d {
1 -> Ok(Monday)
2 -> Ok(Tuesday)
_ -> Error("Invalid date")
}
}
Which is fine for 7 days of the week. But imagine when RPC mappings are desired, or a large enum is defined.
A solution could look something similar to this:
Weekday@2 == Tuesday
Weekday@3 == Error("Invalid Custom Type Postion")
Where some similar erlang code could be generated:
%% [email protected]
-define(WEEKDAY, [monday, tuesday]).
-define(AT_WEEKDAY(index), lists:nth(index, ?WEEKDAY)).
%% [email protected]
...
try ?AT_WEEKDAY(2) of
Day -> Day
catch
{error, "Invalid Custom Type Position")
end.
Unless this exists already?
It does not already exist but it does align well with some ideas I have for derived functions. We can add something like this is the future. :)
Right on :D