Skip to content

feature request: add .Clear method #55

Open

Description

@novadev94

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()
}

Activity

  1. elliotchance commented on Jan 14, 2025

    @elliotchance
    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.

  2. novadev94 commented on Jan 15, 2025

    @novadev94
    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.

  3. elliotchance commented on Jan 15, 2025

    @elliotchance
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions