In the hope of bringing a derailed conversation back on track: Ford, maybe you are not asking a Julia but a general computer science question. In this case: A macro is strictly more powerful than a function, so in principle, every function can be replaced by a macro. There are various "technical" details to get right (i.e. scoping, preventing name clashes) that make this non-trivial.
However, a much stronger argument for functions is current compiler technology. For example, Julia compiles a whole function at a time, which makes it impossible to have recursive macros. Also, since Julia can inline functions, but won't "anti-inline" common expressions, this would lead to rather bad code. Thus, from a practical point of view, you can use macros instead of functions only for small functions without sacrificing efficiency. In fact, when you write a macro in Julia, you'll often find that you need to break a large macro into functions, and then substitute calls to these functions instead of the code you'd like to generate. -erik On Sun, Jun 5, 2016 at 9:06 AM, Ford O. <[email protected]> wrote: > ... > I think I gave you all help I could, to understand the question. I am not > going to explain it over and over and over. > > > On Sunday, June 5, 2016 at 2:40:31 PM UTC+2, Tamas Papp wrote: >> >> I am sorry --- I did not realize you had anger management issues. In >> any case, consider the example in the section of the manual I linked, >> with macros and functions: >> >> macro time_mac(ex) >> return quote >> local t0 = time() >> local val = $ex >> local t1 = time() >> println("elapsed time: ", t1-t0, " seconds") >> val >> end >> end >> >> function time_fun(ex) >> return quote >> local t0 = time() >> local val = $ex >> local t1 = time() >> println("elapsed time: ", t1-t0, " seconds") >> val >> end >> end >> >> Then observe the output of >> >> time_fun(:(foo())) >> >> macroexpand(:(@time_mac(foo())) >> >> On Sun, Jun 05 2016, Ford O. wrote: >> >> > Don't make me angry. >> > >> > I have read that chapter more than 5 times. >> > You could also read my question 5 times. >> > >> > I am asking why do we have to specify macro and function with different >> > keyword, when compiler exactly knows whether we are calling macro or >> > function. >> > >> > Example for ...: >> > callable foo(e::Expr) = :(&e) >> > @foo 2 + 2 >> > foo(2 + 2) >> > # two versions of foo callable are compiled, one as macro, one as >> function >> > >> > Does the *macro *keyword have any significance for compiler? >> > >> > On Sunday, June 5, 2016 at 1:38:32 PM UTC+2, Tamas Papp wrote: >> >> >> >> Sorry for linking a local version of the manual -- still, you could >> >> probably have found it with a bit of effort, but it appears that you >> are >> >> adamant in your refusal to read it. >> >> >> >> I would say that the most important reasons is that macros have >> special >> >> conventions because of hygiene, see >> >> >> http://docs.julialang.org/en/release-0.4/manual/metaprogramming/#hygiene >> >> >> >> Some languages (eg Common Lisp) have the same namespace for macros and >> >> functions, Julia distinguishes them with @. So they are not as >> seamless >> >> as in CL, but at the same time they stand out and warn the reader that >> >> source transformation is going on. There could be arguments for both >> >> choices, but Julia chose this one. >> >> >> >> On Sun, Jun 05 2016, Ford O. wrote: >> >> >> >> > You got it wrong. >> >> > In different words. >> >> > >> >> > Why do we need to specify macro and function block with macro and >> >> function >> >> > keyword? Isnt the '@' symbol enough? >> >> > >> >> > On Sunday, June 5, 2016 at 1:11:48 PM UTC+2, Tamas Papp wrote: >> >> >> >> >> >> A macro is a function that is used to transform (a representation) >> of >> >> >> source code. Consequently, it is called when Julia evaluates your >> >> >> function defintions, not at runtime. Please read >> >> >> file:///usr/share/doc/julia-doc/html/manual/metaprogramming.html >> where >> >> >> you find examples. >> >> >> >> >> >> Regarding you example: it is unclear what you are trying to do, but >> >> >> unless you use the macro keyword, you get an ordinary function, not >> a >> >> >> macro. >> >> >> >> >> >> On Sun, Jun 05 2016, Ford O. wrote: >> >> >> >> >> >> > What makes macro different from function? >> >> >> > >> >> >> > Why is it not possible to do: >> >> >> > foo(e::Expr) = :(&e) >> >> >> > @foo x = x + 5 >> >> >> > >> >> >> > On Friday, June 3, 2016 at 10:05:46 AM UTC+2, Ford O. wrote: >> >> >> > >> >> >> > I think this deserves an own topic. >> >> >> > >> >> >> > You should post here syntax that looks like duplicate or you >> think >> >> it >> >> >> could use already existing keyword. (mark with # identical or # >> >> >> replacement) >> >> >> > Rule of thumb - the less special syntax the better. >> >> >> > >> >> >> > # identical >> >> >> > # replace ' , ' with ' ; ' in arrays ? >> >> >> > [1, 2, 3, 4] >> >> >> > [1; 2; 3; 4] >> >> >> > >> >> >> > # identical >> >> >> > # replace ' = ' with ' in ' in for loops ? >> >> >> > for i = 1:10 >> >> >> > for i in 1:10 >> >> >> > >> >> >> > # replacement >> >> >> > # replace ' -> ' with ' = ' in anonymous functions ? >> >> >> > (a, b, c) -> a + b + c >> >> >> > (a, b, c) = a + b + c >> >> >> >> >> >> >> >> >> >> >> >> -- Erik Schnetter <[email protected]> http://www.perimeterinstitute.ca/personal/eschnetter/
