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

How to define functions based on attribute to elixir?
Remove a const qualifier from a variable in D
Meta-modeling crud FROM/JOIN/WHERE statement fragments for higher-level analysis
Elixir: eval'ing code in module context using Code.eval_quoted/3
What is Declarative Meta-Programming?
Reconsolidate protocols in Elixir 1.2 or higher
Inserting an AST into a quote do end block without unquoting it
Transform quoted elixir code to a code string
Pattern matching benchmarking : Compiletime lookup vs Runtime lookup in D
Meta construction capabilities?
How to manipulate the value of variables based on an array of symbols in Julia?
Multiple #:build macros
Julia automatically generate functions and export them
Dealing with end keyword in macros for array indices
Implementation of AnyMap and runtime overhead of `struct Port(u32);`
Can function application be overriden with Macro in Elixir?

Categories

HOME
cobalt
checkstyle
character-encoding
flex
components
jelastic
devise
regular-language
banner
crystal-lang
sendgrid-api-v3
fabric.io
commonjs
jxl
filechannel
angular-dart
naivebayes
pybossa
do-while
alpacajs
template-engine
shortest-path
scriptella
short-url
onload
itunes
fragment-backstack
apktool
carrierwave
nsurl
corenlp-server
utorrent
weather
record
github3.py
gnat-gps
lubridate
searchkit
magento-1.9.2.4
tex
projects-and-solutions
diacritics
p4merge
tasm
azure-arm
chronicle-map
drupal-theming
optimizely
xbmc
builder
sonarlint-eclipse
tinymce-3
carrot2
context-sensitive-grammar
rxtx
vcloud-director-rest-api
pacemaker
signalr.client
fuzzywuzzy
fink
nio2
jaunt-api
relativelayout
concurrentmodification
grgit
computer-architecture
oracle-spatial
xvim
pax-exam
batik
uncompress
pyobjc
google-refine
node-serialport
uvc
zend-currency
rrule
mmwormhole
packet-sniffers
easyrtc
ignite-ui
parallel-testing
ray
tinyxml
syndicationfeed
gtm-oauth2
away3d
kogrid
kaleidoscope
octal
video-codecs
windows-98
alice
android-sdk-2.3
booksleeve
nokia-s40
vfs
listitem
subproject
objectquery
azman
landscape-portrait
webdev.webserver
clrstoredprocedure
timthumb
rowlex
qtabbar
data-mapping
wordprocessingml
flvplayback

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