Replace `?.` and `!.` with `?` and `!`

Edit: The following suggestion will not work. I misunderstood how these operators are implemented (see below).

Currently, optional chaining looks like this:


It's subjective, but I personally think this would be better:


The ? and ! spellings already have a dot at the bottom, so it seems a bit redundant to have a dot afterwards too.

I understand that retaining the dot is technically consistent with operators like ?(, !(, ?[ and ![, but parens and brackets need to be explicitly opened and closed, so a ? or ! couldn't replace the opening paren or bracket like it could replace a dot.

IIUC, Swift's rules for whitespace around operators ensure that ? and ! would be completely unambiguous infix operators (both for humans and the parser).

I appreciate that it's too late to just change the spellings of operators, but we could support both spellings for the foreseeable, so the dot would just become optional for now.

Thanks for considering it.

1 Like

Counter example:


It would create an inconsistency with this.


These are two different operators. The ? and ! are used to unwrap an optional and the . is used to access a property on an object. There are also cases where you can use ! without necessarily using . afterwords, so this would overload the ! operator with two meanings.


@DevAndArtist - that's a good point. I hadn't thought of that. Still, if the dot remained optional (forever), we'd have the best of both worlds (at the expense of another slight increase in the complexity of the grammar).

@StefanCosminR - IIUC, the Swift compiler can distinguish prefix operators from infix operators, so there's no need for different spellings.

I was under the impression that ?. and !. are implemented as individual operators. The fact that you can do what @DevAndArtist demonstrated (in particular, the fact that the ? or ! can be separated from the . by whitespace) suggests that ? and ! are actually implemented as postfix operators (which makes sense).

So, the revised proposal would actually make the . infix operator optional immediately following the ? or ! postfix operators. That seems like a bridge too far.

Again, thanks for considering it. It would've been nice, but it's not going anywhere.


@StefanCosminR - I just revisited this thread, and realized that you were correct all along. I pushed back on your comment, because I misunderstood how these operators work. Once I realized (based on @DevAndArtist's example) how they actually work, I became able to appreciate that you'd explained this to me already, and I just didn't listen closely enough. Sorry. You did tell me.

1 Like

No problem! We're all here to contribute how we can or to learn something. It's always good to bring new ideas and question why things are the way they are