[swift-users] Can't declare variable with same name as func called to assign its initial value

David Sweeris davesweeris at mac.com
Thu May 12 14:14:27 CDT 2016


I can't recall whether functions and variables can have the same name (though I think not), but at the least that error message seems to miss the point.

Sent from my iPhone

> On May 12, 2016, at 14:07, Evan Maloney via swift-users <swift-users at swift.org> wrote:
> 
> Adopting the newer Swift style of preferring first parameter labels, I wrote a function:
> 
>    func sale(options options: DeepLinkOptions)
>        throws
>        -> Sale
>    {
>        // ...implementation...
>    }
> 
> I then tried calling it elsewhere, and setting the result to a variable named 'sale', which is coincidentally the same name as the function above:
> 
>    let sale = try sale(options: options)
> 
> This line won't compile, failing with the following error and a caret pointing at the second use of 'sale':
> 
>    variable used within its own initial value
> 
> In the past, I would've named the function above according to Objective-C conventions, and it might've ended up with a name like 'saleWithOptions'; there'd be no clash. However, with the more terse Swift 3.0 style, we'll probably end up with more situations like mine.
> 
> Is this (should this be?) considered a bug or compiler limitation? Should I file a JIRA? There doesn't seem to be anything for this on bugs.swift.org yet.
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users


More information about the swift-users mailing list