Clojure defmacro 丢失元数据

发布于 2024-12-10 03:03:59 字数 406 浏览 0 评论 0原文

我正在尝试创建一个带有类型提示的 defsa String 的小 Clojure 宏:

(defmacro def-string [name value]
  `(def ^String ~name ~value))

(def-string db-host-option "db-host")

当我宏扩展它时,类型提示丢失了:

(macroexpand '(def-string db-host-option "db-host"))
;=> (def db-host-option "db-host")

别介意类型提示的智慧这。

为什么宏会丢失元数据?如何编写此宏或任何包含元数据的宏?

I am trying to create a little Clojure macro that defs a String with a type hint:

(defmacro def-string [name value]
  `(def ^String ~name ~value))

(def-string db-host-option "db-host")

When I macroexpand it, the type hint is lost:

(macroexpand '(def-string db-host-option "db-host"))
;=> (def db-host-option "db-host")

Never mind the wisdom of type hinting this.

Why is the macro losing the metadata? How do I write this macro, or any that includes metadata?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

^ 是一个读取器宏。 defmacro 永远看不到它。提示被放在列表(unquote name)中。例如,将 (meta ^String 'x)(meta ' ^String x) 进行比较以查看效果。

您需要将提示放在符号上。

(defmacro def-string
  [name value]
  `(def ~(vary-meta name assoc :tag `String) ~value))

以及用法:

user=> (def-string foo "bar")
#'user/foo
user=> (meta #'foo)
{:ns #<Namespace user>, :name foo, :file "NO_SOURCE_PATH", :line 5, :tag java.lang.String}

^ is a reader macro. defmacro never gets to see it. The hint is put on the list (unquote name). Compare for example (meta ^String 'x) with (meta ' ^String x) to see the effect.

You need to put the hint on the symbol.

(defmacro def-string
  [name value]
  `(def ~(vary-meta name assoc :tag `String) ~value))

And the usage:

user=> (def-string foo "bar")
#'user/foo
user=> (meta #'foo)
{:ns #<Namespace user>, :name foo, :file "NO_SOURCE_PATH", :line 5, :tag java.lang.String}
又爬满兰若 2024-12-17 03:03:59

元数据不会出现在宏展开中,因为它应该是“不可见的”。

如果宏正确(事实并非如此),您应该能够调用 (meta #'db-host-option) 来检查 var 上的元数据。

请注意, (def sym ...) 在从符号接收到的 var 上插入元数据。但是 ^Tag ~name 在 ~name (取消引用名称)上设置元数据,而不是在绑定到名称的传入符号上设置元数据。它不能做任何其他事情,因为 ^Tag ... 处理是由读取器完成的,一旦宏扩展开始,读取器就已经完成了。

你想要类似的东西

(defmacro def-string [name value]
  `(def ~(with-meta name {:tag String}) ~value))


user> (def-string bar 1)
#'user/bar
user> (meta #'bar)
{:ns #<Namespace user>, :name bar, :file "NO_SOURCE_FILE", :line 1, :tag java.lang.String}

Metadata doesn't show up in a macroexpand since it's supposed to be "invisible".

If the macro is correct (which it isn't) you should be able to call (meta #'db-host-option) to inspect the meta data on the var.

Note that (def sym ...) inserts metadata on the var that it receives from the symbol. But ^Tag ~name sets the meta data on ~name (unquote name), not on the passed in symbol bound to name. It can't do anything else since ^Tag ... processing is done by the reader, which is already finished once macro expansion starts.

You want something like

(defmacro def-string [name value]
  `(def ~(with-meta name {:tag String}) ~value))


user> (def-string bar 1)
#'user/bar
user> (meta #'bar)
{:ns #<Namespace user>, :name bar, :file "NO_SOURCE_FILE", :line 1, :tag java.lang.String}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文