Julia:如何在与输入变量匹配的函数内创建符号?

Lan*_*don 2 macros julia

我有一个Julia函数,它接受一些输入参数并使用它们来执行多个操作.为了使其中一个操作正常工作,我需要能够计算与用户输入匹配的符号.例如,函数看起来像这样:

function func(arg1,arg2)

    symb_arg1 =  ## get the symbol for input into arg1
    symb_arg2 =  ## get the symbol for input into arg2

    println(symb_arg1)
    println(symb_arg2)

    ## Do some operations using arg1, arg2, symb_arg1, symb_arg1

end
Run Code Online (Sandbox Code Playgroud)

我希望实现以下行为:

a = 25
b = rand(27,55,18)
func(a,b)  ## prints :a and :b
Run Code Online (Sandbox Code Playgroud)

这里的困难是让函数计算包含变量实际名称的符号,而不是变量的值.这篇文章提供了以下几乎可以实现我想要的宏:

macro mymacro(ex)
    Expr(:quote,ex) # this creates an expression that looks like :(:(x + 2))
end
Run Code Online (Sandbox Code Playgroud)

此宏适用于执行以下操作:

a = rand(27,15)
symb_a = @mymacro(a)  ## prints :a
Run Code Online (Sandbox Code Playgroud)

但是,在我的函数中使用此宏将不会产生所需的效果.具体来说,如果我将我的函数定义为:

function func_bad(arg1,arg2)

    symb_arg1 = @mymacro(arg1)
    symb_arg2 = @mymacto(arg2)

    println(symb_arg1)
    println(symb_arg2)

    ## Do some operations using arg1, arg2, symb_arg1, symb_arg1

end
Run Code Online (Sandbox Code Playgroud)

然后运行命令:

a = 25
b = rand(27,55,18)
func_bad(a,b)  ## prints :arg1 and :arg2 (instead of the desired :a and :b)
Run Code Online (Sandbox Code Playgroud)

当然,一个简单(但不是那么优雅)的解决方案是为函数添加额外的输入参数,以便用户负责创建符号.然而,这是最后的手段.我希望该功能能够自动创建符号.有关如何修改我的函数或宏以实现此行为的任何想法?

Jef*_*son 5

简单的答案是,这是不可能的; 有一个抽象障碍,阻止函数查看其调用者的实现细节.他们得到的只是价值观,这是强大而清晰的计划的关键属性.

您可以做的一件事是编写一个宏来转换调用站点:

@with_syms func(a, b)
Run Code Online (Sandbox Code Playgroud)

变成类似的东西

func((:a,a), (:b,b))
Run Code Online (Sandbox Code Playgroud)

将符号 - 值对传递给函数.

另一个稍微不同的设计是为需要此行为的所有函数提供宏包装器,以便调用看起来像@func(a,b).您可以将参数列表转换分解为辅助函数; 每个宏看起来都像

macro func(args...)
    :(func($(pair_with_symbols(args)...)))
end
Run Code Online (Sandbox Code Playgroud)