Skip to content

Latest commit

 

History

History
44 lines (30 loc) · 870 Bytes

DynamicCallable.md

File metadata and controls

44 lines (30 loc) · 870 Bytes

DynamicCallable

Something you can do in Swift is make an entire data structure callable simply by instantiating it.

Say for example we have a struct with a method that looks like this

struct ChatNotifier {

    func notify() {
      // code
    }    
}

You can convert it into a dynamicCallable (meaning instantiating the struct execute it).

@dynamicCallable
struct ChatNotifier {

    func dynamicallyCall(withArguments: [Any]) {
      // code
    }    
}

So now instead of instantiating and callling explicitly

let notifier = ChatNotifier()
notifier.call()

You can simply instantiate the stuct and by appending () at the end it will automatically call the dynamicallyCallable method.

let notifier = ChatNotifier()()

Links that help