FAQ
I have a question about implements a interface, as below example.



type Comparable interface{
     CompareTo(c Comparable) int
}

type Int int

func (i Int)CompareTo(c Int) int{
     return int(i-c)
}

func main(){
     var i Int
     i = 10
    var c Comparable
     c=i

     fmt.Println(c)
}
Compiler complaint "Int doesn't implement Comparable(Wrong type for
CompareTo method)

And I modified to bellow

type Comparable interface{
     CompareTo(c int) int
}

func (i Int)CompareTo(c int) int{
     return int(i)-c
}


it's work.

I appreciate to anyone who explain this.

Best Regards

--
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].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Chris dollin at May 20, 2013 at 1:42 pm

    On 20 May 2013 08:54, Xiao Liang wrote:

    I have a question about implements a interface, as below example

    type Comparable interface{
    CompareTo(c Comparable) int
    }

    type Int int

    func (i Int)CompareTo(c Int) int{
    return int(i-c)
    }

    func main(){
    var i Int
    i = 10
    var c Comparable
    c=i

    fmt.Println(c)
    }
    Compiler complaint "Int doesn't implement Comparable(Wrong type for
    CompareTo method)
    You provided an CompareTo(Int), which is not the same type as
    CompareTo(Comparable).

    And I modified to bellow
    ("below")

    type Comparable interface{
    CompareTo(c int) int
    }

    func (i Int)CompareTo(c int) int{
    return int(i)-c
    }
    Both the interface and the Int type has the same type for
    CompareTo, viz ()int.

    The aregument-and-result-types for the interface and
    the concrete method must be the same.

    Chris

    --
    Chris "allusive" Dollin

    --
    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].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jan Mercl at May 20, 2013 at 1:43 pm

    On Mon, May 20, 2013 at 9:54 AM, Xiao Liang wrote:
    type Comparable interface{
    CompareTo(c Comparable) int
    }
    The signature satisfying the interface is above. No other signature
    can satisfy this interface. But:

    'CompareTo(c Int) int' != 'CompareTo(c Comparable) int'

    It's simply a different signature -> it cannot satisfy your interface.

    Consider eg.:

    type Comparator interface {
             Compare(Comparator) int
    }

    type Int int

    func (i Int) Compare(c Comparator) int {
             ...
    }

    But I'm not sure you're on the right path with this as I don't know
    that the goal is. And frankly, I think you're doing something strange.

    -j

    --
    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].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Xiao Liang at May 22, 2013 at 1:54 am

    But I'm not sure you're on the right path with this as I don't know
    that the goal is. And frankly, I think you're doing something strange.

    it's concept of recursive.
    Int implements Comparable. The implementation should consider signature is
    same.
    this concept works on JAVA.

    Anyway, I do understand it.

    Thanks for all guys' reply.



    --
    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].
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 20, '13 at 1:32p
activeMay 22, '13 at 1:54a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase