I want to print the type of a function.
func thanksTo(name: String) {
print("Thanks, \(name)")
}
printType(thanksTo) // expected to print "Function (String) -> ()"
Is there any function in Swift that behaves like printType
?
As of Swift 3, __FUNCTION__
is deprecated. Instead, use #function
in place of __FUNCTION__
.
(Thank you, @jovit.royeca.)
You have a few options:
print(__FUNCTION__)
will output functionName()
if the function has no arguments.print(__FUNCTION__)
will output functionName
(without parentheses) if the function has one or more arguments.print(functionName.dynamicType)
will output (() -> Swift.Int) -> Swift.Int
for this hypothetical function:
func functionName(closure: () -> Int) -> Int {
}
Thus, to implement the desired functionality for your printType
function, you could use a combination of Option 2 and Option 3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With