[swift-evolution] Why doesn't Swift allow a variable and a function with the same name?
lneiva at me.com
Tue Jan 31 02:40:54 CST 2017
> On 31 Jan 2017, at 01:59, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
> 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.
It seams like there is discussion to be had, comparing these models:
[A] functions have simple names, and arguments have labeled tuple type model
[B] model where we strictly require the labels for referring to n-ary functions (e.g. "insert(cell:, into:)" instead of "insert")
> Example that does not compile:
> let randomArray = randomArray(withCapacity: 4096)
I like [B] because it does solve cases of ambiguity, where only using the base-name of the func causes the "Variable used within its own initial value” described by Michael.
What are some advantages of [A]? I assume that tuple splatting (i.e. passing a tuple of args when calling a n-ary function) is one of them, or is that not related?
More information about the swift-evolution