Using QuoteNode for symbols

Symbols are quite special when they appear in expressions. They may appear in the head node of an expression object—for example, the = symbol in a variable assignment expression. They may also appear in the arguments of an expression object, in which case they would represent a variable:

Since symbols are already used to represent variables, how would we assign an actual symbol to a variable? To figure out how this works, we can use the same trick that we have learned so far—using the dump function to examine the expression object for such a statement:

As we can see, an actual symbol must be enclosed in a QuoteNode object. Now that we know what is needed, we should try to interpolate an actual symbol into an expression object. The way to achieve this is to create a QuoteNode object manually and use the interpolation technique as usual:

A common mistake is when you forget to create QuoteNode. In this case, the expression object will misinterpret the symbol and treat it as a variable reference. Obviously, the result is very different, and it will not work properly:

Not using QuoteNode would generate code that assigns the value of one variable to another. In this case, the variable x will be assigned with a value from variable hello.

Understanding how QuoteNode works is essential creating expressions on the fly. It is common for programmers to interpolate symbols into an existing expression. So next, we will look at how to work with nested expressions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.80.34