Default Swift "print" function has this signature
func print(
_ items: Any...,
separator: String = " ",
terminator: String = "\n"
)
If I print the type of "print" function it outputs as:
print(type(of: print))
// (Any..., String, String) -> ()
For testing purposes, I want to pass this "print" function as an argument to a function so it will be called inside that function by using the parameter name rather than actually calling the "print" function itself.
func customPrint(printClosure: (Any..., String, String) -> (), text: String) {
printClosure(text)
}
customPrint(printClosure: print, text: "Hello World")
printClosure parameter type is set to the exact type signature of Swift's default print function
printClosure: (Any..., String, String) -> (),
Yet on line of:
printClosure(text)
Swift gives error of
error: missing arguments for parameters #2, #3 in call
parameters of #2 separator and #3 terminator of "print" function are default so they do not need to be passed but swift requires to be passed here.
Because closures do not have argument labels during function call, it is not even possible to provide default values in printClosure() call.
printClosure(text, separator:" ", terminator:"\n") // does not work
How can we fix this code issue?