Editing
Quoting and Unquoting
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
'''Quoting and Unquoting''' '''Quoting''' refers to the ability to capture and manipulate Elixir code as data. In Elixir, code is represented as abstract syntax trees (ASTs), which can be manipulated and evaluated dynamically. This powerful feature allows developers to write macros and metaprogramming constructs. In Elixir, to quote code, you can use the `quote` macro. It takes a single argument and returns the quoted representation of the code. For example: ```elixir quoted_code = quote(do: 1 + 1) ``` The `quoted_code` variable now contains the AST representation of the addition expression `1 + 1`. This quoted code can be used in macros or other metaprogramming scenarios. To unquote code, you can use the `unquote` macro. This macro evaluates the quoted code and inserts its result into the surrounding code. For example: ```elixir x = 1 quoted_code = quote(do: x + unquote(x)) ``` In this example, the value of `x` is dynamically inserted into the quoted code using the `unquote` macro. The resulting AST will be equivalent to `x + 1`. Quoting and unquoting can be incredibly useful when building macros or implementing code generation in Elixir. They allow developers to manipulate code programmatically and generate code dynamically based on runtime conditions. It is important to note that quoting and unquoting are specific to compile-time metaprogramming in Elixir. They are not intended to be used for runtime code evaluation or as a replacement for traditional function calls. For further information on metaprogramming and code generation in Elixir, you can refer to the following articles: * [[Metaprogramming in Elixir]] * [[Macros in Elixir]] * [[Code Generation in Elixir]] {{Elixir}} [[Category:Elixir Programming Language]] [[Category:Metaprogramming]]
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Template:Elixir
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information