Skip to content

Add function types, anonymous functions and closures #4

Closed

Description

@peq
No description provided.

Activity

  1. peq commented on Mar 22, 2013

    @peq
    CollaboratorAuthor

    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.

  2. peq commented on Oct 10, 2013

    @peq
    CollaboratorAuthor

    Type 1 closures are now implemented (see manual).

  3. added a commit that references this issue on Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions