C# style events

I’m don’t think language support for this is necessary. Take a look at this extremely basic implementation: https://gist.github.com/Hikaru755/b430927d4233b0876618090f00c3110a

Using that you can write your example as follows:

class Foo {

    public swaged: Event<String>() // declaration

    fun qux() {
        swaged += { onSwag(it) } // subscription
        swaged("coorge") // invocation   
    }

    // event handler
    fun onSwag(x: String) { ... }
}

Except for the minimal overhead with the lambda braces this is not less concise, and is done using only existing kotlin features. And I’m not even sure the lambda notation there is even necessary, but I’m not quite up to speed about the current state of function references in kotlin.

4 Likes