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

Interfaces support any types #243

Open
sleepymole opened this issue Mar 5, 2024 · 5 comments · May be fixed by #244
Open

Interfaces support any types #243

sleepymole opened this issue Mar 5, 2024 · 5 comments · May be fixed by #244

Comments

@sleepymole
Copy link

sleepymole commented Mar 5, 2024

Some interfaces defined in the package support only comparable types.

sets.Set

type Set[T comparable] interface {
	Add(elements ...T)
	Remove(elements ...T)
	Contains(elements ...T) bool

	containers.Container[T]
	// Empty() bool
	// Size() int
	// Clear()
	// Values() []interface{}
	// String() string
}

maps.Map

type Map[K comparable, V any] interface {
	Put(key K, value V)
	Get(key K) (value V, found bool)
	Remove(key K)
	Keys() []K

	containers.Container[V]
	// Empty() bool
	// Size() int
	// Clear()
	// Values() []interface{}
	// String() string
}

lists.List

type List[T comparable] interface {
	Get(index int) (T, bool)
	Remove(index int)
	Add(values ...T)
	Contains(values ...T) bool
	Sort(comparator utils.Comparator[T])
	Swap(index1, index2 int)
	Insert(index int, values ...T)
	Set(index int, value T)

	containers.Container[T]
	// Empty() bool
	// Size() int
	// Clear()
	// Values() []interface{}
	// String() string
}

However, some implementations of these interfaces can support any type with a custom comparator.
Is it possible to change the type in the interface from comparable to any? This would allow
implementations to support any type with a custom comparator without breaking backward compatibility.

@PapaCharlie
Copy link
Collaborator

Hey @sleepymole, what sort of type are you using right now that isn't comparable?

@sleepymole
Copy link
Author

For example, I want to use []byte as a type in Set or as a key in Map. However, []byte isn't comparable.

@PapaCharlie
Copy link
Collaborator

Hmmm this is interesting. This wouldn't work for map based structures (even the old code would have panicked there), but I suppose it can be relaxed for tree-based ones

@PapaCharlie
Copy link
Collaborator

Which one do you need, specifically?

@sleepymole
Copy link
Author

Specifically, I want to use treeset and treeset with type []byte.

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

Successfully merging a pull request may close this issue.

2 participants