Skip to content

Structures!

Compare
Choose a tag to compare
@brwhale brwhale released this 08 Jan 02:46
· 242 commits to main since this release
5f4db2a

As always, you can try KataScript in your browser: https://brwhale.github.io/KataScript/

KataScript now has structs!

struct person {
    var name;
    var age;
    var hobby;
    func person(n, a, h) {
        name = n;
        age = a;
        hobby = h;
    }
    func wouldLike(other) {
        return hobby == other.hobby;
    }
    func greet() {
        print("Hey I'm " + name + ", age " + age + ", and my hobby is " + hobby);
    }
}

Hooray!

The other big new feature is dot syntax for functions! What this means is that:

function(var1, var2, var3);

and

var1.function(var2, var3);

Are now equivalent. What this means is that you can call the standard library free functions as though they were member functions of a non-struct type. This allows you to create free functions with the same name as a struct's member function to create a sort of dynamic template.

struct A {
    var x;
    func A () {
        x = "A";
    }
    func getName() {
        return x;
    }
}

struct B {
    var x;
    func B () {
        x = "B";
    }
    func getName() {
        return x;
    }
}

func getName(x) {
    return string(x);
}

print(A().getName());
// prints: A
print(B().getName());
// prints: B
print("tacos".getName());
// prints: tacos

This release also includes minor features like the new toarray()/tolist() for casting to list/array rather than the weird overloading of the array()/list() functions, so now array(), and list() with just one argument will now make a list/array of length one and containing just that one argument.