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

2017-02-08 Thread Adrian Zubarev via swift-evolution
For more information about optional chaining read this docs: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html --  Adrian Zubarev Sent with Airmail Am 8. Februar 2017 um 20:05:31, Adrian Zubarev (adrian.zuba...@devandar

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

2017-02-08 Thread Maxim Veksler via swift-evolution
Thanks for teaching me about .map on Optional. Very cool. I agree with the comment of $! misleading, maybe a better alternative would be "??" ? On Wed, Feb 8, 2017 at 5:12 PM Tony Allevato wrote: > What you're asking for is already possible (avoiding the optional unwrap) > by combining map() on

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

2017-02-08 Thread Adrian Zubarev via swift-evolution
Swift does already have a great solution in such a scenario. func handler() -> Result? { return callback?.result() } Does the trick. ;) --  Adrian Zubarev Sent with Airmail Am 8. Februar 2017 um 20:02:38, Maxim Veksler (ma...@vekslers.org) schrieb: For example, assume that we're dealing wit

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

2017-02-08 Thread Maxim Veksler via swift-evolution
Hello Adrian, Please see reply inline. On Wed, Feb 8, 2017 at 4:52 PM Adrian Zubarev < adrian.zuba...@devandartist.com> wrote: > You could always write something like this: > > func query(name: String?) -> String { > > let variables_name = String(format: name == nil ? "%@" : "\"%@\"", name

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

2017-02-08 Thread Anton Zhilin via swift-evolution
With Runes , this looks like: (name1 <^> { "\"\($0)\"" }) ?? "null" Runes defines <^> to have lower precedence than ??. Sadly, they all can’t be in the same precedence group due to right associativity of ??. 2017-02-08 18:11 GMT+03:00 Tony Allevato via swift-

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

2017-02-08 Thread Maxim Veksler via swift-evolution
Hi Martin, please see reply inline. On Wed, Feb 8, 2017 at 4:47 PM Martin R wrote: > Isn't that exactly what the nil-coalescing operator ?? does? > > func query(name: String?) -> String { > return "{ param: \(name ?? "null") }" > } > > No, because func query(name: String?) -> St

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

2017-02-08 Thread Adrian Zubarev via swift-evolution
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@swift.org) schrieb: What you're asking for is already possible (avoiding the optional un

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

2017-02-08 Thread Tony Allevato via swift-evolution
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"

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

2017-02-08 Thread Adrian Zubarev via swift-evolution
If you need a infix operator that traps, here you have one ;) infix operator ?! : NilCoalescingPrecedence func ?! (optional: T?, noreturn: @autoclosure () -> Never) -> T { switch optional { case .some(let value): return value case .none: noreturn() } } let x: Int? = nil

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

2017-02-08 Thread Haravikk via swift-evolution
I'm a bit undecided, as it seems like it doesn't add enough to warrant another operator to learn (and one that's a bit confusing in its purpose, since it doesn't trap like other exclamation mark operators do). For the specific example you can already just do: func query(name:String?) ->

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

2017-02-08 Thread Adrian Zubarev via swift-evolution
You could always write something like this: func query(name: String?) -> String { let variables_name = String(format: name == nil ? "%@" : "\"%@\"", name ?? "null") return "{ param: \(variables_name) }" } query(name: "Max") //=> "{ param: "Max" }" query(name: nil) //=> "{ param:

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

2017-02-08 Thread Martin R via swift-evolution
Isn't that exactly what the nil-coalescing operator ?? does? func query(name: String?) -> String { return "{ param: \(name ?? "null") }" } If you use the JSONSerialization class from the Foundation library then `nil` is bridged to `NSNull` and translated to "null" automatically (

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

2017-02-08 Thread Maxim Veksler via swift-evolution
Hello, 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 c