On Wed, Jun 24, 2020 at 2:33 PM i3dmaster <[email protected]> wrote:
>
> example:
>
> type Key interface {
> type comparable, fmt.Stringer
> }
>
> type Value interface {
> type interface{}, copyable
> }
>
> type Container(type K Key, V Value) interface {
> Add(K, V) bool
> }
>
> type HashMap(type K Key, V Value) struct{ m map[K]V }
>
> func (h *HashMap(K,V)) Add(k K, v V) bool {
> h.m[k] =v
> return true
> }
>
> But of course, copyable isn't defined... So where to find some of these
> predefined constraints? Or if not, how could one define a such constraint?
>
> Also how to specify constrains like
>
> 1. A type that I can call len() against it?
> 2. A type that I can range over it?
Your message came through as white text on a black background that is
hard to read. On this mailing list, please send plain text as plain
text. Thanks.
The current generics design draft doesn't support the kind of
constraints you are looking for.
I'm not even sure what you mean by "copyable". All Go types can be copied.
Typically instead of writing generic type constraints like supports
len or range people write code that takes a slice of some generic
type.
Note that you are using type lists in a way that doesn't look right.
To write a constraint that is a combination of other constraints, you
should use embedding, as in
type Key interface {
comparable
fmt.Stringer
}
Ian
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWcOsVoDLakA6PQO_Y8MXwz1dnniRP1s1mzOoNpyPDF6A%40mail.gmail.com.