[swift-evolution] Selector for current method

Rudolf Adamkovič salutis at me.com
Tue Nov 15 05:47:30 CST 2016


Hi Jordan,

> The stripped-down code seems like it could use any unique key, including #function.


That would work only if #function could be used with an argument just like #selector:

class DirectoryListingStub: DirectoryListing {

   var cannedOutput: [Selector: Any?] = [
       #function(contentsOfDirectory(at:includingPropertiesForKeys:options:)): nil
   ]

   func contentsOfDirectory(at url: URL, includingPropertiesForKeys keys: [URLResourceKey]?, options: FileManager.DirectoryEnumerationOptions) throws -> [URL] {
       return cannedOutput[#function] as! [URL]
   }

}

Obviously, this doesn’t work as #function takes no arguments.

There's no way to get #selector for the current method. And there’s no way to get #function for arbitrary method.

R+

> On 14 Nov 2016, at 20:07, Jordan Rose <jordan_rose at apple.com> wrote:
> 
> This doesn’t seem unreasonable, but I’m not sure if that makes it reasonable. :-) What’s your use case? The stripped-down code seems like it could use any unique key, including #function.
> 
> Jordan
> 
> 
>> On Nov 13, 2016, at 15:50, Rudolf Adamkovič via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Hi there!
>> 
>> in Swift 3, we now have #selector and #keyPath yet there’s still no _cmd like we have in Objective-C.
>> 
>> Example:
>> 
>> class DirectoryListingStub: DirectoryListing {
>> 
>>   var cannedOutput: [Selector: Any?] = [
>>       #selector(contentsOfDirectory(at:includingPropertiesForKeys:options:)): nil
>>   ]
>> 
>>   dynamic func contentsOfDirectory(at url: URL, includingPropertiesForKeys keys: [URLResourceKey]?, options: FileManager.DirectoryEnumerationOptions) throws -> [URL] {
>>       let selector = #selector(contentsOfDirectory(at:includingPropertiesForKeys:options:))
>>       return cannedOutput[selector] as!  [URL]
>>   }
>> 
>> }
>> 
>> Problem: I had to specify #selector twice.
>> 
>> I though I’d be able to use #function but:
>> 
>> #selector = contentsOfDirectoryAt:includingPropertiesForKeys:options:error:
>> #function = contentsOfDirectory(at:includingPropertiesForKeys:options:)
>> 
>> It’d be great if #selector (without arguments) returned the current selector.
>> 
>> Or am I missing something?
>> 
>> R+
>> _______________________________________________
>> 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