clojurescript

Clojure(Script) higher order function to accept only functions from certain namespace


I'm trying to define a function (or macro) which should only accept as arguments functions from a certain namespace

(ns ns1)
  (defn high-function[f & params]
    (if (condition on f meta regarding namespace)
        (apply f params)
        (throw (Exception. "not allowed"))))

The function calling high-function should be in another namespace (ns2), and the argument function in yet another namespace (ns3). ns2 requires ns1 and ns3.

I've tried passing the var of the function instead of the function, as the function has no meta but the var has, but cljs.core has no var-get method for me to execute the function afterwards. I could pass both the var and the function, but that's ugly.


Solution

  • Pass the var and use deref to get the function:

    (defn high-function [f & params]
      (if (= 'app.ns3 (ns-name (:ns (meta f))))
        (apply @f params)
        (throw (Exception. "Not allowed."))))
    
    (high-function #'app.ns3/foo ...)