[swift-evolution] mandatory "?" suffix for identifiers associated with optional types

J. Cheyo Jimenez cheyo at masters3d.com
Fri Dec 4 09:41:32 CST 2015


This a little confusing, I don't think adding yet another question mark
helps. The ?? ( nil coalescing operator) already included in swift handles
checking for nil values. If anything, the ternary operator just makes
matters worse in this example.  If else would be better imo.

https://lists.swift.org/pipermail/swift-evolution/2015-December/000133.html


On Friday, December 4, 2015, Amir Michail <a.michail at me.com> wrote:

> So for example the following (probable) bug would result in a compile
> error:
>
> let f = x < 5 // where x is optional and could be nil
>
> If that was really what was intended, you would need to write:
>
> let f = x? < 5
>
> Similarly, the rule would also apply for functions that return an optional
> type:
>
> let f = x()? < 5
>
> A major advantage of this approach is it would encourage programmers to
> unwrap optionals early to avoid writing “?” and “?!" frequently in their
> code.
>
> Note that conditional chaining would just make use of the existing “?”
> suffix. There is no need to add another “?” after that.
>
> let f = x?.g?.h
> let f = x()?.g?.h
>
> As for implicitly unwrapped optionals, a “?” suffix would only be used
> when you want to treat a value as an optional (e.g., when comparing it to
> nil). For example, for x of type Int?, one could write:
>
> let y = (x? == nil) ? 0 : x
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <javascript:;>
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/ef0cd973/attachment.html>


More information about the swift-evolution mailing list