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).
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 typeT
that represents the value contained by the box, andunwrap()
, a function that returnsboxedValue
. What makes aBox<T>
special is that you can treat is as an instance ofT
without unwrapping it.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 ofBox<T>
, theT
will be implicitly boxed.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 ) andVariant
(#21).