With and Inherits

My writer’s block is over, in that I chose instead to implement the guts with some “reasonable” keywords and can continue the agonizing over the “perfect keywords” after its all done. I am pretty pleased with how everything turned out, even if I might change the keywords themselves ten more times over the life of Yap’s early development.

with

My goal for this keyword was to offer a way to declare a block of function declarations and variables, but without the baggage of having to do it “all at once”. In C/C++, there are architectural reasons for this missing functionality. In Javascript, the object notation allows for a “similar” feeling at the expense of having to chain all of the statements together with colons and commas. Something about it feels strange.

I decided that what I really wanted was a way to reference a variable as the focal point of an arbitrary block of declarations, and have the interpreter “know what to do” every time it saw a new variable declared at that scope level. This allows for the creation of a new/derived object to be decoupled from the overrides or extensions you plan to give it, without lots of boilerplate name prefixes.

Note: Yes, I’m aware this keyword does something else in Python. I currently don’t care, and by the time I do, I will have probably changed it in Yap anyhow.

inherits

I already hate this choice, as it is ambiguous whether it is setting the ancestor of an object or testing for a match. I actually named it “from” at first, but it looked a little bit silly when not chained together with other keywords (“car from vehicle;”). Either way, it sets up an object’s ancestor, and is pretty straightforward.

I haven’t bothered to implement the testing of the ancestor, but when I do, I will most likely rename this keyword to minimize the ambiguity. It certainly seems like a legitimate thing to want to type “if (car inherits vehicle)”, and that is a problem.

Here are some functional examples, and the output it produces:

// -------------------------------------------------------------------
// Keyword: inherits / usage of "static" vs "overridden" variables

var fruit = object();
fruit.type = "fruit"; // override me!
fruit.count = 0;      // static count of eaten fruit

fruit.eat = function(self)
{
    fruit.count = fruit.count + 1; // implement increment, kthx
    print("Eating %s (count: %d)\n" % (self.type, fruit.count));
};

var apple = object();
apple inherits fruit;
apple.type = "apple";
apple:eat();

var pear inherits fruit;
pear.type = "pear";
pear:eat();

// -------------------------------------------------------------------
// Keyword: with / multiple withs for a single var, and 'with' nesting

with var very = object()
{
    function awesome()
    {
        print("very awesome\n");
    }
}

with very
{
    with var very = object()
    {
        function awesome()
        {
            print("very VERY awesome\n");
        }
    }
}

very.awesome();
very.very.awesome();

// -------------------------------------------------------------------
// Variable creation in a 'with' statement, and mixing with/inherits

with var vehicle = object()
{
    var type = "generic";

    function printType(self)
    {
        print("vehicle type: %s\n" % (self.type));
    }
}

var car inherits vehicle;
car.type = "car";

with var boat inherits vehicle
{
    var type = "boat";
}

vehicle:printType();
car:printType();
boat:printType();

Output:

Eating apple (count: 1)
Eating pear (count: 2)
very awesome
very VERY awesome
vehicle type: generic
vehicle type: car
vehicle type: boat
Posted in Blog