2020/05/30 Python東海/JuliaTokaiミニ合同勉強会
@ OSC2020 Online/Nagoya
antimon2(後藤 俊介)
みなさん…
じゃぁ当然…
衛生的マクロ(hygienic macro)は、マクロ展開によって導入される識別子と展開先のプログラムにある識別子が衝突して、意図とは異なる意味になることを防ぐ機構を持つマクロである。
出典:「多言語に対応した衛生的マクロ機構導入方式」(DOI)
macro time_ns(ex)
return quote
t0 = time_ns()
val = $(esc(ex))
t1 = time_ns()
println("elapsed time: ", Int(t1-t0), " nanoseconds")
val
end
end
julia> @time_ns 1 + 2
elapsed time: 5604 nanoseconds
3
julia> @time_ns time_ns()
elapsed time: 9684 nanoseconds
0x00049b777cb1b4b1
julia> @time_ns t0 += 1000000
# => ???
julia> @time_ns t0 += 1000000
ERROR: UndefVarError: t0 not defined
Stacktrace:
[1] top-level scope at REPL[XX]:YY
julia> @macroexpand @time_ns t0 += 1000000
quote
#= REPL[XX]:YY =#
#12#t0 = (Main.time_ns)()
#= REPL[XX]:YY =#
#13#val = (t0 += 1000000)
#= REPL[XX]:YY =#
#14#t1 = (Main.time_ns)()
#= REPL[XX]:YY =#
(Main.println)("elapsed time: ", (Main.Int)(#14#t1 - #12#t0), " nanoseconds")
#= REPL[XX]:YY =#
#13#val
end
t0
val
t1
)が変な名前(#12#t0
#13#val
#14#t1
)に変換されているtime_ns()
→(Main.time_ns)()
)されているesc(ex)
と書かれた部分は勝手に変換されていない(t0 += 1000000
)⇩
esc(ex)
に渡されたモノだけは勝手に変換されずにそのまま使用される!⇩
でコロナ禍を乗り切ろう!
ご清聴ありがとうございます。