I see this code in telegram open source app , how to create function like this and where is this application codes? this class is final and don’t have delegate our protocol
var vote:(MessageId, Data?) -> Void = { _, _ in }
I see this code in telegram open source app , how to create function like this and where is this application codes? this class is final and don’t have delegate our protocol
var vote:(MessageId, Data?) -> Void = { _, _ in }
2
Answers
I don’t know where you saw this, but this is closure variable. You can call it on class/struct where this
vote
is declared and then code inside closure will be called with certain parameters.For example if you assign
vote
like thisthen if
vote
is called from inside of the class/struct…
"Voted"
gets printed.So, this is useful delegate pattern replacement which allows you to declare from one class/struct what will happen when this closure gets called from another class/struct without declaring any protocol, assigning
delegate
and having extra methods.It’s a closure variable that you can assign your own closure to and your code will be executed whenever
vote
is calledNote that I changed the parameter types in my example since I am not familiar with
Telegram
classes