Add String.to_existing_atom/2 to validate against a list of allowed atoms#15483
Conversation
| atom_type = | ||
| with {_, atom_type} when atom_type != nil <- list_of(atoms_type), | ||
| true <- subtype?(atom_type, atom()) do | ||
| atom_type | ||
| else | ||
| _ -> | ||
| atom() | ||
| end | ||
|
|
||
| {return(atom_type, [string_type, atoms_type], stack), context} |
There was a problem hiding this comment.
You can probably change remote_apply instead. do_remote customizes how the whole function is type checked. remote_apply changes only how you compute the return type. This means that, if someone passes to_existing_atom(string, "not a list"), we will type check it already. You don't have anything else to do. And the implementation of remote_apply is likely a simple call to list_hd!
There was a problem hiding this comment.
I managed to make it work in 4456ce4.
And the implementation of remote_apply is likely a simple call to list_hd!
It seems we still need to run remote_apply/3 first to check the domain in our custom remote_apply/5 clause, otherwise it doesn't properly validate args against the signature? But it was fairly straightforward with this method.
|
|
||
| """ | ||
| @spec to_existing_atom(String.t(), [atom]) :: atom | ||
| def to_existing_atom(string, allowed_atoms) |
There was a problem hiding this comment.
Do we want to check it is a non-empty list? As an empty list will always fail anyway?
6a28a27 to
4456ce4
Compare
josevalim
left a comment
There was a problem hiding this comment.
Ship it! We likely want to add List.to_existing_atom too? It can be either here or in a separate PR :)
Yes, I thought I'd just do it as a follow-up once this one is done 👍 |
Co-authored-by: José Valim <jose.valim@gmail.com>
6532de8 to
1d9167e
Compare
:foo | :barI'd like to get proper type warnings if the variable is not a list of atoms, but not sure what the best way to do this.