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

Closure design #314

Closed
bvssvni opened this issue Jun 23, 2016 · 0 comments
Closed

Closure design #314

bvssvni opened this issue Jun 23, 2016 · 0 comments
Assignees

Comments

@bvssvni
Copy link
Member

bvssvni commented Jun 23, 2016

Dyon supports a closures:

a := \(x, y) = x < y
println(\a(2, 3)) // prints `true`

For capturing of variables from the closure environment, see grab expressions.

Use of current objects inside closures

~ msg := "hi!"
a := \(x, y) ~ mut msg = {
    msg = "bye!"
    x < y
}
println(\a(2, 3)) // prints `true`
println(msg) // prints `bye!`

Return a closure from a function

fn foo() -> {
    return \(x, y) = x < y
}

Call other functions from within a closure

fn main() {
    a := foo()
    println(\a(0))
}

fn foo() -> {
    return \(x) = bar(x)
}

bar(x) = x + 2

Store a closure inside an object

a := {x: \(x) = x + 2}
println(\a.x(0))

Use double underscore __ to use named argument syntax:

a := {foo__x: \(x) = x + 2}
println(\a.foo(x: 0))

Rules

The following rules are how closures work currently in Dyon:

  • All closures must return a value (enforced by = declaration)
  • All closure arguments must be immutable
  • No closure arguments can have lifetimes
  • Current objects are not captured, they have same behavior as in normal functions

These rules are designed for:

  • Easy to change a closure into a function and vice versa
  • All closures returns a value because the type is not guaranteed
  • Support a kind of first class function that works for intrinsics, external functions and loaded functions
@bvssvni bvssvni self-assigned this Jun 23, 2016
@bvssvni bvssvni mentioned this issue Jun 23, 2016
@bvssvni bvssvni removed the draft label Jul 11, 2016
@bvssvni bvssvni closed this as completed Jul 11, 2016
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