Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global objects #683

Open
bvssvni opened this issue May 16, 2021 · 0 comments
Open

Global objects #683

bvssvni opened this issue May 16, 2021 · 0 comments

Comments

@bvssvni
Copy link
Member

bvssvni commented May 16, 2021

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[{}] { ... }
@bvssvni bvssvni changed the title Global current objects Global objects May 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant