How can I implement comparable interface in go?

Orest

I've recently started studying Go and faced next issue. I want to implement Comparable interface. I have next code:

type Comparable interface {
    compare(Comparable) int
}
type T struct {
    value int
}
func (item T) compare(other T) int {
    if item.value < other.value {
        return -1
    } else if item.value == other.value {
        return 0
    }
    return 1
}
func doComparison(c1, c2 Comparable) {
    fmt.Println(c1.compare(c2))
}
func main() {
    doComparison(T{1}, T{2})
}

So I'm getting error

cannot use T literal (type T) as type Comparable in argument to doComparison:
    T does not implement Comparable (wrong type for compare method)
        have compare(T) int
        want compare(Comparable) int

And I guess I understand the problem that T doesn't implement Comparable because compare method take as a parameter T but not Comparable.

Maybe I missed something or didn't understand but is it possible to do such thing?

Riscie

Your Interface demands a method

compare(Comparable) int

but you have implemented

func (item T) compare(other T) int { (other T instead of other Comparable)

you should do something like this:

func (item T) compare(other Comparable) int {
    otherT, ok := other.(T) //  getting  the instance of T via type assertion.
    if !ok{
        //handle error (other was not of type T)
    }
    if item.value < otherT.value {
        return -1
    } else if item.value == otherT.value {
        return 0
    }
    return 1
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to implement the Java comparable interface?

From Dev

Implement an interface that extends comparable

From Dev

How can I make an interface implement another interface

From Dev

How can I call multiple beans that all implement a specific interface?

From Dev

How can I implement an interface type using a explicit class

From Dev

How can I reference all components that implement a given interface?

From Dev

How can I implement a generic interface in Rhino JS?

From Dev

How can I make a class implement an interface .... (C++)

From Dev

How can I Implement an interface on a returned class and keep its data?

From Dev

How can I make a class implement an interface .... (C++)

From Dev

How can I implement in fragment with my interface in another fragment

From Dev

How to implement Comparable?

From Dev

How to implement Comparable in KotlinPoet?

From Dev

How can I hint that a type is comparable with typing

From Dev

How do I implement Swift's Comparable protocol?

From Dev

How do I implement a list of lists which extends Comparable?

From Dev

How can I build varidics of type interface in Go?

From Dev

how do i sort an array of objects via the comparable interface?

From Dev

Why implement Comparable interface when you can define compareTo method in a class?

From Dev

In Go, Given an interface{} in which I know what interface it implements, how can I convert to it

From Dev

How to implement equals and comparable in this TreeMap

From Dev

How does the Comparable interface work?

From Dev

How does the Comparable interface work?

From Dev

UML: how component can implement some interface?

From Dev

How can a value type implement an interface type?

From Dev

UML: how component can implement some interface?

From Dev

How do I implement an InfoWindowAdapter interface in Xamarin?

From Dev

how to implement tcpdump -i interface arp with libpcap

From Dev

how to implement tcpdump -i interface arp with libpcap

Related Related

  1. 1

    How to implement the Java comparable interface?

  2. 2

    Implement an interface that extends comparable

  3. 3

    How can I make an interface implement another interface

  4. 4

    How can I call multiple beans that all implement a specific interface?

  5. 5

    How can I implement an interface type using a explicit class

  6. 6

    How can I reference all components that implement a given interface?

  7. 7

    How can I implement a generic interface in Rhino JS?

  8. 8

    How can I make a class implement an interface .... (C++)

  9. 9

    How can I Implement an interface on a returned class and keep its data?

  10. 10

    How can I make a class implement an interface .... (C++)

  11. 11

    How can I implement in fragment with my interface in another fragment

  12. 12

    How to implement Comparable?

  13. 13

    How to implement Comparable in KotlinPoet?

  14. 14

    How can I hint that a type is comparable with typing

  15. 15

    How do I implement Swift's Comparable protocol?

  16. 16

    How do I implement a list of lists which extends Comparable?

  17. 17

    How can I build varidics of type interface in Go?

  18. 18

    how do i sort an array of objects via the comparable interface?

  19. 19

    Why implement Comparable interface when you can define compareTo method in a class?

  20. 20

    In Go, Given an interface{} in which I know what interface it implements, how can I convert to it

  21. 21

    How to implement equals and comparable in this TreeMap

  22. 22

    How does the Comparable interface work?

  23. 23

    How does the Comparable interface work?

  24. 24

    UML: how component can implement some interface?

  25. 25

    How can a value type implement an interface type?

  26. 26

    UML: how component can implement some interface?

  27. 27

    How do I implement an InfoWindowAdapter interface in Xamarin?

  28. 28

    how to implement tcpdump -i interface arp with libpcap

  29. 29

    how to implement tcpdump -i interface arp with libpcap

HotTag

Archive