(extends) and (implements) for pure-kiss classes

This commit is contained in:
2022-10-06 01:21:10 +00:00
parent 52c1b570ab
commit 637ae85d46
3 changed files with 42 additions and 2 deletions

View File

@@ -580,6 +580,33 @@ class SpecialForms {
return none(wholeExp);
};
k.doc("extends", 1, 1, "(extends <Class>)");
map["extends"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
requireContext(wholeExp, "extends");
var type = context.getType();
type.kind = switch (type.kind) {
case TDClass(null, interfaces, false, false, false):
TDClass(Reader.toString(exps[0].def).asTypePath(), interfaces, false, false, false);
default:
throw KissError.fromExp(wholeExp, '${type.name} must be a class without a superclass');
}
return none(wholeExp);
};
k.doc("implements", 1, null, "(implements <Interfaces...>)");
map["implements"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
requireContext(wholeExp, "implements");
var type = context.getType();
var interfaces = [for (exp in exps) Reader.toString(exp.def).asTypePath()];
type.kind = switch (type.kind) {
case TDClass(superClass, [], false, false, false):
TDClass(superClass, interfaces, false, false, false);
default:
throw KissError.fromExp(wholeExp, '${type.name} must be a class without any interfaces');
}
return none(wholeExp);
};
return map;
}

View File

@@ -0,0 +1,5 @@
package test;
interface BasicInterface {
public var otherNum(default,null):Int;
}

View File

@@ -2,8 +2,16 @@
(importAs test.OtherPureKissClass Alias)
(using test.OtherPureKissClass)
(extends OtherPureKissClass)
(implements test.BasicInterface)
(defNew [num &prop :Int otherNum]
(super num))
(function test []
(assert (= (.extensionMethod "string") "EXTENDED"))
(let [instance (new OtherPureKissClass 5)
other (new Alias 5)]
(assert (= instance.num other.num 5))))
other (new Alias 5)
third (new PureKissClass 5 6)]
(assert (= instance.num other.num third.num 5))
(assert (= third.otherNum 6))))