[swift-evolution] !? operator for ternary conditional unwrapping
Maxim Veksler
maxim at vekslers.org
Wed Feb 8 13:02:25 CST 2017
Hello Adrian,
Please see reply inline.
On Wed, Feb 8, 2017 at 4:52 PM Adrian Zubarev <
adrian.zubarev at devandartist.com> wrote:
> 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: null }"
>
> As far as I’m concerned, string interpolation in Swift does not allow
> nested string literals (yet).
>
Thanks, that's another take on the specifics of String, but I think *?!*
extends beyond String.
For example, assume that we're dealing with a callback of some form.
let callback: CallbackProtocol? = StructExampleCallback()
You could write inside you handler function:
func handler() -> Result? {
return callback ?! callback.result() : nil
}
Which would return either nil, or the callback result. I don't think you
can do it with ?? but then again guard is a valid solution for this
specific use case.
> ------------------------------
>
> But a ternary operator that could potentially unwrap a list of optional
> values would be neat.
>
> Bikeshedding:
>
> (optional1, optional2, optional1) ? { tupleWithUnwrappedOptionals in return R } : { R }
>
> // variadic generic parameter list
> ternary func ?: <…T, R>(list: (…T?), lhs: ((…T) -> R), rhs: () -> R) -> R {
>
> if let unwrappedList = // unwrap all optionals in `list` {
> return lhs(unwrappedList)
> } else {
> return rhs()
> }
> }
>
>
>
> --
> Adrian Zubarev
> Sent with Airmail
>
> Am 8. Februar 2017 um 15:04:44, Maxim Veksler via swift-evolution (
> swift-evolution at swift.org) schrieb:
>
> 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 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?
>
>
> -m
>
> _______________________________________________
> 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/20170208/b884d639/attachment.html>
More information about the swift-evolution
mailing list