Would it be possible to override the ‘require’ command so that it will try to download a certain resource if it was not found on the local machine. For example:
(require 'examples.introduction)
; if not found => download from the net
; (url: http://media.pragprog.com/titles/shcloj/code/examples/introduction.clj)
You could override the
requirefunction and of course the overriden variant could download stuff if the namespace it was asked for is not available on the classpath. Overriding the way:requireworks in(ns ...)forms is, AFAIK, impossible for now due to the way in whichnsis handled.Note that such a ‘downloading
require‘ wouldn’t be very helpful if you wanted to place new paths on the classpath (including new jars), as classpath injection doesn’t work reliably in Clojure (due to JVM issues). There isclojure.core/add-classpath… but it’s been marked as deprecated since forever now, its use is strongly discouraged, there are no guarantees that it will work for you and this situation isn’t likely to change anytime soon. On the other hand, if you wanted to put new source files in a directory which was already present on the classpath, then that should work fine.In case you do want to play around with overriding
require, if you have afoonamespace, you could doThen define your own require, using
clojure.core/requirewhen appropriate:clojure.contrib.find-namespacesnamespace might be helpful in finding out what’s available on the classpath. (Or you could use thethe-nsfunction and see if it throws an exception after an initial attempt at requiring the namespace throughclojure.core/require.)Note that the
bindingapproach which might come to mind first ((binding [require ...] ...)) will not work, sincerequirenormally resolves to a Var interned in theclojure.corenamespace and Vars from namespaces whose names start withclojureare currently directly linked by the compiler (meaning no actual Var lookup is performed at runtime, so rebinding of those Vars has no effect on the code).The
(:refer-clojure :exclude [require])in thensform for your namespace preventsrequirefrom resolving toclojure.core/requireand leaves you free to define a Var of that name in your own namespace. As mentioned above, that doesn’t prevent theclojure.core/requireVar from being accessible if you type out the fully qualified the symbol.