[swift-evolution] [Discussion] Type for matching a closure

Vladimir.S svabox at gmail.com
Tue May 31 06:45:23 CDT 2016


+1, I also think we need a special protocol for function/closure.

On 31.05.2016 13:12, Charlie Monroe via swift-evolution wrote:
> AFAIK there isn't currently a way of matching if an Any instance is a closure:
>
> funcsomething<T>(x: T) -> String{
>     /// is x a function? We want to support Any (Int, Int32, Float, Double,
>     ///  AnyObject, ...), but disallow functions.
>     ...
> }
>
> letmyClosure = { print("Hello") }
> something(myClosure) // Don't allow this.
>
>
> I propose adding a protocol Function - all closures would conform to it.
> See the code on
>
> https://gist.github.com/charlieMonroe/655f2b5e25cc0b4ba06c0ddafa41c73b
>
> which outlines the API and possible usage of it:
>
> - Allowing to match a function from an Any instance.
> - Inspect the function object - arguments, captured values, return type.
> This may help debugging retain cycles by printing the catputred variables -
> you will be able to see `self` within these.
> - Invocation - creating something as NSInvocation, since you'd be able to
> call the function with a list of arguments.
> - This could also become a basis for some RPC in Swift.
>
> I know this is partially something for the Reflection discussion going on
> here as well as something for the existentials, but since it kind of
> overlapses both discussions, I thought creating a new thread would perhaps
> be beneficial.
>
> Charlie
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>


More information about the swift-evolution mailing list