This question is frequently asked <https://golang.org/doc/faq#t_and_equal_interface>. In short: Because the arguments to myType.Less have to be of type Compare, not myType. Go does not have polymorphic types.
To answer the "how should this be done in Go the right way": IMO the answer is "not". The only way to do it, is to use Compare in the individual methods and type-assert: func (t myType) Less(i, j Compare) bool { return i < j } But to me, wanting to do this seems to indicate the wrong abstraction. What is it, you are actually trying to solve? You can also have a look at how the sort package <https://godoc.org/sort#Interface> solves a similar problem. On Tue, Nov 21, 2017 at 2:39 PM, <christian.b.muel...@gmx.net> wrote: > hi there, > > and first of all sorry that i couldn't find the answer by myself here. I > tried to improve some of my code with general interface definitions. They > should work like macros for different types of variables. Here is my > reduced example and the question - how should this be done in go the right > way? > > thanx a lot, > c. > > Code hier eingeben... > package main > > import "fmt" > > func main() { > // test > fmt.Println(Eq(myType(5), myType(5))) > // gives me ... > /* > * ./main.go:7:23: cannot use myType(5) (type myType) as type > Compare in argument to Eq: > * myType does not implement Compare (wrong type for Less > method) > * have Less(myType, myType) bool > * want Less(Compare, Compare) bool > */ > } > > // define my custom type > type myType int > // and satisfy the interface > func (t myType) Less(i, j myType) bool { > return i < j > } > > // the interface condition > type Compare interface { > Less(i, j Compare) bool > } > // functions when satisfied > func Ls(i, j Compare) bool { > return i.Less(i, j) } > func Gt(i, j Compare) bool { > return i.Less(j, i) } > func GtEq(i, j Compare) bool { > return !i.Less(i, j) } > func LsEq(i, j Compare) bool { > return !i.Less(j, i) } > func Nt(i, j Compare) bool { > return i.Less(i, j) || j.Less(j, i) } > func Eq(i, j Compare) bool { > return !Nt(i, 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 golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.