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

Per Melin p at greendale.se
Mon Dec 7 05:48:50 CST 2015


On Mon, Dec 7, 2015 at 9:19 AM, Kevin Ballard via swift-evolution <
swift-evolution at swift.org> wrote:

> Do you commonly write code where you don't understand the types you're
> working with?
>

Here is one type of mistake that I made long after that I should have known
better:

    var x: Int?

    // ...

    switch x {
    case let x where x < 0: // x is still Optional
    default: // ...
    }

Since you write 'if let' so often to unwrap, I kept forgetting that 'case
let' does not, and that there is a special syntax for that: 'case let x?'.



> Personally, I find the ability to compare optionals to be very valuable,
> almost as valuable as the ability to use == with optionals (which I doubt
> anyone will argue is a misfeature).
>

I have been trying to come up with situations where you would want to
compare two values, knowing that one or both may be nil, with <, <=, > or
>=. Other than as for sorting, I can't think of much. I doubt the Swift
team explicitly made this possible without good reason, so I must be
missing something.

Could you please share some examples? It doesn't necessarily have to be
code, just explain the context where this is valuable.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151207/9877dcac/attachment.html>


More information about the swift-evolution mailing list