Reputation: 1344
In Erlang, \"
is an escape character that means double quote.
My question is, what is the difference between "\"test\""
and ""test""
? The reason I ask is because, I'm trying to handle a list_to_atom
error:
> list_to_atom("\"test\"").
'"test"'
> list_to_atom(""test"").
* 1: syntax error before: test
Upvotes: 2
Views: 4042
Reputation: 2612
""
is a string/list of length 0
\"
is just an escaped double-quote when used in the context of a string. If you wanted to have a string that consists of just a double-quote (ie \"
), then you could do: "\""
.
""test""
is a syntax error and is no difference than "" test ""
which is syntactically <list><atom><list>
. What are you trying to accomplish?
Upvotes: 5
Reputation: 1660
It's not advised to dynamically generate atoms as they're never garbage collected.
You'd better use list_to_existing_atom/1
when reading user input. Otherwise, you might end up running out of memory (in a system running long enough; but hey, that's what systems Erlang is for, isn't it?) and crashing the whole virtual machine.
list_to_existing_atom/1
will throw an error in case the atom doesn't exist and return the atom if it exists. A construct like catch list_to_existing_atom(some_atom)
might prove useful coupled with a case .. of
or a try ... catch
block. Try it in the shell and see what you like the best.
If this answer seems irrelevant to the question, then please note I'm not allowed to post comments yet, so this answers the question in the comment to chops' answer, namely:
I have to write a function that reads from keyboard until an atom is typed. I have to do this with get_line and list_to_atom. – otisonoza
Upvotes: 2