gods icon indicating copy to clipboard operation
gods copied to clipboard

Interfaces support any types

Open sleepymole opened this issue 1 year ago • 6 comments

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.

sleepymole avatar Mar 05 '24 02:03 sleepymole

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

PapaCharlie avatar Apr 23 '24 23:04 PapaCharlie

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

sleepymole avatar Apr 24 '24 03:04 sleepymole

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 avatar Apr 26 '24 18:04 PapaCharlie

Which one do you need, specifically?

PapaCharlie avatar Apr 26 '24 18:04 PapaCharlie

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

sleepymole avatar Jun 03 '24 03:06 sleepymole