use of dyvilx.tools.compiler.ast.expression.constant.NullValue in project Dyvil by Dyvil.
the class CaseClassMetadata method createUnapplyAnyMethod.
private CodeMethod createUnapplyAnyMethod() {
// static final func unapply<TypeParams...>(value: any) -> (T...)?
final SourcePosition position = this.theClass.position();
final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
final IType type = NullableType.apply(this.getUnapplyReturnType());
final CodeMethod unapply = new CodeMethod(this.theClass, Names.unapply, type, attributes);
unapply.setPosition(position);
unapply.getTypeParameters().addAll(this.theClass.getTypeParameters());
final CodeParameter parameter = new CodeParameter(unapply, position, Names.value, Types.NULLABLE_ANY);
unapply.getParameters().add(parameter);
// = (param is This) ? unapply(param as This) : null
final InstanceOfOperator isOperator = new InstanceOfOperator(new FieldAccess(parameter), this.theClass.getClassType());
final CastOperator castOperator = new CastOperator(new FieldAccess(parameter), this.theClass.getThisType());
final IValue call = new MethodCall(position, null, Names.unapply, new ArgumentList(castOperator));
final IfStatement ifStatement = new IfStatement(isOperator, call, new NullValue());
unapply.setValue(ifStatement);
return unapply;
}
Aggregations