首页 > 解决方案 > 查找包含表单的顶级表单的宏

问题描述

有没有办法使用宏访问顶级表单?大意是:

(defmacro which-defn []
     ....)

(defn hello []
  (which-defn))

(defn world []
  (which-defn))

(hello) => 'hello
(world) => 'world

标签: clojure

解决方案


这个函数已经存在于Tupelo 库中。您不需要宏:

(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require [tupelo.misc :as misc]))

(defn hello []
  (misc/fn-info))

(dotest
  (is= (hello)
    {:class-name  "tst.demo.core$hello",
     :file-name   "core.clj",
     :method-name "invokeStatic",
     :line-num    6,
     :ns-name     "tst.demo.core",
     :fn-name     "hello"}))

推荐阅读