I have this function:
def top(genre)
“genre” is always a string, but in can only take 3 different values. How do I type it in Elixir ?
I’m thinking on something like enum types in C++:
@type valid_genres :: "horror" | "action" | "drama"
@spec def top(genre:: valid_genres)
2 Likes
I would use atoms instead of strings and then have a conversion to and from the string representation.
@type valid_genres :: :horror | :action | :drama
I don’t think there is any simple way to type on different values of strings (that I am aware of).
2 Likes
There isn’t. Strings can’t be typed beyond String.t
/ binary()
.
3 Likes