[swift-evolution] [Review] SE-0088: Modernize libdispatch for Swift 3 naming conventions

Hooman Mehr hooman at mac.com
Thu May 12 13:51:31 CDT 2016


Exactly: I also consider synchronous dispatch a special case not worthy of sacrificing a better name (`dispatch`) for it. I prefer to have a single `dispatch` as the main function name. Here is my proposed modified signature based on the proposal <https://github.com/apple/swift-evolution/blob/master/proposals/0088-libdispatch-for-swift3.md>:

class DispatchQueue : DispatchObject {
    func dispatch(synchronous: Bool, work: @convention(block) () -> Void)

    func dispatch(
        group: DispatchGroup? = nil, 
        qos: DispatchQoS = .unspecified, 
        flags: DispatchWorkItemFlags = [], 
        work: @convention(block) () -> Void)
}

queue. dispatch(group: group) {
    print("Hello World")
}

queue.dispatch(synchronous: true) {
    print("Hello World")
}

In the above synchronous variant, passing synchronous flag as false simply delegates to async function. This saves us from having to use a name like dispatchSync(). 

Actually, I currently have my own wrapper for libdispatch that works in this way. My wrapper also unifies more of the API under the same `dispatch` function with more optional parameters. For example, `apply` functions are also handled with `dispatch` function. It will iterate if `iterations:` optional parameter is specified and so on. I find it very convenient to use and the resulting code is very clear and readable.

I know, this goes further than importing the existing API, but concurrency API has such a profound impact on the overall style and readability of code that it is worth going even further than what proposal suggests to get a really clear and readable API.

Hooman 
> On May 12, 2016, at 11:16 AM, Thorsten Seitz via swift-evolution <swift-evolution at swift.org> wrote:
> 
> dispatch() and dispatchSync() would be my preference as asynchronous dispatch is the point of GCD and synchronous dispatch is a special case and therefore I think the visual asymmetry is an advantage.
> 
> -Thorsten
> 
> 
>> Am 12.05.2016 um 20:10 schrieb James Dempsey via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
>> 
>> 
>>> On May 11, 2016, at 8:02 PM, Ricardo Parada <rparada at mac.com <mailto:rparada at mac.com>> wrote:
>>> 
>>> 
>>> 
>>> For synchronously and asynchronously how about the adverbs before the verb:
>>> 
>>> syncDispatch()
>>> asyncDispatch()
>> 
>> 
>> I think with the abbreviation ‘sync’ it’s very easy to read ‘sync’ as a verb and dispatch as a noun.
>> 
>> i.e. I’m going to sync up with you about our plan
>> i.e. I received a dispatch from headquarters
>> 
>> I would be very fine with
>> 
>> dispatchAsync() and dispatchSync() as method names.
>> 
>> 
>> 
>>> 
>>> ?
>>> 
>>> On May 11, 2016, at 10:50 AM, James Dempsey <dempsey at mac.com <mailto:dempsey at mac.com>> wrote:
>>> 
>>>>> So maybe that will conform to the API naming guideline?  Or would the verb have to be in the base name of the func?
>>>> 
>>>> 
>>>> It seems from the guidelines that the intent is for the verb to be in the base name of the func, especially since there is another set of guidelines for naming function parameters.
>>>> 
>>>> In general the other methods in the proposal are verbs (perform(), notify(), wait(), cancel(), etc.)
>>>> 
>>>> At least for me, not including a verb makes the API read like the sentence “The dog quickly”.  This wasn’t so bad in the C API, because you could read the word ‘dispatch’ as the verb.
>>>> 
>>>> 
>>>> Looking at the current GDC API, it does seem like dispatching synchronously is the rare and special case.
>>>> 
>>>> Could there be just a single dispatch() method, with async as a flag with a default value of true?
>>>> 
>>>> It might be a little ugly because most of the other parameters of the proposed asynchronously() method would not apply in the sync case.
>>>> 
>>>> James
>>>> 
>>>> 
>>>> 
>>>>> On May 11, 2016, at 7:14 AM, Ricardo Parada <rparada at mac.com <mailto:rparada at mac.com>> wrote:
>>>>> 
>>>>> Jacob Bandes-Storch suggested:
>>>>> 
>>>>> synchronously(execute work: …)
>>>>> 
>>>>> So maybe that will conform to the API naming guideline?  Or would the verb have to be in the base name of the func?
>>>>> 
>>>>> Or perhaps:
>>>>> 
>>>>> synchronously(dispatch work: …)
>>>>> asynchronously(dispatch work: …)
>>>>> 
>>>>> 
>>>>> 
>>>>>> On May 11, 2016, at 9:32 AM, James Dempsey via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>>> 
>>>>>> The method names
>>>>>> 
>>>>>> 	synchronously()
>>>>>> 	asynchronously() 
>>>>>> 
>>>>>> are both adverbs, not noun phrases or verb phrases.
>>>>>> These methods have side effects, so each name should have a verb in it to make it a verb phrase.
>>>>>> 
>>>>>> 
>>>>>> Since these are the methods where you actually dispatch a block into a queue
>>>>>> 
>>>>>> dispatchSynchronously()
>>>>>> dispatchAsynchronously()
>>>>>> 
>>>>>> would include the verb in the name of the methods.
>>>>>> 
>>>>> 
>>>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160512/6e0e3ec9/attachment.html>


More information about the swift-evolution mailing list