Skip to content

Commit

Permalink
Misc edits for closures
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 2, 2024
1 parent 4b2f6df commit 778ffe5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
1 change: 0 additions & 1 deletion convex-core/src/main/java/convex/core/cvm/AFn.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public byte getTag() {

@Override
public boolean equals(ACell o) {
if (!(o instanceof AFn)) return false;
return Cells.equalsGeneric(this, o);
}

Expand Down
6 changes: 6 additions & 0 deletions convex-core/src/main/java/convex/core/lang/impl/AClosure.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ protected AClosure(AVector<ACell> lexicalEnv) {
this.lexicalEnv=lexicalEnv;
}

@Override
public int encode(byte[] bs, int pos) {
bs[pos++]=getTag();
return encodeRaw(bs,pos);
}

/**
* Produces an copy of this closure with the specified environment
*
Expand Down
8 changes: 3 additions & 5 deletions convex-core/src/main/java/convex/core/lang/impl/MultiFn.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,17 @@ public void validateCell() throws InvalidDataException {
if (num<=0) throw new InvalidDataException("MultiFn must contain at least one function",this);
fns.validateCell();
}

@Override
public int encode(byte[] bs, int pos) {
bs[pos++]=CVMTag.FN_MULTI;
return encodeRaw(bs,pos);
public byte getTag() {
return CVMTag.FN_MULTI;
}

@Override
public int encodeRaw(byte[] bs, int pos) {
pos = fns.encode(bs,pos);
return pos;
}


/**
* Decodes a MultiFn instance from a Blob encoding
Expand Down
3 changes: 3 additions & 0 deletions convex-core/src/test/java/convex/core/lang/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4544,6 +4544,9 @@ public void testDefnMulti() {
assertEquals(2L,evalL("(do (defn f ([] 4) ([a] 1 2)) (f 3))"));

assertArityError(step("(do (defn f ([] nil)) (f 3))"));

// Check multi-fns capture lexical env
assertEquals(Vectors.of(2,1,2),eval("(do (def a 1) (let [a 2 f (fn ([] a) ([b] (+ a b)))] [a *address*/a (f)]))"));
}

@Test
Expand Down

0 comments on commit 778ffe5

Please sign in to comment.