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

feature request: add .Clear method #55

Open
novadev94 opened this issue Jan 14, 2025 · 3 comments
Open

feature request: add .Clear method #55

novadev94 opened this issue Jan 14, 2025 · 3 comments

Comments

@novadev94
Copy link

Currently, it's impossible to clear the OrderedMap keeping its capacity.

A simple implementation would be:

func (l *list[K, V]) Clear() {
	l.root = Element[K, V]{} // just in case K & V are pointer types
}

func (m *OrderedMap[K, V]) Clear() {    
	clear(m.kv)
	m.ll.Clear()
}
@elliotchance
Copy link
Owner

Happy to accept a PR for this. I see clear() was introduced in Go 1.21, so for backwards compatibility it shouldn't use that function in this implementation.

@novadev94
Copy link
Author

I see orderedmap/v3 targets Go 1.23 with generics and iterators. Maybe this .Clear method will only be added to v3?

Doing a custom implementation for clear would be inefficient since mapclear is implemented in the runtime package.

@elliotchance
Copy link
Owner

Generics were released with Go 1.18. I'm saying you can implement a Clear method, but it cannot rely on new functionality in Go 1.23, at least until 1.23 becomes old enough that its sensible to expect clients to be running that.

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

No branches or pull requests

2 participants