Runtime: add method dispatch for array/string/dict/number (.len, .push, .pop, .keys, .values, .has, .toInt) Stdlib: delete global len/push/pop/keys/values/has Tests/docs/examples: migrate to method style; add tests/test_builtin_methods_style.bob All tests pass Breaking: global len/push/pop/keys/values/has removed; use methods instead Parser/AST: add class/extends/extension/super, field initializers Runtime: shared methods with this injection; classParents/classTemplates; super resolution; ownerClass/currentClass; extension lookup order Builtins: method dispatch for array/string/dict/number (.len/.push/.pop/.keys/.values/.has/.toInt); remove global forms Tests/docs/examples: add/refresh for classes, inheritance, super, polymorphism; migrate to method style; all tests pass VS Code extension: update grammar/readme/snippets for new features
36 lines
781 B
Plaintext
36 lines
781 B
Plaintext
print("\n--- Test: Extension methods on built-ins ---");
|
|
|
|
extension array {
|
|
func sum() {
|
|
var i = 0;
|
|
var s = 0;
|
|
while (i < this.len()) {
|
|
s = s + this[i];
|
|
i = i + 1;
|
|
}
|
|
return s;
|
|
}
|
|
}
|
|
|
|
extension dict {
|
|
func size() { return this.len(); }
|
|
}
|
|
|
|
extension string {
|
|
func shout() { return toString(this) + "!"; }
|
|
}
|
|
|
|
extension any {
|
|
func tag() { return "<" + type(this) + ">"; }
|
|
}
|
|
|
|
assert([1,2,3].sum() == 6, "array.sum should sum elements");
|
|
assert({"a":1,"b":2}.size() == 2, "dict.size should return count");
|
|
assert("hi".shout() == "hi!", "string.shout should append !");
|
|
assert(toInt(42).tag() == "<number>", "any.tag works on numbers");
|
|
assert("x".tag() == "<string>", "any.tag works on strings");
|
|
|
|
print("Extension methods on built-ins: PASS");
|
|
|
|
|