zcaudate
zcaudate

Reputation: 14258

macro to find the top level form containing the form

Is there a way to access the top level form with a macro? Something to the effect of:

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

(defn hello []
  (which-defn))

(defn world []
  (which-defn))

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

Upvotes: 1

Views: 78

Answers (1)

Alan Thompson
Alan Thompson

Reputation: 29986

This function already exists in the Tupelo library. You don't need a macro:

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

Upvotes: 1

Related Questions