Comments (2)
Some thoughts about closures:
We need several kinds of closures, because the general variant (variant 1) is
not performant enough for most use cases.
This post is not about syntax, just about the concepts...
1. Closures as Objects
Closures are like Objects and are translated into anonymous classes.
Closures must be destroyed manually.
For example consider a damage system where you can add an action to be executed
after a unit takes damage the next time.
public abstract class DamageCallBack
abstract function onDamage(unit target, real damage)
public function afterNextDamage(unit whichUnit, DamageCallBack d)
List<DamageCallBack> l = getListforUnit(whichUnit)
l.add(d)
function damageSystemOnDamage(target, damage)
let l = getListforUnit(target)
for cb in l
cb.onDamage(target, damage)
destroy cb
l.clear()
// Usage of the system without closures:
// we would need to define a class like this:
class MyDamageCallBack extends DamageCallBack
real healAmount
construct(real healAmount)
this.healAmount = healAmount
override function onDamage(unit target, real damage)
target.addLife(healAmount)
// and then use the system like this:
let healAmount = ...
afterNextDamage(target, new MyDamageCallBack(healAmount))
// Usage of the system with closures
let healAmount = ...
afterNextDamage(target, (target, damge) => target.addLife(healAmount))
// or alternative Syntax:
afterNextDamage(target)
function onDamage(unit target, real damage)
target.addLife(healAmount))
2. Closures which are always inlined
As seen in Kotlin: http://confluence.jetbrains.com/display/Kotlin/Functions#Functions-Inlinefunctions
class List<T>
...
/** remove all elements which fulfill a predicate*/
function removeAll(T => boolean filter)
let it = iterator()
while it.hasNext()
let t = it.next()
if filter(t)
it.remove()
// Usage:
List<unit> someUnits = ...
real minHp = 100
// remove all units with less than minHp:
someUnits.removeAll(u => u.getLife() < minHp)
The translation would yield something like this:
List<unit> someUnits = ...
real minHp = 100
// remove all units with less than minHp:
let it = iterator()
while it.hasNext()
let t = it.next()
if u.getLife() < minHp
it.remove()
This variant can only be used if the lifetime of the closure does not exceed the lifetime of
the function incarnation.
Variant 1 could be used for the same purpose with one more destroy statement at the end of the
removeAll function. However the performance would be worse as it has the overhead of creating and destroying objects
and dynamic dispatch.
3. closures for timers
One could also use variant 1 with timers but the overhead of dynamic dispatch is quite high for most
use cases involving timers.
// Assume we are in some spell
vec2 velocity = ...
vec2 pos = ...
real time = 0.
// Now we could use a timer-closure to move a missile
// 'periodic' is a builtin construct
periodic(TIME_STEP)
pos += velocity*TIME_STEP
// and here we could do collision detection etc.
if time > 10
stop // stop is a builtin construct to stop periodic closures
Similar to periodic closures there could be an only-once construct
unit target = ...
// damage the target after 1 second:
after(1.0)
target.damage()
The translation would be similar to variant 1 but we could avoid the dynamic dispatch because
we always know the exact type of the closure.
from wurstscript.
Type 1 closures are now implemented (see manual).
from wurstscript.
Related Issues (20)
- Can we have the safe call sugar to check null simply?
- how can choice typescriptTolua or this wurst? HOT 1
- [Lua] Reflection natives are not implemented in lua backend HOT 1
- [Lua] Modules are not initialized in derived classes when the module is used in the base class HOT 1
- [Lua] The reserved functions "objectToIndex" and "objectFromIndex" collide with user functions
- how to really create a string autohash?? HOT 7
- how to run debug mode, save some codes HOT 1
- wurst cannot good at low version war3. (about array limit)
- low version cj cannot change the limit size of the class HOT 2
- Why are there so many lines of code after compilation HOT 1
- Use w3x2lni to speed up compilation HOT 1
- support utf8 Character to Name the compile time variable
- how to use ExecuteFunc in wurst HOT 1
- [Lua] Parser Local Limit HOT 1
- why *.wurst filename must as same as package name?? HOT 1
- Connection to server got closed. Server will not be restarted. HOT 1
- Sometimes wurst built an unrunnable map. HOT 6
- Map crashes due to generated jass function with too many arguments HOT 7
- Internal error. HOT 1
- VersionExtractionException: Failed to extract executable version data HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wurstscript.