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

Added current objects #225

Merged
merged 5 commits into from May 24, 2016

Conversation

@bvssvni
Copy link
Member

commented May 24, 2016

Closes #224

This PR adds current objects to Dyon.

What are "current objects"?

"Current objects" is a similar concept to globals in other languages, but better and more powerful. They are inspired by the current library, but for names instead of types.

A current object is a way to pass information "outside" function arguments.

One common use of current objects is when some objects are initialized at the start, but needed deep down without the middle layers needing to know about them.

fn foo() {
    // `~` tells Dyon to use `settings` as a current object for this scope
    ~ settings := { msg: "hi!" }
    bar()
}

// `bar` does not know about `settings`
fn bar() {
    baz()
}

fn baz() ~ settings: {} {
    println(settings) // prints `hi!`
}

Things this is useful for:

  • window
  • main frame buffer
  • draw list
  • scene
  • player controllers
  • audio output
  • settings
  • optimization data structures for physics
  • systems in ECS architectures
  • standard output/input
  • environment variables

How to use Current Objects in Dyon

To set a current object, you put a ~ in front of a variable when declaring it:

fn main() {
    ~ settings := init_settings()
    ~ data := init_data()
    ...
    update(dt) // `update` can now use `settings` and `data`
    ...
}

Use a current object by putting a ~ and a list of variables after the function arguments:

fn update(dt: f64) ~ mut data: {}, settings: {} { ... }

Design

The implementation of current objects uses these rules:

  • A current object shadows the old with same name (like normal Dyon variables)
  • If you are not using a current object, you must get it through an argument
  • A current object outlives local variables
  • A run-time error is shown if a current object can not be found

Motivation

The benefit of current objects over globals are:

  • They can be nested, making it easy to compose applications
  • They run out of scope, not taking up memory when no longer needed
  • It is easy to look at a function whether it uses current objects or not

bvssvni added some commits May 24, 2016

Added current objects
- Added “syntax/current.dyon”
- Added `ast::Function::currents`
- Added `ast::Current`
Check lifetime on current objects
- Fixed syntax when `->` is after current objects
- Added “syntax/lifetime_13.dyon”
- Added “syntax/lifetime_14.dyon”
- Added `lt::Lifetime::Current`

@bvssvni bvssvni merged commit 83f5370 into PistonDevelopers:master May 24, 2016

@bvssvni bvssvni deleted the bvssvni:current branch May 24, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.