【问题标题】:How can I write this Clojure macro more idiomatically?如何更惯用地编写这个 Clojure 宏?
【发布时间】:2018-11-21 22:45:29
【问题描述】:
(defmacro get-color [color-name]
  `@(thi.ng.color.core/as-int32 (var-get (resolve (symbol "thi.ng.color.core"
                                            (str '~color-name))))))

我喜欢避免使用(var-get (resolve (symbol ... (str '~parem))))。像thi.ng.color.core/(~color-name) 这样的东西。

(我在一个非常小的个人项目中使用这个宏,我不在乎为这个用例创建一个宏是否真的很糟糕。虽然我想知道为什么它会在更大的项目中出现问题。)

【问题讨论】:

    标签: clojure macros lisp lisp-macros


    【解决方案1】:
    (require 'thi.ng.color.core)
    
    (defmacro get-color
      [color-name]
      (let [sym (symbol "thi.ng.color.core"
                        (str color-name))]
        `@(thi.ng.color.core/as-int32 ~sym)))
    
    (comment
      (get-color "RED") ;;=> 4294901760
      (get-color RED) ;;=> 4294901760
      )
    

    【讨论】:

    • 我的解决方案是这样工作的(get-color RED)。这个甚至不适用于(get-color 'RED)
    • 您没有指定您的用途。我更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2020-05-29
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 2023-03-07
    • 2018-05-23
    • 1970-01-01
    相关资源
    最近更新 更多