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
Since Set and List's definitions of Add accept vararg params (values ...T), you can already call these with a slice, no?
set.Add(slice...)
However, it would still be nice to update stack.Push()'s signature to also be variadic:
// Before
Push(value T)
// After
Push(values ...T)
This should be backwards compatible, since the underlying data structures used in arraystack and linkedliststack already support this behavior
EDIT:
The main complication here is that arraystack.Push is append-based, while linkedliststack.Push is prepend-based, so even though both implementations currently satisfy stack.Push, in a variadic implementation, elements would be added in opposite order between the two:
arraystack.Push(1, 2, 3)
top := arraystack.Pop() // top == 3
linkedliststack.Push(1, 2, 3)
top = linkedliststack.Pop() // top == 1
Perhaps that's intended (as otherwise, writing around this case would cause linkedliststack.Push to not align with the behavior of linkedlist.Prepend), but it does make using the broader Stack interface less intuitive:
stack.Push(1, 2, 3)
top := stack.Pop() // top varies depending on which stack implementation is used
Add elements of a slice [] T to data structures.
set.Add(slice)
list.Add(slice)
stack.Push(slice)
etc
The text was updated successfully, but these errors were encountered: