Re: [go-nuts] Generics Draft: Mixing types and methods.

2019-08-03 Thread Steven Blenkinsop
On Sat, Aug 3, 2019 at 2:11 AM, Ian Lance Taylor wrote: > You're right, I wrote it wrong. Actually I'm not sure how to write that > contract. Ideally we want to say that T is either comparable or has the > Equal(T) bool method, but there is no way to write that. We can embed > comparable(T),

Re: [go-nuts] Generics Draft: Mixing types and methods.

2019-08-03 Thread Ian Lance Taylor
On Fri, Aug 2, 2019 at 9:25 PM Steven Blenkinsop wrote: > > On Fri, Aug 2, 2019 at 6:55 PM 'Axel Wagner' via golang-nuts > wrote: >> >> >> contract Comparable(T) { >> T comparable, Equal(T) bool >> } > > > Wait, does this work? I mean, `comparable` is a contract, but it's being used > as a

Re: [go-nuts] Generics Draft: Mixing types and methods.

2019-08-02 Thread Steven Blenkinsop
On Fri, Aug 2, 2019 at 6:55 PM 'Axel Wagner' via golang-nuts < golang-nuts@googlegroups.com> wrote: > > contract Comparable(T) { > T comparable, Equal(T) bool > } > Wait, does this work? I mean, `comparable` is a contract, but it's being used as a constraint. Could you also write: contract

Re: [go-nuts] Generics Draft: Mixing types and methods.

2019-08-02 Thread Robert Engels
Clear as mud. > On Aug 2, 2019, at 6:03 PM, 'Axel Wagner' via golang-nuts > wrote: > > Just tried running it through the prototype type-checker and one correction: > You also have to convert `a` and `` to interface{}, so that you are allowed > to type-assert on them: > if e, ok :=

Re: [go-nuts] Generics Draft: Mixing types and methods.

2019-08-02 Thread 'Axel Wagner' via golang-nuts
Just tried running it through the prototype type-checker and one correction: You also have to convert `a` and `` to interface{}, so that you are allowed to type-assert on them: if e, ok := (interface{}(a)).(equaler(T)); ok { return e.Eq(b) } if e, ok := (interface{}()).(equaler(T)); ok {

Re: [go-nuts] Generics Draft: Mixing types and methods.

2019-08-02 Thread 'Axel Wagner' via golang-nuts
FWIW: interface{}(a) == interface{}(b) would work. It would panic if a and b have the same, non-comparable type. But if you know the type is equal and comparable, it's well-defined and does what you want. So you have to modify your code a bit: type equaler(type T) interface { Equal(T) bool }

Re: [go-nuts] Generics Draft: Mixing types and methods.

2019-08-02 Thread Bruno Albuquerque
Ok, it makes sense. I do think that supporting something like this might make the proposal even more appealing as it will bring custom types somewhat closer to builtin types by allowing generic functions/methods that can act on both at the same time. On Fri, Aug 2, 2019 at 1:28 PM Ian Lance

Re: [go-nuts] Generics Draft: Mixing types and methods.

2019-08-02 Thread Ian Lance Taylor
On Fri, Aug 2, 2019 at 1:12 PM Bruno Albuquerque wrote: > > I was thinking about a way to "extend" usual operations (say, equality > checks) to types that can not be compared the usual way (they are not > "comparable" in the contract sense) and came up with something like this: > > // Just to

[go-nuts] Generics Draft: Mixing types and methods.

2019-08-02 Thread Bruno Albuquerque
I was thinking about a way to "extend" usual operations (say, equality checks) to types that can not be compared the usual way (they are not "comparable" in the contract sense) and came up with something like this: // Just to use for type assertion later. type Equaler interface {