Skip to content

Commit ab9a567

Browse files
committed
last-one-in-wins for refers and interns, warns on replacement
1 parent 68a14c8 commit ab9a567

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/jvm/clojure/lang/Compiler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,9 @@ else if(!(RT.second(form) instanceof Symbol))
407407
if(!v.ns.equals(currentNS()))
408408
{
409409
if(sym.ns == null)
410-
throw new Exception("Name conflict, can't def " + sym + " because namespace: " + currentNS().name +
411-
" refers to:" + v);
410+
v = currentNS().intern(sym);
411+
// throw new Exception("Name conflict, can't def " + sym + " because namespace: " + currentNS().name +
412+
// " refers to:" + v);
412413
else
413414
throw new Exception("Can't create defs outside of current ns");
414415
}

src/jvm/clojure/lang/Namespace.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.io.ObjectStreamException;
1616
import java.io.Serializable;
17+
import java.io.PrintWriter;
1718
import java.util.concurrent.ConcurrentHashMap;
1819
import java.util.concurrent.atomic.AtomicReference;
1920

@@ -66,7 +67,23 @@ public Var intern(Symbol sym){
6667
if(o instanceof Var && ((Var) o).ns == this)
6768
return (Var) o;
6869

69-
throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name);
70+
// throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name);
71+
72+
if(v == null)
73+
v = new Var(this, sym);
74+
75+
warnOnReplace(sym, o, v);
76+
77+
78+
while(!mappings.compareAndSet(map, map.assoc(sym, v)))
79+
map = getMappings();
80+
81+
return v;
82+
}
83+
84+
private void warnOnReplace(Symbol sym, Object o, Object v){
85+
((PrintWriter) RT.ERR.deref()).println("WARNING: " + sym + " already refers to: " + o + " in namespace: " + name
86+
+ ", being replaced by: " + v);
7087
}
7188

7289
Object reference(Symbol sym, Object val){
@@ -85,7 +102,14 @@ Object reference(Symbol sym, Object val){
85102
if(o == val)
86103
return o;
87104

88-
throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name);
105+
// throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name);
106+
warnOnReplace(sym, o, val);
107+
108+
while(!mappings.compareAndSet(map, map.assoc(sym, val)))
109+
map = getMappings();
110+
111+
return val;
112+
89113
}
90114

91115
public static boolean areDifferentInstancesOfSameClassName(Class cls1, Class cls2) {

0 commit comments

Comments
 (0)