Skip to content
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

Proposal: Add replace(_:with:) function to the stdlib #93

Closed
wants to merge 1 commit into from

Conversation

lilyball
Copy link
Contributor

No description provided.

@adrfer
Copy link

adrfer commented Jan 10, 2016

@kballard, what about a version of replace that would return the new value instead of mutating it? Well, as of now, according to the API Design Guidelines, the suggested name would be replaced.

Do you think it's worth updating your proposal to include a non-mutating counterpart for replace?

Also, I came across this proposal... but not sure how current it ir though.

@lilyball
Copy link
Contributor Author

I have no idea what you mean by a version of replace that returns the new value instead of mutating it. The new value is the second parameter to the replace(_:with:) function. If you want to just return the new value instead of mutating it, then the function is literally doing nothing at all, e.g. replace(&a, with: b) would just become b without a function call. The whole point of the function is the mutation; without it, there's no function at all.

@adrfer
Copy link

adrfer commented Jan 11, 2016

@kballard I apologize for my confusion. Guess I read too many times your proposals Add scan, takeWhile, dropWhile, and iterate and Add function SequenceType.find(_:) and ended up treating the replace function in the same context, unrelated to properties.

@DougGregor
Copy link
Member

There hasn't been enough discussion on this to form adequate consensus to move forward with a proposal.

@DougGregor DougGregor closed this Feb 4, 2016
@Coeur
Copy link
Contributor

Coeur commented Feb 4, 2016

@kballard When reading a name like replace, I would expect it to return the new value, not the old one, which makes the feature counter-intuitive. I would advice to name the feature exchange or swap. This naming issue also affects the alternative operator that you proposed, <-, which doesn't express well what will be the returned value. Suggestions for an operator would be <-=> or <=->.

@Coeur
Copy link
Contributor

Coeur commented Feb 4, 2016

Oh, and what about handling two inout values at the same time?

@lilyball
Copy link
Contributor Author

lilyball commented Feb 4, 2016

@Coeur We already have a function called swap, and it takes two inout values (and returns nothing). It's rarely used, because most of the time where you'd want to use it, one of your values is actually a temporary instead of a mutable variable (hence the need for replace(_:with:)). Which is why in my Swift code I use replace(_:with:) pretty often, but I think I've only ever used swap(_:_:) once. Similarly in Rust code replace() is used pretty frequently and swap() almost never.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants