Error using @sync/@async within macro, help is highly appreciated

If I understand things correctly - and I may not, because macro hygiene is difficult - var"##sync#48" comes from expansion of the @sync macro, and then the hygiene pass of the outer @waituntil turns it into Main.:(var"##sync#48"). Which it shouldn’t do imho, because it is clearly a local variable. So, maybe a bug? Let’s see what others say on the matter.

With this macro there shouldn’t be any unintentional side effects when the returned expression is escaped, and nexttime is explicitely hygiened(?)

 macro waituntil(nexttime_from_now, sleeptime_from_diff)
           nexttime = gensym("nexttime")
           esc(quote
               $nexttime = $nexttime_from_now()
               @sync @async begin
                   diff = $nexttime - Dates.now()
                   while diff > Dates.Millisecond(0)
                       sleep($sleeptime_from_diff(diff))
                       diff = $nexttime - Dates.now()
                   end
               end
           end)
       end

I honestly fail to see why this ought to be a macro though.

1 Like