Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to include apostrophe in clojure require?

Tags:

syntax

clojure

What is the difference between:

(require '[some-package.sub as some])
(require 'example.core)

and

(require [some-package.sub as some])
(require example.core)

When should I use one over the other? Only the one with apostrophe works in an REPL environment.

like image 712
fyquah95 Avatar asked Jul 12 '15 21:07

fyquah95


1 Answers

require is a function (check with (source require) in a REPL). Thus is evaluates its parameters before its own evaluation.

At the time you call require the symbol some-package.sub resolves (points) to nothing, so you get a "Unable to resolve symbol: the-symbol-that-points-to-nothing in this context..."

However, if you quote the said symbol we can get to the actual evaluation of require. Thus require, since it is a function, necessitates quoted forms.

Now ns is a macro (check with (source ns) in a REPL). Thus is doesn't evaluate its parameters before expansion. Technically it could take free symbols or quoted forms but it was decided to use raw free symbols. (ns lalala (:require [some-package.sub :as some]))

No-o-o-o-ow, when you call require (function) with a, you will actually require the symbol which is held by the var which a resolves to.

Example:

(def a 'B)
(require a)
FileNotFoundException Could not locate B__init.c;ass or B.clj on the classpath[...]

...that's because require is a function.

Hope it helped.

like image 68
freakhill Avatar answered Sep 20 '22 12:09

freakhill