You are correct -- it's weird, because I'm sure I tested it several times 
before posting, but I now get '5', as you suggest.

On Thursday, May 15, 2014 7:51:15 AM UTC-4, Mike Innes wrote:
>
> I think your second snippet must have gotten a bit muddled, since `expr` 
> should end up with the value 5.
>
> macro createVar(name, value)
>   quote
>     $name = $value;
>   end
> end
>
> expr = @createVar foo 5
> # This is equivalent to `expr = (foo = 5)`, *not* `expr = :(foo = 5)`
>
> expr == 5
>
> If you do want `createVar` to return an expression, it should be a 
> function instead of a macro. Maybe try running the example again to check 
> it's behaving in the expected way?
>
>
> On Thursday, 15 May 2014 12:29:13 UTC+1, Abe Schneider wrote:
>>
>> As an experiment I wrote a simple macro to set a variable:
>>
>> macro createVar(name, value)
>>   eval(quote
>>     $name = $value;
>>   end)
>> end
>>
>> which works as expected:
>>
>> @createVar foobar 5;
>> println("foobar = $foobar"); # "foobar = 5" (OK)
>>
>> However, if I instead do:
>>
>> macro createVar(name, value)
>>   quote
>>     $name = $value;
>>   end
>> end
>>
>> expr = @createVar(foobar, 5);
>>
>> println("$expr"); # "foobar = 5" (OK)
>>
>> # now evaluate the expression to do the actual assignment
>> eval(expr);
>> println("foobar = $foobar");
>>
>> I get "ERROR: foobar not defined". I would expect that if I do the eval 
>> outside of the macro I should get the same result as doing the eval inside 
>> the macro. Is this expected behavior?
>>
>> I should add that I'm using a version of 0.3 from the repository.
>>
>

Reply via email to