[swift-evolution] Brainstorming: Optional sugar inferred map
Paul Ossenbruggen
possen at gmail.com
Mon Feb 15 22:54:41 CST 2016
Oops, I had an example and decided to delete it.
> On Feb 15, 2016, at 8:52 PM, Paul Ossenbruggen <possen at gmail.com> wrote:
>
>
>> On Feb 15, 2016, at 9:45 AM, Chris Lattner <clattner at apple.com <mailto:clattner at apple.com>> wrote:
>>
>> On Feb 15, 2016, at 12:49 AM, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>
>>> The ? operator is unwrapped and the appendContentsOf method is only executed if the value is non-nil. However you can not do the following, which seems natural:
>>>
>>> a? + b
>>
>> FWIW, "a?” is the degenerate form of the optional chaining operation, which tests A, conditionally extracts its value, then re-wraps it back up without performing any other operations on it. It is equivalent to “a?.identity()”. The compiler rejects it because it is confusing/surprising for people, not because it is undefined.
>
> Thanks for explaining that, that it is doing an identity and that is not undefined. I certainly missed that. .
>
>>
>> I would be very concerned with defining this operation to mean something else, because that introduces inconsistency and a completely different kind of surprise to the language.
>
>
> I am sorry, I am not getting why this is meaning something else, confusing or inconsistent, seems more consistent because you are returning an optional and that would not surprise me, it seems more surprising that you get an error. if it was already an optional and the expression as a whole results in a an optional, which is similar to what happens with optional chaining. With optional chaining, I expect it to return an optional as well, to me this seems very similar. If a is an optional, it would not be surprising for a? to return an optional. For example,
>
>>
>>>
>>> To address this, the nil-coalescing operator would allow $$, where $$ is the unwrapped unnamed result of the expression when non nil:
>>
>> Just to point one thing out, ?? is an operator in the stdlib. Doing something like this would require baking it into the compiler.
>>
>
> I guess if auto closure could take a parameter you could do something like this:
>
> @warn_unused_result
> public func ??<T>(optional: T?, @autoclosure defaultValue: (result : T) throws -> T) rethrows -> T {
> return optional != nil ? optional! : try defaultValue(result: optional!)
> }
>
> I am not an expert on this though. I would acknowledge that to the later examples with multiple statements may be harder to do.
>
>>
>> IMO, the problem you’re addressing is already adequately solved by guard.
>>
>> -Chris
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160215/7873b06d/attachment.html>
More information about the swift-evolution
mailing list