On Wednesday, June 24, 2020 at 3:02:43 PM UTC-7, Ian Lance Taylor wrote:
>
> On Wed, Jun 24, 2020 at 2:33 PM i3dmaster <[email protected] <javascript:>> 
> 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. 
>
> Oh god, I am sorry about that. I was using new browser + black background 
and didn't realize it even decorated the message. 

 

> The current generics design draft doesn't support the kind of 
> constraints you are looking for. 
>

Is there or will there be a plan? Using generic types with [], map, etc can 
work on some use cases,
but there are likely also use cases where custom container types would more 
appreciated to abtract
or confine certain API surfaces. I wish Go authors wouldn't completely 
eliminate these possiblities.
 

>
> I'm not even sure what you mean by "copyable".  All Go types can be 
> copied. 
>
Hm... I thought there are some aren't at least recommened to copy, e.g. 
Mutex, Cond, or any types that explicitly documented not to copy. 

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

Interesting, maybe I wasn't still quite not understanding the draft spec... 
What's the difference here?

// Numeric is a constraint that matches any numeric type. 
// It would likely be in a constraints package in the standard library. 
type Numeric interface { 
   type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, 
uint64, uintptr, float32, float64, complex64, complex128 
}

Also, btw, it compiles just fine.


> 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/a311297b-865a-417d-a383-3734ee07723co%40googlegroups.com.

Reply via email to