RemoteREPL.jl icon indicating copy to clipboard operation
RemoteREPL.jl copied to clipboard

Allow interpolation into @remote calls

Open KronosTheLate opened this issue 2 years ago • 5 comments

Based on this thread, it is possible to process a macro to allow interpolating of values. My use case is that I want to define something like

function get_ADC(addr, bit)
    @remote(pyconvert(Float32, DAQC2.getADC($addr, $bit)))
end

to make it feel like a pure julia function. If I do not interpolate, I get an error from addr and bit not being defined remotely, which makes sense. However, I can not interpolate the arguments into the call to @remote, due to a RemoteException:


ERROR: RemoteException:
  ERROR: syntax: "$" expression outside quote
  Stacktrace:
   [1] top-level scope
     @ none:1
   [2] eval
     @ ./boot.jl:360 [inlined]
  
Stacktrace:
 [1] (::RemoteREPL.var"#47#48"{RemoteREPL.Connection, Expr})()
   @ RemoteREPL ~/.julia/packages/RemoteREPL/BFqrB/src/client.jl:391
 [2] ensure_connected!(f::RemoteREPL.var"#47#48"{RemoteREPL.Connection, Expr}, conn::RemoteREPL.Connection; retries::Int64)
   @ RemoteREPL ~/.julia/packages/RemoteREPL/BFqrB/src/client.jl:178
 [3] ensure_connected!
   @ ~/.julia/packages/RemoteREPL/BFqrB/src/client.jl:174 [inlined]
 [4] remote_eval_and_fetch
   @ ~/.julia/packages/RemoteREPL/BFqrB/src/client.jl:380 [inlined]
 [5] get_ADC(addr::Int64, bit::Int64)
   @ Main ~/Uni/Semester/8. Sem/Independent Study Activity/scripts/test.jl:24
 [6] top-level scope
   @ ~/Uni/Semester/8. Sem/Independent Study Activity/scripts/test.jl:26

The fix would perhaps be to do something like benchmark-tools, as seen in the linked post, to allow interpolation?

KronosTheLate avatar Jun 19 '23 11:06 KronosTheLate

It looks like I can wrap this into an @eval call, and correctly interpolate into that, as a workaround. The following definition works:

function get_ADC(addr, bit)
    @eval @remote(pyconvert(Float32, DAQC2.getADC($addr, $bit)))
end

KronosTheLate avatar Jun 19 '23 11:06 KronosTheLate

We should definitely do this. I'm surprised I didn't do it already! (If I recall, I did this exact thing in the SQLStrings/SQLREPL packages.)

c42f avatar Jun 23 '23 05:06 c42f

We should definitely do this.

Do you know the appropriate way to do this? I am very inexperienced around macros.

KronosTheLate avatar Jun 25 '23 13:06 KronosTheLate

I know how to do it, I just don't have that much time :sweat_smile:

We should try something like

  1. After parsing the expression, search it recursively for Expr(:$, ex) expressions
  2. call eval(ex) for each of these
  3. replace the Expr(:$, ex) part of the expression with the result of the eval
  4. Serialize the resulting expression and send it down the pipe to the other end

c42f avatar Jul 01 '23 23:07 c42f

Sounds good. I am also pressed for time, but I think it is perfectly fine to keep this issue open as a FR. Perhaps adding a temporary paragraph in the docs about the workaround I found, in case this issue remains open for months/years?

KronosTheLate avatar Jul 02 '23 02:07 KronosTheLate