This would be similar to using x?.m() to skip a method call when the object x is nil.
As for syntax, perhaps you could try m(x, y?, z) to indicate that y may be nil even though the corresponding formal parameter is not an optional. The return type of m would be turned into an optional if necessary.
Why would you want to do such a thing? That seems like it would hide potential logic errors around nils that the programmer should probably otherwise handle.
I think this sort of thing has been discussed before, but in the context of an expression that would have otherwise created an optional to begin with. Such as this:
struct Thing {
func doSomething(_ x: Int) -> Int { x + 1 }
func doSomething2() -> Int? {
return nil
}
}
let a = Thing() as Thing?
let b = a?.doSomething(a?.doSomething2())
I don't feel this kind of pattern is common enough to require sugaring it. And even then, I would be highly skeptical about allowing sugar that looks like it allows passing an optional to something that doesn't allow optionals.
Right now you can do y.map { m(x, $0, z) } which does return an optional, but I have also often wished for a cleaner equivalent of ?.
I think the problem with m(x, y?, z) is that the "?" is buried in the middle so it's less clear what's going on. I haven't come up with a better alternative, though.
Similar to how zip(_:_:) takes a tuple of Sequences and returns a Sequence of tuples, a zip(_:_:_:) that takes a tuple of Optionals and returns an Optional tuple would help with this:
IMO, whenever the best solution for something is to use map/flatMap/reduce/filter, then Swift should be modified to allow an elegant solution that looks more like typical Swift code than something out of a functional language.