Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I refer another namespace and expose its functions as public for the current ns?

I thought use would do it but it seems the mapping created in the current namespace is not public. Here is an example of what I'd like to achieve:

(ns my-ns
  (:use [another-ns :only (another-fct)]))

(defn my-fct
  []
  (another-fct 123)) ; this works fine

Then I have another namespace like this:

(ns my-ns-2
   (:require [my-ns :as my]))

(defn my-fct-2
  []
  (my/another-fct 456)) ; this doesn't work

I would like to do that because another-ns is a library to access a database. I would like to isolate all the calls to this library in a single namespace (my-ns), this way all the DB dependent functions would be isolated in a single namespace and it becomes easier to switch to another DB if needed.

Some of the functions of this library are just fine for me but I'd like to augment others. Let's say the read functions are fine but I'd like to augment the write functions with some validation.

The only way I see so far is to hand-code all the mapping into my-ns even for the functions I don't augment.

like image 686
Damien Avatar asked Jan 19 '11 05:01

Damien


1 Answers

One way to do this selectively (specifying each function explicitly) is to use something like Zach Tellman's Potemkin library. An example of it's use is found in the lamina.core namespace which serves as the public entry point for Lamina, importing the key public functions from all other internal namespaces.

You can also use clojure.contrib.def/defalias:

(use 'clojure.contrib.def/defalias)
(defalias foo clojure.string/blank?)
(foo "")
like image 146
Alex Miller Avatar answered Oct 11 '22 18:10

Alex Miller