[swift-evolution] !? operator for ternary conditional unwrapping

Adrian Zubarev adrian.zubarev at devandartist.com
Wed Feb 8 09:23:27 CST 2017

I forgot about map :D That solution is more swifty than mine with String(format::).

Adrian Zubarev
Sent with Airmail

Am 8. Februar 2017 um 16:12:11, Tony Allevato via swift-evolution (swift-evolution at swift.org) schrieb:

What you're asking for is already possible (avoiding the optional unwrap) by combining map() on Optional with ??:

let name1: String? = "name"
print(name1.map { "\"\($0)\"" } ?? "null")  // prints "\"name\""

let name2: String? = nil
print(name2.map { "\"\($0)\"" } ?? "null")  // prints "null"

So I guess the question is, does simplifying that rise to the level of wanting a custom operator? I personally don't think it does, but I could see an argument being made that an operator with defined semantics might be a small amount clearer than map + ??. But I think the benefits would have to be very strong, though.

And as other folks have mentioned, having "!" in the operator name is somewhat misleading, since when I see that I expect a trap to occur in nil cases.

On Wed, Feb 8, 2017 at 6:04 AM Maxim Veksler via swift-evolution <swift-evolution at swift.org> wrote:

Let's assume I have an optional "name" parameter, based on the optional status of the parameters I would like to compose string with either the unwrapped value of name, or the string "null". The use case for is syntactic sugar to compose a GraphQL queries.

A (sampled) illustration of the code that currently solves it looks as following:

func query(name: String?) {
  let variables_name = name != nil ? "\"\(name!)\""
: "null"
return "{ param: \(variables_name) }"

Based on optional status the following output is expected

let name = "Max"
query(name: name) 
// { param: "Max" }

let name: String? = nil
query(name: name)
// { param: null }

I think it might be useful to have an conditional unwrap operator !? that would enable syntax sugar uch as the following built into the language:

func query(name: String?) {
  return "{ param: \(name !? "\"\(name)\"": "null")

This expression is expected to produce same
output as the examples above. It means check the Optional state
of name: String?, in case it has a value, unwrap it and make the
unwrapped value accessible under the same name to the true
condition part of the expression, otherwise return
the false condition part of the expression.

The effectively removes the need to have the "if !=
nil" and the forced unwrap syntactical code, and IMHO improves code
readability and expressiveness.

I'm wondering what the community thinks of the displayed use case, and proposed solution?

swift-evolution mailing list
swift-evolution at swift.org
swift-evolution mailing list
swift-evolution at swift.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170208/c6f9ce36/attachment.html>

More information about the swift-evolution mailing list