[swift-evolution] Mutability inference

Shawn Erickson shawnce at gmail.com
Wed Feb 24 15:55:03 CST 2016


I like compact code but not at the cost of code readability and clarity. I
think this degrades the later more then helps the former.

-Shawn

On Tue, Feb 23, 2016 at 11:13 PM Darko Damjanovic via swift-evolution <
swift-evolution at swift.org> wrote:

> In the current Swift version the compiler is warning me if a variable is
> never written to and therefore should be a "let" constant. So now the
> compiler knows best if "let" or "var" should be applied. During writing
> code I experience repetitive hints about using "let" or "var" so why do I
> have to declare it all the time by myself if the compiler anyway knows best?
>
> My proposal would be to make the declaration of "let" and "var" optional.
>
> Example:
> x = 0 // <- implicitly declared as "var x" because later was written to,
> no need to declare it manually
> x = 5
>
> Example:
> y = 0 // <- implicitly declared as "let y" because later was not written to
> print(y)
>
> Example Optional Binding:
> if index = myArray.indexOf("A") {
>         print(index) // here it's clear that index can be "let index"
> }
>
> etc...
>
> This would _not_ mean to disallow or remove the "var and "let" mutability
> declarations - just to make them optional. If I still want to write it to
> make it clear just by reading thru the code then this is ok. But I can omit
> "var and "let" if I want -  why bother about it at all if I can go sure
> that the compiler is already doing the best?
>
> Another option (if this is "too much" change) would be to just make "let"
> optional and "var" still should be explicitly written. So "let" would be
> the default and if I want mutability I have explicitly declare it as "var".
> This is already the rule for function parameters.
>
> Kind regards,
> Darko Damjanovic-Lichtfuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160224/c72b8dff/attachment.html>


More information about the swift-evolution mailing list