Search code examples
clojureclojurescriptoverloadingprivate-functions

overloaded private function which is private in clojure


Usually I have the same structure of my functions:

(defn func-name
  ([] (some actions))
  ([ar] (some actions))
  ([ar aar] (some actions)))

And usually only one of this variant is public. But as You can see from my entry - all my function is public because of using defn instead of defn-. But defn- hide all function, including all overloaded.

Is there any way to 'hide' only part of overloaded function?

For example, I want to hide an func-name with arity of one and two arguments.

Ofcorse I can hide overloaded function inside one defn like this:

(defn awesome[]
  (let [func (fn some-func ([] (some actions))
               ([ar] (some actions)))]
    (func)))

But I think it's a little bit messy and I'm sure there have to be a way to solve it.

Thanks!


Solution

  • As I know this visibility is defined by :private flag in var's meta. So this two expressions are equal:

    (defn ^:private foo [] "bar")
    (defn- foo [] "bar")
    

    So I think you can control a visibility only of a whole var.

    I can suggest to use different function names for public and private spaces. I.e func-name for public one and func-name- for private.