>
> can someone help me understand the difference between:

Potentially-interpolatable strings parse as expressions (try your example
w/out the dollar-sign), except when used in a string-literal macro -- in
that case, the dollar-sign is escaped. For example:

# helper macro...

julia> macro L_str(s)
           s
       end

julia> ex = parse(L" r\"\"\"x = $y + $z\"\"\" ")
:(@r_str "x = \$y + \$z")


vs.

julia> ex = parse(L" \"\"\"x = $y + $z\"\"\" ")
:("x = $(y) + $(z)")


Why? Well, the short answer is: because interpolation is evil. The slightly
longer answer is: because the system needs to preserve interpolatable
strings through (potentially multiple levels of) expansions. See Stefan's
explanation here, though some implementation details may have changed since
then:

https://github.com/JuliaLang/julia/issues/455#issuecomment-4176276

And other discussion of these issues:

https://github.com/JuliaLang/julia/issues/3150
https://github.com/JuliaLang/julia/issues/11567
https://github.com/JuliaLang/julia/issues/11764
https://github.com/JuliaLang/julia/pull/11815

...

Practically, your best bet may be to check whether the argument is a string
or an expression. If the latter, check if expr.head is :string.

julia> ex = parse(" \"x = \$y\ + \$z\" ")
:("x = $(y) + $(z)")
ulia> ex.head
:string

julia> ex.args
4-element Array{Any,1}:
 "x = "
 :y
 " + "
 :z



On Sun, Jan 3, 2016 at 5:45 AM, Eric Forgy <eric.fo...@gmail.com> wrote:

> Hi,
>
> Resurrecting an old thread :)
>
> Hmm... I was recently playing around with MATLAB.jl and fell in love with
> the mat"" syntax. I've also been playing around with getting Julia and JS
> to talk to each other (and, by extension, JS talking to Matlab via Julia)
> and I'd really like to be able to do things like:
>
> julia>js"""
> console.log("Hello from Julia!")
> """
>
> Reading Stefan's suggestions, I was able to get this to work (without
> Blink.jl or Electron):
>
> julia>js("""
> console.log("Hello from Julia!")
> """)
>
> My inability to get the non-standard string literal to work with
> interpolation is bugging me.
>
> As a start, can someone help me understand the difference between:
>
> julia> @js_str "x = $x"
>
> and
>
> julia>js"x = $x"
>
> As a little experiment, I tried the following:
>
> function js(ex::AbstractString)
>     println("AbstractString:")
>     println(ex)
> end
>
> function js(ex::Expr)
>     println("Expression:")
>     println(ex)
> end
>
> macro js_str(ex)
>     js(ex)
> end
>
> with the following results:
>
> julia> @js_str "x = $x"
> Expression:
> "x = $(x)"
>
> julia> js"x = $x"
> AbstractString:
> x = $x
>
> Why is the first one an expression and the second one a string? I kind of
> expected the two lines above to be the same.
>
> I did peak under the hood of MATLAB.jl to see how @mat_str was defined and
> found scary words like "DumbParser" and "Hack to do interpolation" followed
> by tons of scary code. I suppose this is also what jsexprs.jl is all about
> in Blink.jl too.
>
> There's got to be a better way? :)
>

Reply via email to