[swift-evolution] Why doesn't Swift allow a variable and a function with the same name?
Jaden Geller
jaden.geller at gmail.com
Mon Jan 30 19:23:49 CST 2017
Not quite—you can already shadow function names:
```
func f() { print("hi") }
do {
let f = { print("bye") }
f()
}
```
You can refer to shadowed variables within their initial value—function or not—within a guard let or if let:
```
func f() -> (() -> ())? { return { print("hi")} }
do {
guard let f = f() else { fatalError() }
f()
}
```
BUT, you cannot refer to shadowed variables within their initial value otherwise:
```
func f() { print("hi") }
do {
let f = { f(); f() } // ERROR!!!
f()
}
```
This is inconsistent IMO.
> On Jan 30, 2017, at 5:17 PM, Derrick Ho <wh1pch81n at gmail.com> wrote:
>
> The answer is simple, it becomes ambiguous about whether it should get the var or the function pointer
>
> Suppose we could do this
>
> let r = 5
> func r() {}
>
> If we call r, should we get 5 or should we get ()->()
>
>
> On Mon, Jan 30, 2017 at 4:21 PM Sean Heber via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> I used to believe this was a problem, but once I internalized the idea that this ugliness was a signal to choose better variable and property names, it has ceased to be an issue for me and in fact, IMO, has become an asset of the language.
>
> l8r
> Sean
>
>
> > On Jan 30, 2017, at 3:17 PM, Jaden Geller via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> >
> > I personally find it kind of weird that `let x = 0; do { let x = x + 1 }` is disallowed but `let x: Int? = 0; if let x = x { }` is allowed. The former case requires you first rename the variable you plan to shadow, inconveniently:
> >
> > ```
> > let x = 0
> > do {
> > let tempX = x // ew
> > let x = tempX + 1
> > }
> > ```
> >
> >> On Jan 30, 2017, at 11:56 AM, Robert Widmann via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> >>
> >> This seems like it’s running through the same check that disallows defining and calling a closure
> >>
> >> let randomFunc : () -> () = randomFunc()
> >>
> >>> On Jan 30, 2017, at 2:37 PM, Michael Gubik via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> >>>
> >>> Example that does not compile:
> >>>
> >>> let randomArray = randomArray(withCapacity: 4096)
> >>>
> >>> Compiler error: “Variable used within its own initial value”
> >>> The variable name unfortunately clashes with the function name.
> >>>
> >>> This problem forces the developer to think about an alternative name.
> >>> IMHO that’s suboptimal since many times the most canonical naming would be one where these two go by the same name.
> >>>
> >>> It’s not a big problem in practice but I wonder if there are plans to change this?
> >>>
> >>>
> >>> Thanks,
> >>> Michael Gubik
> >>>
> >>> _______________________________________________
> >>> swift-evolution mailing list
> >>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> >>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> >>
> >> _______________________________________________
> >> swift-evolution mailing list
> >> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> >> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> >
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> > https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170130/66bde86c/attachment.html>
More information about the swift-evolution
mailing list