Hacker News new | past | comments | ask | show | jobs | submit login

The unquote in your code example doesn't necessarily need to be unquoted, it would be enough to unquote the codepoint variable here. The point, I guess, is about at what point you want to evaluate the code. There is a great little function called Macro.to_string that might make this more clear:

  Macro.to_string(quote do: length([1,2,3]))
will return "length([1,2,3])", so it basically creates a macro that, if included in some code, will calculate the length of the list [1,2,3] at runtime.

  Macro.to_string(quote do: unquote(length([1,2,3])))
will return "3" instead, it basically executes the length function at compile time and then the macro will simply return that if called.

So the code example you provided basically calculates the byte size at compile time and will then only return static results at runtime.




Thanks! That makes it clearer. So presumably the for loop it's nested in is also evaluated at compile time?


Yes, exactly. This code basically runs at compile time and creates functions that return static results. This case doesn't have to be wrapped inside of the quote thing (I'm not even sure if it would compile if you did...), which is somewhat confusing in my opinion.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: