The basic reason for this is that it's fairly common to write something
like:

quote
  let x = 5
    "x is $x"
  end
end |> eval #> "x is 5"

Where the $x referred to is the one available in the context of the created
expression. If strings were treated similarly to other code here you'd have
to write something more like:

quote
  let x = 5
    @eval $(Expr(:$, :x)) + 1
  end
end |> eval #> 6

Code that generates code that generates strings is generally more common
than code that generates code that generates code, so that roughly explains
why the behaviour in both cases is different.

The pattern I've used when I want what you're going for is

 r = get_oauth($"https://api.twitter.com/1.1/$endp.json";, options)

The first $ will directly embed the string object, rather than a string
literal, and the second will interpolate the value of endp. This is
essentially the equivalent to your version – the difference between that
and Jacob's is whether you construct the string at compile or run time. In
your case that's unlikely to matter so you should go with whatever's most
readable to you.

On 18 November 2014 02:59, Randy Zwitch <[email protected]> wrote:

> It would appear so...I swear that I tried that, but I guess I didn't try
> that permutation!
>
> So what's it about the @eval macro that doesn't allow for regular string
> interpolation, that I have to use the string() function instead of an
> inline $?
>
> On Monday, November 17, 2014 9:50:55 PM UTC-5, Jacob Quinn wrote:
>>
>> Can you just do
>>
>>
>> funcname = (:get_help_configuration, :get_help_languages,
>> :get_help_privacy, :get_help_tos, :get_application_rate_limit_status)
>> endpoint = ("help/configuration.json", "help/languages.json",
>> "help/privacy.json",  "help/tos.json", "application/rate_limit_
>> status.json")
>>
>> for (func, endp) in Dict(funcname, endpoint)
>> @eval begin function ($func)(; options=Dict{String, String}())
>>
>>         r = get_oauth(string("https://api.twitter.com/1.1/";, $endp),
>> options)
>>
>>         return r.status == 200 ? JSON.parse(r.data) : r
>>
>>         end
>>     end
>> end
>>
>>
>> ?
>>
>> On Mon, Nov 17, 2014 at 9:35 PM, Randy Zwitch <[email protected]>
>> wrote:
>>
>>> I've seen this type of function generation in other packages, and wanted
>>> to try it for myself. This file in Twitter.jl has 5 functions with the same
>>> overall structure:
>>>
>>> https://github.com/randyzwitch/Twitter.jl/blob/master/src/help.jl
>>>
>>>
>>> Here's what I ended up doing, which works, but I've got no idea why I
>>> had to write the string interpolation the way I did. I went through many
>>> permutations here, so if someone could explain why the `:($(string("
>>> https://api.twitter.com/1.1/";, $endp))` line needs to be written that
>>> way for string interpolation, it would be appreciated. And any improvements
>>> in general are welcome.
>>>
>>>
>>>
>>> funcname = (:get_help_configuration, :get_help_languages,
>>> :get_help_privacy, :get_help_tos, :get_application_rate_limit_status)
>>> endpoint = ("help/configuration.json", "help/languages.json",
>>> "help/privacy.json",  "help/tos.json", "application/rate_limit_
>>> status.json")
>>>
>>> for (func, endp) in Dict(funcname, endpoint)
>>> @eval begin function ($func)(; options=Dict{String, String}())
>>>
>>>         r = get_oauth(:($(string("https://api.twitter.com/1.1/";,
>>> $endp))), options)
>>>
>>>         return r.status == 200 ? JSON.parse(r.data) : r
>>>
>>>         end
>>>     end
>>> end
>>>
>>> Thanks!
>>>
>>
>>

Reply via email to