具有多个 return 表达式的 Julia 宏

Julia macros with multiple return expressions

我有点厌倦了为输入类型的不同排列定义函数,例如

f(x::MyType1, y::MyType2) = x.data + y.data
f(y::MyType2, x::MyType1) = x.data + y.data

所以我决定尝试一个 return 上述两个定义的宏。

我能够制作一个切换参数输入的宏,但我无法将其设为 return 多个函数定义。

所以这有效:

julia> macro argrev(ex)
          if (ex.head == :(=)) && (ex.args[1].head == :call)
              ex_ = copy(ex)
              args = ex_.args[1].args
              args[2:3] = args[[3, 2]]
              return ex_
          end
          return ex
       end
@argrev (macro with 1 method)

julia> @argrev f(x::Int, y::Float64) = x + y
f (generic function with 1 method)

julia> f(2, 3.5)
MethodError: no method matching f(::Int64, ::Float64)

julia> f(3.5, 2)
5.5

不过,我不知道如何 return 同时 exex_。这是我的尝试之一:

julia> macro argrev1(ex)
           if (ex.head == :(=)) && (ex.args[1].head == :call)
               ex_ = copy(ex)
               args = ex_.args[1].args
               args[2:3] = args[[3, 2]]
               return quote
                   ex
                   ex_
               end
           else
               return ex
           end
       end
@argrev1 (macro with 1 method)

julia> @argrev1 f(x::Int, y::Float64) = x + y
UndefVarError: ex_ not defined

这个错误是怎么回事,我如何 return 两个表达式 以其他方式实现我在这里尝试做的事情?

编辑: 似乎 this 是相关的,但我不太明白我应该如何适应我的情况。

quote块中,需要插入exex_,如

 return quote
    $ex
    $ex_
 end