Skip to content

Commit

Permalink
[lazy] added (optional) detection of conditional test of LazySeq, bui…
Browse files Browse the repository at this point in the history
…ld with:

ant -Dclojure.assert-if-lazy-seq=true
patch from Chouser
  • Loading branch information
richhickey committed Feb 14, 2009
1 parent db27101 commit 8c59f86
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build.xml
Expand Up @@ -11,6 +11,7 @@
<property name="build" location="classes"/>
<property name="clojure_jar" location="clojure.jar"/>
<property name="slim_jar" location="clojure-slim.jar"/>
<property name="clojure.assert-if-lazy-seq" value=""/>

<target name="init" depends="clean">
<tstamp/>
Expand All @@ -28,6 +29,7 @@
<java classname="clojure.lang.Compile"
classpath="${build}:${cljsrc}">
<sysproperty key="clojure.compile.path" value="${build}"/>
<sysproperty key="clojure.assert-if-lazy-seq" value="${clojure.assert-if-lazy-seq}"/>
<arg value="clojure.core"/>
<arg value="clojure.main"/>
<arg value="clojure.set"/>
Expand Down
20 changes: 19 additions & 1 deletion src/clj/clojure/core.clj
Expand Up @@ -35,6 +35,10 @@
#^{:macro true}
fn (fn* fn [& decl] (cons 'fn* decl)))

(def
#^{:macro true}
if (fn* if [& decl] (cons 'if* decl)))

(def
#^{:arglists '([coll])
:doc "Returns the first item in the collection. Calls seq on its
Expand Down Expand Up @@ -291,6 +295,20 @@

(. (var defmacro) (setMacro))

(defmacro assert-if-lazy-seq? {:private true} []
(let [prop (System/getProperty "clojure.assert-if-lazy-seq")]
(if prop
(if (clojure.lang.Util/equals prop "") nil true))))

(defmacro if [tst & etc]
(if* (assert-if-lazy-seq?)
(let [tstsym 'G__0_0]
(list 'let [tstsym tst]
(list 'if* (list 'clojure.core/instance? clojure.lang.LazySeq tstsym)
(list 'throw (list 'new Exception "LazySeq used in 'if'"))
(cons 'if* (cons tstsym etc)))))
(cons 'if* (cons tst etc))))

(defmacro when
"Evaluates test. If logical true, evaluates body in an implicit do."
[test & body]
Expand Down Expand Up @@ -1642,7 +1660,7 @@
([coll]
(sort compare coll))
([#^java.util.Comparator comp coll]
(when (and coll (not (zero? (count coll))))
(when (seq coll)
(let [a (to-array coll)]
(. java.util.Arrays (sort a comp))
(seq a)))))
Expand Down
2 changes: 1 addition & 1 deletion src/clj/clojure/core_proxy.clj
Expand Up @@ -155,7 +155,7 @@
meths (concat
(seq (. c (getDeclaredMethods)))
(seq (. c (getMethods))))]
(if meths
(if (seq meths)
(let [#^java.lang.reflect.Method meth (first meths)
mods (. meth (getModifiers))
mk (method-sig meth)]
Expand Down
2 changes: 1 addition & 1 deletion src/jvm/clojure/lang/Compiler.java
Expand Up @@ -40,7 +40,7 @@ public class Compiler implements Opcodes{
static final Symbol DEF = Symbol.create("def");
static final Symbol LOOP = Symbol.create("loop*");
static final Symbol RECUR = Symbol.create("recur");
static final Symbol IF = Symbol.create("if");
static final Symbol IF = Symbol.create("if*");
static final Symbol LET = Symbol.create("let*");
static final Symbol DO = Symbol.create("do");
static final Symbol FN = Symbol.create("fn*");
Expand Down

0 comments on commit 8c59f86

Please sign in to comment.