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),
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
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
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 :=
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 {
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
}
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
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
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 {