type checks with (the [type] [value])

This commit is contained in:
2020-11-18 16:16:41 -07:00
parent fea2798513
commit 2308fd8635
3 changed files with 18 additions and 2 deletions

View File

@@ -48,7 +48,16 @@ class SpecialForms {
// TODO special form for switch // TODO special form for switch
// TODO special form for try // Type check syntax:
map["the"] = (args:Array<ReaderExp>, convert:ExprConversion) -> {
if (args.length != 2) {
throw '(the [type] [value]) expression has wrong number of arguments: ${args.length}';
}
ECheckType(convert(args[1]), switch (args[0]) {
case Symbol(type): Helpers.parseTypePath(type);
default: throw 'first argument to (the... ) should be a valid type';
}).withPos();
};
map["try"] = (args:Array<ReaderExp>, convert:ExprConversion) -> { map["try"] = (args:Array<ReaderExp>, convert:ExprConversion) -> {
if (args.length == 0) { if (args.length == 0) {

View File

@@ -137,4 +137,8 @@ class BasicTestCase extends Test {
Assert.equals(6, BasicTestCase.myTryCatch(404)); Assert.equals(6, BasicTestCase.myTryCatch(404));
Assert.equals(7, BasicTestCase.myTryCatch(["list error"])); Assert.equals(7, BasicTestCase.myTryCatch(["list error"]));
} }
function testTypeCheck() {
Assert.equals(5, BasicTestCase.myTypeCheck());
}
} }

View File

@@ -83,3 +83,6 @@
(catch [:String error] 5) (catch [:String error] 5)
(catch [:Int error] 6) (catch [:Int error] 6)
(catch [error] 7))) (catch [error] 7)))
(defun myTypeCheck []
(the Int 5))