0

I have a list of macros in Elixir and trying to put quotes around it, please help me.

I want to convert [Id, Node, Timestamp] into "[Id, Node, Timestamp]".

How would I do that?

1
  • 2
    What have you tried so far? Commented Dec 19, 2018 at 20:11

2 Answers 2

2

[Id, Node, Timestamp] is a list of atoms not a list of macros.

Atoms are plain terms and hence plain old good Kernel.inspect/2 would perfectly do.

inspect [Id, Node, Timestamp]
#⇒ "[Id, Node, Timestamp]"
Sign up to request clarification or add additional context in comments.

Comments

1

If you have a quoted expression, I believe Macro.to_string/2 is what you're looking for.

iex> ast = quote do: [Id, Node, Timestamp]
[Id, Node, Timestamp]
iex> Macro.to_string(ast)
"[Id, Node, Timestamp]"

Like Aleksei pointed out, however, the AST for a list of atoms is itself, so if that's all you're trying to convert to a string, Kernel.inspect/2 accomplishes the same thing.

2 Comments

Quoting a list of atoms makes zero sense since both lists and atoms are already quoted expressions, equal to their AST. Macro.to_string([Id, Node, Timestamp]) has the same effect.
Macro.to_string(ast) helped. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.