You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This would be ideal in a situation where a slice is passed down until reaching an append.
Currently, the append will return a slice which needs to be propagated back up and set on the original variable.
Clearly this is not ideal when you could simply append by reference and not need to pass the slice back.
Suggested Implementation
This can be achieved by creating a wrapper for the current append function which will overwrite the pointer of the original object with that of the one returned from the current append and will garbage collect the original slice.
The text was updated successfully, but these errors were encountered:
bradfitz
changed the title
Append By Reference
proposal: language: add append variant that modifies its first argument instead of returning result
Feb 27, 2019
ianlancetaylor
changed the title
proposal: language: add append variant that modifies its first argument instead of returning result
proposal: Go 2: add append variant that modifies its first argument instead of returning result
Feb 27, 2019
This would change append from being a builtin but otherwise ordinary function into being an operation that implicitly takes the address of its first argument. Actually, it's even more complicated than that because presumably we would want
append(f(), 1)
to work. This would make append unlike anything else in the language, which is not a good thing.
@OneOfOne this could work as well. In my mind it would be more generic and part of the standard library rather than a method on a specific slice type.
Effectively, a generic method on any slice type that wraps the standard append function in a similar fashion to your example.
It might be complicated though as this method will need to be written on a slice of type empty interface to be compatible with any type slice and then have some sort of type assertion.
If we had this for slices of builtin types, this could be a good first step and then one can extend that method for custom types through an interface
Proposal
A wrapper on the append function that can append to a slice by reference
Currently
Append(ByReference)
Output For Both
Why?
This would be ideal in a situation where a slice is passed down until reaching an append.
Currently, the append will return a slice which needs to be propagated back up and set on the original variable.
Clearly this is not ideal when you could simply append by reference and not need to pass the slice back.
Suggested Implementation
This can be achieved by creating a wrapper for the current append function which will overwrite the pointer of the original object with that of the one returned from the current append and will garbage collect the original slice.
The text was updated successfully, but these errors were encountered: