spool-lang / Spool-Legacy-Repo

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

Memory Management #11

Closed RedstoneParadox closed 5 years ago

RedstoneParadox commented 5 years ago

Overview

In Silicon, the VM manages memory by reference counting as opposed to a standard garbage collector. Anything that goes out of scope will lose a reference count and will be deleted at 0 references.

In-Language Control

Because dropping everything that goes out of scope as soon as they do so can cause massive freezing if too much goes out of scope at once, Silicon also offers use of the deref keyword, which immediately removes it from the current scope. For example, the following function (#5) will cause an error as the variable foo is dropped:


func BadFunction() {

    var Foo : foo = new Foo()

    deref foo

    #This if statment will cause a crash as foo is no longer valid.
    if (foo == new Foo()) {
        print("This will never get printed!")
    }
}

Holding a Reference

Sometimes, it may be benificial to keep an object in memory even when nothing holds a reference to it. This is what the hold keyword is for:


class Foo() {

    contructor() {
        hold
    }

}

In this case, the engine will hold a reference to any instance of Foo that gets created.