[swift-evolution] Why doesn't Swift allow a variable and a function with the same name?

David Hart david at hartbit.com
Tue Jan 31 01:24:20 CST 2017


> On 31 Jan 2017, at 01:59, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Jan 30, 2017, at 11:42 AM, Austin Zheng via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> The reason Swift works like this is because you can assign a function value (independently of calling it) to a variable. So there aren't two separate namespaces separating function names and variable names.
> 
> To be honest, I would say that there's no "reason" for this, except as lingering effects of our early "functions have simple names, and arguments have labeled tuple type" model. If we had originally implemented the language with its current (at least aspirational) Smalltalk-ish compound-names model, we probably would have ended up allowing this, since the var and func do formally have different names. The ability to reference a function by only the first segment of its name is likewise legacy of the original model, though it happens to be useful since good naming hygiene encourages different base names for different things to begin with.

Is there a reason we couldn't push a proposal forward to have the compiler prefer function identifiers when the identifier is followed by a function call syntax? It would be a breaking change, but I'd wager it'd be quite rare: at least rarer than the amount of times this problem has bitten me since Swift 3's grand renaming, forcing me to `self.` prefix the function call (when I can and it's an instance function).

> -Joe
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list