lit-lang/lit

Allow trailing function as last parameter

MatheusRich opened this issue · 2 comments

fn on_click { |btn, callback|
  # run the callback when the button is clicked
} 

on_click(my_btn) { puts("clicked!") }


fn map { |array, f|
  # for each element in array, apply f
}

map([1, 2, 3]) { |element| element * 2 }

Swift actually lets you pass multiple lambdas to methods.

func loadPicture(from server: Server, completion: (Picture) -> Void, onFailure: () -> Void) {
    if let picture = download("photo.jpg", from: server) {
        completion(picture)
    } else {
        onFailure()
    }
}

loadPicture(from: someServer) { picture in
    someView.currentPicture = picture
} onFailure: {
    print("Couldn't download the next picture.")
}

Elixir does something similar.

Lots of good ideas in Kotlin