New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a optional value setter ??=
#63
Conversation
??=
I could imagine it's preferred to squash the commits into one. |
I think it's worth pointing out that there's a (sorta) precedent in other languages, e.g. |
@radex Definitely, I have mentioned that in my proposal. |
@dabrahams I updated, is this what you mean't ? |
@jcampbell05 yes, although you missed a couple of spots. Also I would use something other than |
@dabrahams do you mind providing a better example? Another local variable ? |
@jcampbell05 I think what Dave meant is that you now have brackets (subscript) on the left-hand side, and brackets (empty array) on the right hand side. How about an empty string? |
@radex Cool I have updated :) |
``` | ||
## Impact on existing code | ||
|
||
Since this is a new feature, it won't effect existing code and should make code more concise going forward. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[minor] this should read "won't affect existing code", not "effect"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated this.
(Somewhat) relevant stackoverflow discussion, on why GCC's ternary extension in C ( http://stackoverflow.com/questions/16925429/why-doesnt-gccs-ternary-extension-support-assignment |
I'd like to see more compelling, real-world examples (rather than the I used Can any of you provide real-world examples of where you'd want to use |
if really.long.lvalue[expression] == nil { So looking for “??” isn’t likely to turn up much in this case.
-Dave |
Fair point, though I don't really have any nil-test assignments either :) I seem to have preferred default values. Stephen |
Ah, scratch that, a few dictionary assignments came up. I'll try to see if there are any other common cases. Stephen |
When implementing a collection view or table view where I need to reuse a cell (I also have a custom one I have built), I have this: '''swift If view == nil //cell setup code return cell ''' With this operator I could do this: '''swift //cell setup code return cell ''' So I would imagine it would be used a lot for providing defaults in a dictionary when one isn't set or as a way of constructing an object (like above) when a cached instance isn't available. You could have it all on one line but I think this would be a great operator as some variable names can be long so you have to put it on multiple lines via an if to avoid making a long line but then it isn't as concise. |
@jcampbell05 UICollectionView's dequeue method doesn't return an optional cell. If you mean UITableView's Even if these dequeue methods returned optionals, I still prefer using let cell = tableView.dequeueReusableCellWithIdentifier(identifier) ?? MyCell() I am a little afraid that adding I'm open to more examples. A |
@stephencelis I have built a view that works similar to those views so the ??= would be handy. You're right with the Apple default classes I would avoid the nil dance. In any case I have noticed there is a proposal for a or pipe operator like so, which I think is a superior option:
The idea is it will set let with the first case that matches :) so this could be a much more powerful way of expressing ??= or a ?? b or (a) ? a : b or switch (x) .... So I would happily close this, in favour of that proposal. |
Please squash these commits. |
Update and rename 0015-OptionalValueSettier.md to 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Update 0015-OptionalValueSetter.md Improved spelling, grammar, and formatting.
@lattner Squashed :) |
Looks reasonable for review, to @DougGregor to find a review manager. |
Introduce a optional value setter `??=`
Introduce a new operator an "Optional Value Setter". If the optional is set via this operator then the new value is only set if there isn't an already existing value.
Swift-evolution thread: link to the discussion thread for that proposal