[swift-evolution] Propagating Optionals

T.J. Usiyan griotspeak at gmail.com
Fri Oct 7 17:40:29 CDT 2016


On Sun, Sep 25, 2016 at 4:19 PM, Trans via swift-evolution <
swift-evolution at swift.org> wrote:

> As I've been learning Swift recently, one aspect of the language
> jumped out at me with a "code smell". Namely, the way Optionals are
> handled. For starters, just consider how long this chapter is:
> https://developer.apple.com/library/content/documentation/
> Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html
> That's pretty beefy for something on the surface is pretty simple.
>
>
Handling 'nothing' is actually complicated. I am not sure that there is a
reasonable way to make it less complicated. There are quite a few tools to
deal with "this might not contain anything" and yes, that can be daunting,
but each tool pulls its weight.



> More concretely, consider the example given:
>
>     class Person {
>         var residence: Residence?
>     }
>
>     class Residence {
>         var numberOfRooms = 1
>     }
>
>     let john = Person()
>
>     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.
>

If you click the fixit, the compiler will insert a `!` for you. If you
typed that using autocomplete, it would have put a `?` in for you. I don't
see understand what else it can do for you. A choice has to be made about
how to handle "The left portion of this might be nil" and an implicit
choice is bad.



>
> Basically "john.residence.numberOfRooms" is a completely wasted
> expression -- it's meaningless. You have to put a `?` or `!` in there
> to get anything useful. I can't see any good reason for that.
> "john.residence.numberOfRooms" could just behave one way or the other,
> either as if the `?` were there, or the `!`. And of the two, the
> obvious choice is `?` because... I already told the program it the was
> optional in "var residence: Residence?".  I said it was optional, and
> yep I meant that. (Reminds me of the old retort "did I stutter?")
>

You meant 'this might be nil'. You did not say what to do when it is and
you usually cannot always say if finding nothing is recoverable even for
the same property.



> Thus, if I try to assign it to something else it too should be
> optional. If I want it to be otherwise I'd add the `!`.
>
This seems like a step backward. `?` calls out everyplace that there is
uncertainty about `nothing`. Going back to hidden nil messaging isn't a
good move. I know what it feels like to unexpectedly message nil. I can
debug it just fine. I know that the skill can be acquired and yet, I think
that swift is better for not requiring that skill of new programmers.


> Making this change would just simplify a whole mess of code and about
> half that chapter would all but vanish.
>

It would not simplify the code at all. It would complicate reading it.


>
> In addition, seeing that `!` acts a short-circuit to error, it would
> be nice to have something equivalent for fallback value. We can't use
> `??` b/c it doesn't chain, though maybe it could be made to? And I'd
> rather not reuse `?.` here  (for reasons I can explain later). Maybe
> `:` is a good choice? In any case, exact syntax aside,
>
>     let homelessShelter = Residence()
>     let roomCount = john.residence:homelessShelter.numberOfRooms
>
> Now, roomCount will not be optional, because there is a guaranteed value.
>
> I think simplifying Optionals this way would be a good fit for Swift,
> making this part of the language a whole lot cleaner and clearer.
>


I disagree, as the rest of my reply has likely indicated. Some things a
fine being implicit (the positive sign on integers, for instance).
Optionality is not one of those things.




> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161007/089c2c4e/attachment.html>


More information about the swift-evolution mailing list