Do you mean you want a default closure?

func forEach<U,T>(transform: (U)->T = { return $0 }, do: (T)->Void)

doSomething { 
    if let g = $0 as? Giraffe { g.eatTheTallLeaves() } 
}

doSomething(transform: { $0 as! Giraffe }) { 
    $0.eatTheTallLeaves() 
}



> On 6 Jan 2017, at 15:28, Jay Abbott via swift-evolution 
> <swift-evolution@swift.org> wrote:
> 
> Some good arguments regarding warnings and multiple closures. But I can't 
> help that feel when designing an API, if you have to add a convenience method 
> that takes just one closure simply to call the real method, this is exactly 
> the sort of boilerplate code that default arguments are supposed to replace.
> 
> On Thu, 5 Jan 2017 at 05:48 Douglas Gregor <dgre...@apple.com 
> <mailto:dgre...@apple.com>> wrote:
>> On Jan 4, 2017, at 9:44 PM, Saagar Jha <saa...@saagarjha.com 
>> <mailto:saa...@saagarjha.com>> wrote:
>> 
>> So, then this should have a warning? I’m still not getting one.
>> 
>> func foo(a: () -> (), b: (() -> ())? = nil, c: Int) {
>>      a()
>>      b?()
>> }
>> 
>> foo(a: {
>>      print(“Bar”)
>> }, c: 0)
>> 
> 
> If you give “c” a default value (e.g., “= 0”), it will give a warning.
> 
> I’m happy for the warning to get more eager, so long as it also gets a Fix-It 
> at the same time.
> 
>       - Doug
> 
>> Saagar Jha
>> 
>> 
>> 
>>> On Jan 4, 2017, at 9:34 PM, Douglas Gregor <dgre...@apple.com 
>>> <mailto:dgre...@apple.com>> wrote:
>>> 
>>> 
>>>> On Jan 4, 2017, at 9:32 PM, Saagar Jha <saa...@saagarjha.com 
>>>> <mailto:saa...@saagarjha.com>> wrote:
>>>> 
>>>> 
>>>> 
>>>> Saagar Jha
>>>> 
>>>> 
>>>> 
>>>>> On Jan 4, 2017, at 8:35 PM, Douglas Gregor <dgre...@apple.com 
>>>>> <mailto:dgre...@apple.com>> wrote:
>>>>> 
>>>>> 
>>>>> On Jan 4, 2017, at 7:48 PM, Saagar Jha via swift-evolution 
>>>>> <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>>>>> 
>>>>>> Check out this thread 
>>>>>> <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160606/020470.html>–it’s
>>>>>>  very similar to what you proposed, but it didn’t go anywhere. FWIW +1 
>>>>>> to this as well as the ability to use multiple trailing closures like so:
>>>>>> 
>>>>>> animate(identifier: “”, duration: 0, update: {
>>>>>>  // update
>>>>>> }, completion: {
>>>>>>  // completion
>>>>>> }
>>>>>> 
>>>>>> Saagar Jha
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>>> On Jan 4, 2017, at 6:25 PM, Jay Abbott via swift-evolution 
>>>>>>> <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>>>>>>> 
>>>>>>> When you have a function with a closure and then another optional 
>>>>>>> default = nil closure at the end, like this:
>>>>>>> 
>>>>>>> open static func animate(identifier: String,
>>>>>>>                          duration: Double,
>>>>>>>                          update: @escaping AnimationUpdate,
>>>>>>>                          completion: AnimationCompletion? = nil) {
>>>>>>> You can’t use trailing closure syntax for the update argument when 
>>>>>>> leaving the completion argument out/default.
>>>>>>> 
>>>>>>> This kind of breaks one of the benefits of default arguments, which is 
>>>>>>> that you can add them to existing released functions without breaking 
>>>>>>> the calling code. This means you have to add a separate convenience 
>>>>>>> function without the extra argument, which is annoying and inelegant.
>>>>>>> 
>>>>> Why not simply add the "completion" parameter before the trailing 
>>>>> closure? That would still allow existing callers to work, without having 
>>>>> to change the language. 
>>>>> 
>>>>>>> Another annoying thing is that you can easily miss this error if you 
>>>>>>> happen to not use trailing closure syntax in your tests or other usage, 
>>>>>>> because adding the extra default argument compiles fine for code that 
>>>>>>> uses normal syntax.
>>>>>>> 
>>>>> The Swift compiler warns when a parameter written as a closure type isn't 
>>>>> the last parameter. The warning is actually disabled in the specific case 
>>>>> above because you've written it using a typealias... maybe we should warn 
>>>>> on such cases (it's worth a bug report). Regardless, in the majority of 
>>>>> instances, you'll get a warning, so it won't be silent on disabling 
>>>>> trailing closure syntax. 
>>>> 
>>>> Tried this out in the playground:
>>>> 
>>>> func foo(a: () -> (), b: (() -> ())? = nil) {
>>>>    a()
>>>>    b?()
>>>> }
>>>> 
>>>> foo(a: {
>>>>    print(“Bar”)
>>>> })
>>>> 
>>>> and didn’t receive a warning for it, either.
>>> 
>>> We don’t warn here because ‘foo’ does have a trailing closure… it’s for the 
>>> second parameter. I guess we could still warn about ‘a’ (maybe lump it into 
>>> the same bug about the typealias case).
>>> 
>>>     - Doug
>>> 
>> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to