[swift-users] Overload by return type optionality?

Greg Parker gparker at apple.com
Thu Oct 13 16:51:05 CDT 2016


> 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.

You can specify which you want by explicitly naming the type:
    let a = s.deleting(prefix: "foo") as String
or
    let a : String = s.deleting(prefix: "foo")

…but presumably that doesn't give you the usability that you were hoping for.


-- 
Greg Parker     gparker at apple.com <mailto:gparker at apple.com>     Runtime Wrangler


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161013/94167462/attachment.html>


More information about the swift-users mailing list