spool-lang / Spool-Legacy-Repo

Legacy repo for the Spool programming language.
3 stars 0 forks source link

Components #14

Closed RedstoneParadox closed 5 years ago

RedstoneParadox commented 5 years ago

Overview

The component system is one of two options for composition over inheritance, the other being traits (#12). The component system is based on the delegation model and shared many similarities, but the primary one is that it allows for the composition of any given class instance to be changed at runtime.

Basics

In order for a class to have components, the class must first specify that it can have components and what kinds of components there are. To do this, a special component variable is defined in the class along with the kind annotation; functions are then annotated with this annotation to specify that function calls will automatically be delegated to the respective component.


class Baz {

    component foo;
    component bar;

    foo`func dooFooThing() {

    }

   bar`func doBarThing() {

   }

}

We then specify the components using the same annotations and then override the respective functions:


component Foo of Baz`foo {

    override func doFooThing() {
        print("A foo component is attached!")
    }

}

component Bar of Baz`bar {

    override func doBarThing() {
        print("A bar component is attached!")
    }

}
RedstoneParadox commented 5 years ago

This is most likely not going to make it into the language.