Skip to content

Commit

Permalink
Fix implicit constructors not compiling properly
Browse files Browse the repository at this point in the history
  • Loading branch information
kindlich committed Sep 6, 2024
1 parent 5eb14f6 commit 5635594
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zencode.shared.CompileException;
import org.openzen.zenscript.codemodel.FunctionHeader;
import org.openzen.zenscript.codemodel.HighLevelDefinition;
import org.openzen.zenscript.codemodel.Modifiers;
import org.openzen.zenscript.codemodel.compilation.CompilingMember;
Expand Down Expand Up @@ -43,7 +44,11 @@ public void linkTypes() {
@Override
public void compile(List<CompileException> errors) {
compiled.annotations = ParsedAnnotation.compileForMember(annotations, compiled, compiler);
compiled.setBody(body.compile(compiler.forMethod(compiled.header.withReturnType(BasicTypeID.VOID))));

// Implicit .ctors return the created object, but "normal" ones don't, so for non-implict .ctors we change the header to return VOID
FunctionHeader compilingHeader = modifiers.isImplicit() ? compiled.header : compiled.header.withReturnType(BasicTypeID.VOID);
compiled.setBody(body.compile(compiler.forMethod(compilingHeader)));

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ function enquote(input: string): string => '"' + input + '"';

interface JsonData {

public implicit this(data: JsonData[]) => new JsonArray(data);
public implicit this(data: JsonData[]) {
// this one uses the method body syntax to verify that both syntaxes work
return new JsonArray(data);
}
public implicit this(data: JsonData[string]) => new JsonObject(data);
public implicit this(data: string) => new JsonString(data);
public implicit this(data: int) => new JsonNumber(data);
Expand Down

0 comments on commit 5635594

Please sign in to comment.