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.

Reply via email to