[swift-evolution] Mutability inference

Radosław Pietruszewski radexpl at gmail.com
Wed Feb 24 03:04:35 CST 2016


>>> 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.
> 
> In this way most of your concerns would be relieved:
> 
> - if let would be the default (without explicitly typing it) then „x = y“ would lead to a compiler error (if x was declared already before)

This is a fair point, but considering the small cost of typing “let”, I think the benefits of disambiguation and more explicit information for the *reader* are well worth it.

> - mutability would be the default generally

I take it you meant “immutability”.

> 
> But don’t let me misunderstood - I am not pushing for it. It just feels strange to me that the compiler warns me all the time about something which he already knows (and could solve by himself).

I understand, but again, the point is not for the compiler to “solve” compilation, rather to aid you write correct code.

PS. I didn’t realize Swiftc is a “he”.

> Imagine you would still have to write retain/release all the time and the compiler would warn you all the time when and how to write it. (that’s what ARC solved eventually)
> 
> — Darko
> 
> 
> 
>> Am 24.02.2016 um 09:45 schrieb Radosław Pietruszewski <radexpl at gmail.com>:
>> 
>> This is by design:
>> 
>> 1. You do have to explicitly declare a variable with let or var to disambiguate, in the eyes of the reader, declaration and assignment. This way, when you see `let x = y`, you know for sure, without double checking, that “x” has no previous value. This is also makes the code more resilient, because if you accidentally reuse a name or whatever, the compiler will warn you.
>> 
>> 2. You have to explicitly opt into mutability not to aid the compiler, but to let the compiler aid you!
>> 
>> This teaches you to make immutable things by default, because immutability gives you a guarantee that a thing won’t change. You don’t have to think about it. You’re protected from your future self trying to change a value of a constant. And when you declare a `var`, but don’t need mutability capabilities, the compiler pushes you to change it to `let`, again, to protect yourself from future yourself’s mistakes.
>> 
>> Fun fact: Rust goes even further in pushing you to be immutable by default. Variables are declared by “let x = y”, and to opt into mutability you have to write “mut let x = y”.
>> 
>> — Radek
>> 
>>> On 24 Feb 2016, at 08:06, 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
>>> 
>>> 
>>> 
>>> 
>>> 
>>> _______________________________________________
>>> 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