如何在 Julia 中创建用于创建符号的宏 [英] How to create a macro for creating a symbol in Julia

查看:16
本文介绍了如何在 Julia 中创建用于创建符号的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 字符串文字宏 在 Julia 中创建一个 symbol,使 s"x":x 相同.它不起作用:

I am trying to create a string literal macro in Julia to create a symbol, so that s"x" is the same as :x. It does not work:

julia> macro s_str(p)
           symbol(p)
       end

julia> s'x'
ERROR: s not defined

julia> s"x"
ERROR: x not defined

推荐答案

原因是宏观卫生.你可以做任何一个

The reason is macro hygiene. You can do either

macro s_str(p)
  quote
    symbol($p)
  end
end

这很容易阅读,或者做更复杂但等效的.

which is easy to read, or do the more complicated but equivalent.

macro s_str(p)
  esc(:(symbol($p)))
end

这篇关于如何在 Julia 中创建用于创建符号的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆