Re: [go-nuts] stucked in the interface mud

2017-11-21 Thread 'Axel Wagner' via golang-nuts
This question is frequently asked
. 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
 solves a similar problem.

On Tue, Nov 21, 2017 at 2:39 PM,  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.


Re: [go-nuts] stucked in the interface mud

2017-11-21 Thread Jan Mercl
On Tue, Nov 21, 2017 at 2:40 PM  wrote:

https://play.golang.org/p/InPAO_haLU works, but I don't think it's what
you're after, because once more types implement Compare this approach gets
dirty with type too many type switches.


-- 

-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.


[go-nuts] stucked in the interface mud

2017-11-21 Thread christian . b . mueller
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.