spool-lang / Spool-Legacy-Repo

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

Box #19

Open RedstoneParadox opened 5 years ago

RedstoneParadox commented 5 years ago

Overview

Box<T> is an interface (#?) that is part of the core standard library. The interface defines two members: boxedValue, which is a constant of generic type T that represents the value contained by the box, and unwrap(), a function that returns boxedValue. What makes a Box<T> special is that you can treat is as an instance of T without unwrapping it.


class Foo {

    func sayHi() {
        println("Hey!")
    }
}

class FooBox implements Box<Foo> {

    constructor(foo : Foo) {
        boxedValue = foo
    }
}

main {
    const foo = new Foo()
    const boxedFoo = new BoxedFoo(new Foo())

    foo.sayHi() // Works
    boxedFoo.sayHi() // Also works.
    println(boxedFoo is Foo) //Prints true.
}

This is because Silicon will implicitly call Box#unwrap() in those instances.

Additionally, the the boxing of any value is also done implicitly. For example, if you return T from a function with a return type of Box<T>, the T will be implicitly boxed.


func boxFoo(foo : Foo) -> FooBox {
    return foo
}

For the sake of limiting confusion, any implementations of Box<T> must not define any new members aside from constructors.

This implicit behavior is used to provide ergonomics when using Optional (#20 ) and Variant (#21).