Description
A global object is declared at module level with some fields:
~ foo { a: f64 }
fn bar() foo {
println(foo.a)
}
fn baz() mut foo {
foo.a = 42
}
The global object is created automatically if it doesn't exist already.
A global object outlives any other lifetime.
The globals
function returns a list of all global objects:
fn main() {
println(globals())
}
A global can be destroyed, but it will recreated when calling some function accessing it or using it:
fn main() {
foo() // Creates global object `foo_object`
destroy("foo")
foo() // Recreates global object `foo_object`
}
~ foo_object {}
fn foo() foo_object {}
A global object can inherit from another global object:
~ rect { pos: [vec4], size: [vec4] }
~ button: rect { label: [str] }
When a global object only contains arrays as members, one can use the for-in syntax:
fn foo() mut button {
for b in button {
b.label = "click me!"
}
}
Methods can be declared on global objects that can be used inside for-in syntax:
~ rect {
pos: [vec4],
size: [vec4],
min() = pos
max() = pos + size
}
fn foo() rect {
for r in rect {
println(r.max())
}
}
Methods can also combine lists and scalar fields:
~ foo {
a: f64,
b: [f64],
bar() = a + b
}
To insert a new item in a global object, one uses push
syntax:
~ rect { pos: vec4, size: vec4 }
fn main() {
foo()
}
fn foo() mut rect {
push rect { pos: (0, 0), size: (0, 0) }
}
The super
function returns the inherited global object of a global object:
fn super(global: str) -> opt[str] { ... }
The methods
function returns a list of methods and their types:
fn methods(global: str) -> [{}] { ... }
The glob
function converts a global into an ordinary object (read-only):
fn glob(global: str) -> opt[{}] { ... }