Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upClosures #313
Conversation
bvssvni
added some commits
Jun 23, 2016
bvssvni
merged commit e8e6dae
into
PistonDevelopers:master
Jun 23, 2016
bvssvni
deleted the
bvssvni:closures
branch
Jun 23, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
bvssvni commentedJun 23, 2016
•
edited
For design, see #314
This PR adds a simple form of closures to Dyon. It is closer to anonymous functions with current objects as dynamical scoped variables. There is no capturing of variables in the environment, instead current objects are used to simulate aspects of closures. Capturing of environment might be added at a later point, depending on how it fits the language.
nullHow to use closures
Assume we have a function
foothat applies a transformation for each item in a list, returning a new list:We can inline the function by creating a closure:
Instead of transforming the values in the list, you can compute a value from the index by setting
listas a current object (using~in front of it):If you forget
~ listafter the arguments you will get an error message:Objects, arrays and named syntax
When you call a closure, you can use any item, including objects:
... arrays:
... and named syntax (Dyon uses
__to separate function name and arguments, and_to separate arguments:There is a trick you can do with objects by combining a property with named syntax:
Dyon rewrites the AST such that named arguments are appended to the item:
You can also use the
?operator to propagate error before calling:A new class pattern?
Closures and current objects in Dyon gives a syntax that looks a bit like methods, but remember that is is no
selfkeyword. Instead, one might use this pattern:For example:
This fits a typical ECS pattern.
How current objects differ from captured variables and globals
A current object is not stored within a closure. You can create multiple closures using the same current object and mutate it when the closure is called:
This means that closures in Dyon at the moment does not have the nice mathematical properties of normal closures, but are actually anonymous functions with current objects as dynamic scoped variables. It might be added later, but right now the only way to change the environment is through current objects.
Current objects is similar to using first class functions with globals, but differs in the way that you can pass the closure to another function that sets up a different current object: