metaprogramming


What's the difference between `use` and `#before_compile`?


What's the difference between use and #before_compile? Both can be used to inject some code in to current module.
One main difference is that the code in __using__ is called and return value injected as soon as you call use within a module, while __before_compile__ is called and the return value injected after all the code directly inside the module is executed, just before the module is compiled.
A simple program to demonstrate the difference:
defmodule When do
defmacro __using__(_) do
IO.inspect :using
__CALLER__.module |> Module.definitions_in |> IO.inspect
quote do end
end
defmacro __before_compile__(_) do
IO.inspect :before_compile
__CALLER__.module |> Module.definitions_in |> IO.inspect
quote do end
end
end
defmodule Main do
#before_compile When
use When
IO.puts "added `#before_compile` and `use`"
def add(x, y), do: x + y
end
Output:
:using
[]
added `#before_compile` and `use`
:before_compile
[add: 2]
ExUnit uses #before_compile to define a function that returns all the tests to make it possible for the ExUnit runner to get and run all the tests.

Related Links

What is the correct syntax in metaprogramming assignment in Julia?
How can I call .WHY on a subroutine in a class in Perl 6?
Using Roslyn, if I have an IdentifierNameSyntax, can I find the member type it refers to (field, property, method…)
Elixir - Define functions in module by metaprogramming
Disambiguate the various quoting mechanisms in Julia metaprogramming
How to initialize list using macros and then generate same set of functions in 2 different modules?
Type-safe metaprogramming? Using strings to query ceylon.language for meta information?
Interpolating an Expression into an Expression
Loop unrolling? in Julia with metaprogramming
Macro for a function template: escaping issues
Macro that defines macro returning struct constant
Array of Expressions to an Array Definition
Code generation from restricted set of input
System of equations using metaprogramming
How to define a partial copy constructor metaprogramically in Julia?
How to interpolate into a Julia “for” expression?

Categories

HOME
windows
google-app-engine
css3
oracle-apex-5
error-handling
vscode-extensions
can
newrelic
switch-statement
teechart
heap
xcodebuild
rtf
libc
onedrive-api
google-maps-autocomplete
breadth-first-search
highstock
strongname
interbase
appirater
handler
custom-controls
kodi
turfjs
hibernate-validator
autolisp
spring-security4
gojs
vue-chartjs
installscript
sql-delete
processwire
wurfl
reactivekit
verifiable-c
xv6
apriori
naming
linqpad
apache-spark-dataset
oim
libgphoto2
rserve
carrot2
aquamacs
filehandle
dt
er-diagram
oozie-coordinator
template-toolkit
polyline
pytables
greatest-n-per-group
jodd
ng-grid
lua-telegram-bot
turbo-prolog
html-to-pdf
mobiledoc-kit
phpgrid
fiware-monitoring
wso2greg
aspectj-maven-plugin
node-serialport
j-security-check
cakephp-2.7
qtwebengine
apache-spark-1.3
contrast
jama
ss7
verold
jjaql
tinyxml
decoupling
spring-3
gd-graph
inappsettingskit
composite-component
apache-abdera
asplinkbutton
sqlclr
booksleeve
cuda-gdb
stress
luabind
pylucene
codebase
twitterizer
mtu
diagrams
maven-1
office-2007
jqueryform
hbm2java
opml
qtabbar
perl5.12
commercial-application

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App