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

Amir Michail a.michail at me.com
Fri Dec 4 10:09:39 CST 2015


> On Dec 4, 2015, at 11:01 AM, Sean Heber <sean at fifthace.com> wrote:
> 
> Couldn’t this issue of remembering which variables are optional be solved in the IDE by using a different color for optionals or something like that?

It’s better in the language because it forces the programmer to think more about what they are doing and the compiler could generate more errors for possible bugs.

Also, the syntax highlighting you propose would not be supported in all contexts (e.g., email, mailing lists, github, etc.).

> 
> l8r
> Sean
> 
> 
>> On Dec 4, 2015, at 9:56 AM, David Waite <david at alkaline-solutions.com> wrote:
>> 
>> 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.
>> 
>> -DW
>> 
>>> 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> 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
>>> 
>> 
>> _______________________________________________
>> 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