[swift-users] Overload by return type optionality?

Joe Groff jgroff at apple.com
Thu Oct 13 17:36:23 CDT 2016


> On Oct 13, 2016, at 3:27 PM, Rick Mann <rmann at latencyzero.com> wrote:
> 
>> 
>> On Oct 13, 2016, at 14:47 , Joe Groff <jgroff at apple.com> wrote:
>> 
>> 
>>> On Oct 13, 2016, at 2:36 PM, Rick Mann via swift-users <swift-users at swift.org> wrote:
>>> 
>>> It seems I can write this:
>>> 
>>> extension String
>>> {
>>> public func deleting(prefix inPrefix: String) -> String
>>> public func deleting(prefix inPrefix: String) -> String?
>>> }
>>> 
>>> But I was hoping it would do the right thing:
>>> 
>>> let a = s.deleting(prefix: "foo")
>>> if let b = s.deleting(prefix: "foo") { }
>>> 
>>> But it finds these ambiguous, and I'm not sure how to specify which I want.
>> 
>> The first one is truly ambiguous since either overload works. If you specify the type of 'a', you should be able to choose one or the other:
>> 
>> let a: String = s.deleting(prefix: "foo")
>> let b: String? = s.deleting(prefix: "foo")
>> 
>> The `if let` should not be ambiguous, since only the Optional-returning overload is a valid candidate. Got time to file a bug?
> 
> Here you go: https://bugs.swift.org/browse/SR-2942

Thanks!

-Joe


More information about the swift-users mailing list