8000 Lifetimes · Issue #173 · PistonDevelopers/dyon · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content < 10000 span style="width: 0%;" data-view-component="true" class="Progress-item progress-pjax-loader-bar left-0 top-0 color-bg-accent-emphasis">
Lifetimes #173
Closed
Closed
@bvssvni

Description

@bvssvni

Dyon uses a lifetime checker instead of a garbage collector. This adds a little more work to get the program running, but makes it easier to run the program. There are no pauses to clean up unused memory, just full steam ahead with predictable performance.

In Rust a lifetime is declared separately from arguments. Dyon uses argument names to describe lifetimes.

a: 'b means a outlives b

a: 'return means a outlives the return value of the function.

What are lifetimes?

For example, consider the following program:

fn foo(mut a, b) {
    a.x = b
}

fn main() {
    a := {x: [0]}
    b := [5]
    foo(mut a, b)
}

Dyon won’t let you run it, because storing b inside a requires it to outlive a:

Function `foo` requires `b: 'a`
2,11:     a.x = b
2,11:           ^

The b variable must be declared before a in stack memory. Or else when the stack rolls back, then a points to memory that is outside the valid memory, a dangling pointer.

One way to fix it is to write clone(b), but you can also do as Dyon says:

fn foo(a, b: 'a) {
    a.x = b
}

Now there is another error:

`b` does not live long enough
8,16:     foo(mut a, b)
8,16:                ^

This is because b is declared after a:

fn main() {
    a := {x: [0]}
    b := [5] // `b` does not outlive `a`
    foo(mut a, b)
}

By moving it before a, the program works:

fn main() {
    b := [5] // `b` outlives `a`
    a := {x: [0]}
    foo(mut a, b)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0