My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members

A typical situation in a Clojure project is that several namespaces start with the same combination of use-require-import, or at least have much of these references to the outside world in common. However, Clojure currently has no way to define a combination of external references once and then re-use and extend it in individual namespaces.

Nstools is an attempt to provide a solution: it permits any namespace to serve as a template for constructing other namespaces. An enhanced version of the ns macro adds the clauses "like", "clone" and "remove". Suppose you have a namespace foo that serves as the template:

(ns foo
  (:use [clojure.contrib.monads :exclude (cont-m)])
  (:require [clojure.contrib.seq :as su])
  (:import (java.io InputStream)))

You can then create a namespace bar and make it "like foo". This takes over all the references created by the use, require, and import clauses in foo, including aliases defined by :require ... :as:

(clojure.core/use 'nstools.ns)
(ns+ bar
  (:like foo))

; Use the alias su
(prn (su/flatten [[1 2] [3 4]]))

A :clone clause is the same as :like followed by :use for the same namespace.

A :remove clause makes it possible to remove references from a namespace. It doesn't matter if those references were created by :like, :clone, or :use. The references need not even exist. It is thus possible to list in a :remove clause all symbols that will be defined in the namespace later on, guaranteeing that there will be no name clashes with references to other namespaces, in particular if symbols are added to later versions of these namespaces.

The :from clause provides an alternative cleaner syntax for the functionality offered by :use. The format is (:from namespace sym1 sym2 ...), which is equivalent to (:use :only (sym1 sym2 ...)). It is in fact not fully equivalent, because :from allows the specified symbols to overwrite previously existing references. Another format, to be used with caution, is (:from namespace :all), which is equivalent to (:use namespace).

The jar files for clj-nstools releases are at clojars.org and in the download section of this site. The dependencies are

Powered by Google Project Hosting