[swift-evolution] Optional comparison operators

Jacob Bandes-Storch jtbandes at gmail.com
Tue Jul 12 12:17:36 CDT 2016


I've thought of this before, too, and I agree that it's light enough it
could probably replace implicit promotion. That said, I'm not convinced
we'd really want to remove implicit promotion everywhere.

I'd also be concerned that "postfix ?" is becoming too overloaded — it's
already used in optional chaining (where it sort of "removes" a layer of
optionality), and it's the "optional pattern" for case statements...this
idea would see it used with non-optional values being passed to optional
parameters, and there's another thread started recently which (although it
seems unlikely to happen in the near future) would see it used with
optional values being passed to non-optional parameters. "postfix ?" just
becomes the "solve all my problems" operator, and I'm pretty sure
readability would suffer for it.

Jacob

On Tue, Jul 12, 2016 at 8:33 AM, Nevin Brackett-Rozinsky via
swift-evolution <swift-evolution at swift.org> wrote:

> Regarding the spelling of optional promotion, I think “postfix ?” could be
> a solution. Notably, it already works in pattern matching (see the first
> two case conditions here):
>
> let anInt = 16
> let anOpt = Optional(anInt)
>
> switch anOpt {
> case 2? : print("Deuces")
> case anInt? : print("Huzzah")
> default : print("Eh")
> }
>
> I propose we make ”postfix ?” be a general-purpose optionalizing operator,
> which lets you write:
>
> let anOpt = anInt?      // Optional<Int>.some(anInt)
> let anotherOpt = 2?     // Optional<Int>.some(2)
>
> Notably “?” would be essentially the inverse of “!”, so given the above we
> would have:
>
> anInt == anOpt!
> 2 == anotherOpt!
>
> If we could write it ourselves the implementation would be simply,
>
> postfix operator ? {}
> postfix func ? <T> (value: T) -> T? { return Optional(value) }
>
> The “?” syntax is so light that we could seriously consider removing all
> implicit promotion to optionals, and just use “?”. Thus:
>
> func takesOptionalInt(_ x: Int?) { }
>
> takesOptionalInt(2?)
> takesOptionalInt(anInt?)
> takesOptionalInt(anOpt)
>
> In general, anytime you have a value that you want to promote to an
> optional, you would just put a “?” after it.
>
> The one place I see where ambiguity can arise is optional chaining. I
> think “anOpt?.someMethodOnInt()” should use optional chaining, whereas
> explicit promotion followed by member access would need parentheses:
> “(anOpt?).someMethodOnDoubleOptional()”.
>
> In particular, “anInt?.something()” should probably be a syntax error
> (attempting to using optional chaining on non-optional value), it should
> not silently become “(anInt?).something()” — that would have to use
> parentheses.
>
> Anyway, that’s my idea: “postfix ?” for general-purpose optional wrapping.
>
> Is this worthy of consideration?
> Should it be part of the current proposal or spun off separately?
> Would “postfix ?” sufficiently soften the landing to enable the
> elimination of implicit optional-promotion across the board?
> And is there a better way to handle its interaction with optional chaining?
>
> Thanks,
> Nevin
>
>
>
> On Tue, Jul 12, 2016 at 3:58 AM, Charlie Monroe via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>> An example to keep in mind:
>>
>> let dict: [String : String] = ...
>> if dict["key"] == "value" { // String? == String
>> // Do something
>> }
>>
>> If I understand correctly, when the proposal is accepted, you'd need to
>> do something like:
>>
>> if let value = dict["key"], value == "value" { }
>> -- OR --
>> if dict["key"] == Optional("value") { }
>>
>> It's not an end of the world, but makes life a bit more difficult and
>> such usecase should be kept in mind.
>>
>>
>> On Jul 12, 2016, at 9:09 AM, Mark Lacey via swift-evolution <
>> swift-evolution at swift.org> wrote:
>>
>>
>> On Jul 11, 2016, at 11:55 PM, Jacob Bandes-Storch <jtbandes at gmail.com>
>> wrote:
>>
>> Mark,
>> Thanks for writing this up. Just to clarify, will these still work if
>> your proposal is implemented?
>>
>>     let x: Int?
>>     let y: Int
>>     struct NotEquatable {}
>>     let z: NotEquatable?
>>
>>     x == y; x != y
>>     x == nil; x != nil
>>     z == nil; z != nil
>>
>> I would hope that these continue to work. If any changes need to be made
>> to ensure that, please make sure they're included in the proposal too.
>>
>>
>> The last four would work, but the first two (x == y and x != y) would not
>> because they still involve coercing y to an optional.
>>
>> Similarly, === and !== on reference types where one is an optional would
>> require coercing one side, and would not be accepted without an explicit
>> cast using Optional().
>>
>> I’m curious what the motivation is for further special casing these
>> operators. They do occur more in practice than <, <=, >, >= (in fact most
>> of the source updates I had to make were due to === and !==, with == and !=
>> a close second), but overall these are still quite uncommon from what I’ve
>> seen.
>>
>> If you’d like I can certainly update the “alternatives considered” to
>> include the suggestion that we add overloads for (T, T?) and (T?, T) for
>> those four operators.
>>
>> Mark
>>
>>
>> Jacob
>>
>> On Mon, Jul 11, 2016 at 9:35 PM, Mark Lacey <mark.lacey at apple.com> wrote:
>>
>>>
>>> On Jul 11, 2016, at 9:12 PM, Chris Lattner via swift-evolution <
>>> swift-evolution at swift.org> wrote:
>>>
>>>
>>> On Jul 11, 2016, at 8:14 PM, Jacob Bandes-Storch via swift-evolution <
>>> swift-evolution at swift.org> wrote:
>>>
>>> You'd have to unwrap it, or use the ??/==/!= operators:
>>> https://gist.github.com/jtbandes/9d88cc83ceceb6c62f38
>>>
>>> I'd be okay with </<=/>/>= returning Bool?, as I suggested in an older
>>> email (which somehow didn't make it to gmane's archive, but it's quoted in some
>>> other messages
>>> <http://thread.gmane.org/gmane.comp.lang.swift.evolution/10095>). I
>>> think it would be more convenient in some cases than unwrapping the
>>> individual values before comparing them.
>>>
>>>
>>> I’d be strongly opposed to those operator returning “Bool?”.  Doing so
>>> would prevent conforming to Comparable and would be extremely surprising.
>>>
>>> -Chris
>>>
>>>
>>> I just pushed the current draft of the proposal:
>>> https://github.com/rudkx/swift-evolution/blob/eliminate-value-to-optional-coercion/proposals/0000-disallow-value-to-optional-coercion-in-operator-arguments.md
>>>
>>> I haven’t addressed removal of the ordered comparison operators. I
>>> suspect this should be a separate proposal, but I can roll that into this
>>> one if it’s desired.
>>>
>>> I’ll update the proposal as the discussion continues until it’s selected
>>> for review.
>>>
>>> Mark
>>>
>>>
>>> _______________________________________________
>>> 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
>>
>>
>>
>> _______________________________________________
>> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160712/f8718dd6/attachment.html>


More information about the swift-evolution mailing list