简体   繁体   中英

How can I write this Clojure macro more idiomatically?

(defmacro get-color [color-name]
  `@(thi.ng.color.core/as-int32 (var-get (resolve (symbol "thi.ng.color.core"
                                            (str '~color-name))))))

I like to avoid using the (var-get (resolve (symbol ... (str '~parem)))) . Something like thi.ng.color.core/(~color-name) .

(I am using this macro in a very small personal project, and I don't care if it's really bad practice to create a macro for this use case. Though I like knowing why it would be problematic in bigger projects.)

(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
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM