Giter Club home page Giter Club logo

Comments (2)

peq avatar peq commented on June 11, 2024

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.

peq avatar peq commented on June 11, 2024

Type 1 closures are now implemented (see manual).

from wurstscript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.