Skip to content

Commit 3746fcf

Browse files
committed
Minor code simplification / cleanup
1 parent 4b64cfb commit 3746fcf

File tree

3 files changed

+20
-49
lines changed

3 files changed

+20
-49
lines changed

CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CastedEval.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ public CastedExpression of(Expression value) {
6161

6262
Optional<Expression> implicitCast = resolvedValueType.tryCastImplicit(type, compiler, position, value, optional);
6363
if (implicitCast.isPresent())
64-
return new CastedExpression(CastedExpression.Level.IMPLICIT, implicitCast.get());
64+
return CastedExpression.implicit(implicitCast.get());
6565

66-
if (value.type.canCastImplicitTo(type))
67-
return new CastedExpression(CastedExpression.Level.IMPLICIT, value.type.castImplicitTo(position, value, type));
68-
if (type.canCastImplicitFrom(value.type))
69-
return CastedExpression.implicit(type.castImplicitFrom(position, value));
66+
Optional<Expression> castedImplicitlyTo = value.type.castImplicitTo(position, value, type);
67+
if (castedImplicitlyTo.isPresent())
68+
return CastedExpression.implicit(castedImplicitlyTo.get());
69+
70+
Optional<Expression> castedImplicitlyFrom = type.castImplicitFrom(position, value);
71+
if (castedImplicitlyFrom.isPresent())
72+
return CastedExpression.implicit(castedImplicitlyFrom.get());
7073

7174
if (value.type == BasicTypeID.NULL && type.isOptional())
7275
return CastedExpression.exact(new NullExpression(position, type));

CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -94,36 +94,12 @@ default InvalidTypeID asInvalid() {
9494
return new InvalidTypeID(CodePosition.UNKNOWN, CompileErrors.invalidType());
9595
}
9696

97-
default boolean canCastImplicitTo(TypeID other) {
98-
return false;
99-
}
100-
101-
default boolean canCastExplicitTo(TypeID other) {
102-
return false;
103-
}
104-
105-
default boolean canCastImplicitFrom(TypeID other) {
106-
return false;
107-
}
108-
109-
default boolean canCastExplicitFrom(TypeID other) {
110-
return false;
111-
}
112-
113-
default Expression castImplicitTo(CodePosition position, Expression value, TypeID toType) {
114-
return null;
115-
}
116-
117-
default Expression castExplicitTo(CodePosition position, Expression value, TypeID toOther) {
118-
return null;
119-
}
120-
121-
default Expression castImplicitFrom(CodePosition position, Expression value) {
122-
return null;
97+
default Optional<Expression> castImplicitTo(CodePosition position, Expression value, TypeID toType) {
98+
return Optional.empty();
12399
}
124100

125-
default Expression castExplicitFrom(CodePosition position, Expression value) {
126-
return null;
101+
default Optional<Expression> castImplicitFrom(CodePosition position, Expression value) {
102+
return Optional.empty();
127103
}
128104

129105
default Optional<OptionalTypeID> asOptional() {

JavaShared/src/main/java/org/openzen/zenscript/javashared/types/JavaFunctionalInterfaceTypeID.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.openzen.zenscript.javashared.expressions.JavaFunctionInterfaceCastExpression;
1111

1212
import java.lang.reflect.Method;
13+
import java.util.Optional;
1314

1415
public class JavaFunctionalInterfaceTypeID extends FunctionTypeID {
1516
public final Method functionalInterfaceMethod;
@@ -28,32 +29,23 @@ public TypeID instance(GenericMapper mapper) {
2829
}
2930

3031
@Override
31-
public boolean canCastImplicitTo(TypeID other) {
32-
return other instanceof FunctionTypeID && ((FunctionTypeID) other).header.isEquivalentTo(header);
33-
}
34-
35-
@Override
36-
public boolean canCastImplicitFrom(TypeID other) {
37-
return other instanceof FunctionTypeID && ((FunctionTypeID) other).header.isEquivalentTo(header);
38-
}
39-
40-
@Override
41-
public Expression castImplicitTo(CodePosition position, Expression value, TypeID other) {
32+
public Optional<Expression> castImplicitTo(CodePosition position, Expression value, TypeID other) {
4233
if (other instanceof FunctionTypeID) {
4334
FunctionTypeID otherType = (FunctionTypeID) other;
4435
if (header.isEquivalentTo(otherType.header))
45-
return new JavaFunctionInterfaceCastExpression(position, otherType, value);
36+
return Optional.of(new JavaFunctionInterfaceCastExpression(position, otherType, value));
4637
}
47-
return null;
38+
39+
return Optional.empty();
4840
}
4941

5042
@Override
51-
public Expression castImplicitFrom(CodePosition position, Expression value) {
43+
public Optional<Expression> castImplicitFrom(CodePosition position, Expression value) {
5244
if (value.type instanceof FunctionTypeID) {
5345
FunctionTypeID otherType = (FunctionTypeID) value.type;
5446
if (header.isEquivalentTo(otherType.header))
55-
return new JavaFunctionInterfaceCastExpression(position, this, value);
47+
return Optional.of(new JavaFunctionInterfaceCastExpression(position, this, value));
5648
}
57-
return null;
49+
return Optional.empty();
5850
}
5951
}

0 commit comments

Comments
 (0)