[swift-evolution] Propagating Optionals
Jeremy Pereira
jeremy.j.pereira at googlemail.com
Mon Sep 26 07:35:48 CDT 2016
> On 26 Sep 2016, at 00:26, William Sumner via swift-evolution <swift-evolution at swift.org> wrote:
>
>>
>> let roomCount = john.residence.numberOfRooms
>>
>> // error: value of optional type 'Residence?' not unwrapped; did
>> you mean to use '!' or '?'?
>>
>> As general rule of thumb, whenever I get an error and the system tells
>> me what I probably meant, that is a pretty good sign the system isn't
>> doing all it can for me and making me jump through an unnecessary
>> hoop.
In this case, the compiler hasn’t told you what you probably mean. It’s given you two different meanings to choose from and hasn’t actually excluded other ways to fix the issue e.g. if let or guard let or even an extension to Optional.
As Haravikk and Brent have said, assuming either of these as the default is dangerous.
Assuming ! as the default, to my mind negates the point of introducing optionals at all, we might as well go back to null references. The whole point is to eliminate the “no value” problem at compile time, not run time.
Assuming ? as the default is more insidious. The application may carry on and may persist state that is incorrect.
I think the current behaviour is correct.
More information about the swift-evolution
mailing list