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

David Waite david at alkaline-solutions.com
Fri Dec 4 09:56:51 CST 2015

Ruby recently ran into a similar problem from the other direction - they wanted to support ?. syntax, but ‘?’ was already legal in identifier names (and used) to represent result values which are meant to be considered ‘truthy’. In the end, they went with a different “&." syntax because
1. This meant ‘?’ was both a naming convention and an operation
2. Too many question marks made the code psychologically confusing
3. They already had to change the syntax (to ‘.?’) to make code unambiguous

An naming system or pattern indicating something optional isn’t a bad idea - but it probably shouldn’t use ‘?’ for the same reason. A ‘?’ is already used heavily to indicate actions against optionals, and every time you see that identifier you will have to think about whether you are just referencing it by name, or performing some operation on it.

I think you however are pushing for a mandatory naming system for optionals - to make it clear in your examples below that ‘x’ needs to be treated as an optional, not as the type contained within. This makes coming up with an acceptable system significantly harder.


> On Dec 4, 2015, at 8:44 AM, Amir Michail <a.michail at me.com> wrote:
> I don’t want to remember if a variable is optional. I want to see it in the variable name.
>> On Friday, December 4, 2015, Amir Michail <a.michail at me.com <mailto: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 <>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <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/af8f54ea/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4139 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/af8f54ea/attachment-0001.p7s>

More information about the swift-evolution mailing list