Thanks. I am just trying to understand the various contexts in which macros 
may appear, so the examples are contrived and do not imply I will 
eventually program that way.

Is there a clear language spec for macros, or is it still evolving? I 
understand that relying on implementation to learn a language feature can 
be risky.

Consider this:

function retMacro()

macro myMacro(anexpr)

parse(anexpr)

end

end

When I call retMacro(), I get an error saying "Macro definition not allowed 
in local scope". What I tried to do in my earlier example (pre post) is to 
hide the macro definition in a string and then parse and evaluate it to get 
the same effect as the above function. Obviously the behaviour is 
different. 

You have said that 

julia> typeof(mymacro)
#@myMacro


Does it mean every macro is its own type? Should it not be a predefined 
type like "macro" (just as there is type for functions)? If macros are 
"pre-processed away", then there can't be an independent existence for them?

Finally, in 0.5-dev, if "returnMacro()" works as expected, then calling 
myMacro("1+1") should return 2 (and not the parsed structure), isn't it? 
The reason is does an additional evaluation?

As I had mentioned in my first post, these questions are based on my 
experience with Lisp and I am trying to digest the differences, if any. 

-Rangarajan

On Tuesday, July 5, 2016 at 9:35:52 PM UTC+5:30, Isaiah wrote:
>
> Anonymous functions are a bit incomplete / not-fully-integrated in 0.4, 
> and I think this falls out as a consequence.
>
> You can simulate the result you want with:
>
> julia> function returnMacro()
>          eval(parse("""
>          macro myMacro(anexpr)
>            parse(anexpr)
>          end
>          """))
>          eval(symbol("@myMacro"))
>        end
>
> julia> mymacro = returnMacro()
> (anonymous function)
>
> julia> mymacro("1+1")
> :(1 + 1)
>
> However, there have been significant improvements to anonymous functions 
> in the nightly (0.5-dev), and the result is what you expect:
>
> julia> function returnMacro()
>          eval(parse("""
>          macro myMacro(anexpr)
>            parse(anexpr)
>          end
>          """))
>        end
> returnMacro (generic function with 1 method)
>
> julia> mymacro = returnMacro()
> @myMacro (macro with 1 method)
>
> julia> typeof(mymacro)
> #@myMacro
>
> julia> mymacro("1+1")
> :(1 + 1)
>
> That said, `mymacro` here is purely a function handle -- totally identical 
> to writing `mymacro(expr) = parse(expr)`. There is no special behavior.
>
> (are you trying to do something specific?)
>
> On Tue, Jul 5, 2016 at 11:04 AM, Rangarajan Krishnamoorthy <
> [email protected] <javascript:>> wrote:
>
>> Consider this function:
>>
>> function returnMacro()
>>   eval(parse("""
>>   macro myMacro(anexpr)
>>     parse(anexpr)
>>   end
>>   """))
>> end
>>
>> I assign the function's result to a variable:
>> mymacro = returnMacro()
>>
>> Then, typeof(mymacro) returns "Void". Is this correct? Because macros are 
>> parse time entities, wouldn't it be better for "eval" to raise some sort of 
>> exception?
>>
>> -Rangarajan
>>
>
>

Reply via email to