type Node interface {GetNumberConnections() intGetConnection(int) (bool, Node)Distance(Node) vectors.Ftype // get the distance between 2 nodesTotalDistance() vectors.Ftype // the total distance from the zeroth nodeSetTotalDistance(vectors.Ftype)GetParentNode() NodeSetParentNode(Node)}
func (mn mapnode) Distance(mn2 mapnode) vectors.Ftype {h1 := mn.parentarray.underlyingmap.array[mn.x][mn.y]h2 := mn2.parentarray.underlyingmap.array[mn2.x][mn2.y]distance := (h2 - h1)return distance * distance}
(has this come up before?)I was writing an interface for shortest path algorithms and came across an issue -type Node interface {GetNumberConnections() intGetConnection(int) (bool, Node)Distance(Node) vectors.Ftype // get the distance between 2 nodesTotalDistance() vectors.Ftype // the total distance from the zeroth nodeSetTotalDistance(vectors.Ftype)GetParentNode() NodeSetParentNode(Node)}when I try to implement the interface with a "mapnode" struct type - the function :func (mn mapnode) Distance(mn2 mapnode) vectors.Ftype {h1 := mn.parentarray.underlyingmap.array[mn.x][mn.y]h2 := mn2.parentarray.underlyingmap.array[mn2.x][mn2.y]distance := (h2 - h1)return distance * distance}I've abstracted the problem here : https://play.golang.org/p/j8SFhePBPYThe compiler doesn't like this, though if it could be persuaded to allow mapnode as a valid Node interface type there would be no issues. (chicken and egg).
I have a workaround which involves a minor rewrite of my interface avoiding the self reference.. but in general is there a fix for such self references ? Is this something the language designers feel should work..? or plan to fix or ignore
What you need to do in this instance is change the signature of Distance tofunc (mn mapnode) Distance(mn2 Node) vectors.Ftype {You want to ensure that Distance can be called between ANY two Nodes, no matter what their underlying type is, no just between two Nodes that happen to have the same underlying type.
func (mn mapnode) Distance(mn2_ Node) vectors.Ftype {
mn2 := mn2_.(mapnode)
h1 := mn.parentarray.underlyingmap.array[mn.x][mn.y] h2 := mn2.parentarray.underlyingmap.array[mn2.x][mn2.y] distance := (h2 - h1) return distance * distance}The problem is that the interface requires to implement a Distance(Node), but you are providing a Distance(mapnode). The method signatures are different, it doesn't matter if map node is in the end a Node or not, you are not implementing the interface correctly. You could try with:Let me know if this works.func (mn mapnode) Distance(mn2_ Node) vectors.Ftype {
mn2 := mn2_.(mapnode)
h1 := mn.parentarray.underlyingmap.array[mn.x][mn.y]h2 := mn2.parentarray.underlyingmap.array[mn2.x][mn2.y]distance := (h2 - h1)return distance * distance}
It's not gonna change. You could check the sort package to see how it deals with such problem.(Instead of having an Interface for a single node, it define an interface for a group of node)
Distance(Node) vectors.Ftype
with a
DistanceToConnection(int) vectors.Ftype
(has this come up before?)
(has this come up before?)Yes and many times , search for "covariance" in this group. Go doesn't support covariance nor hierarchies between types.
Le samedi 15 août 2015 21:04:34 UTC+2, xiio...@gmail.com a écrit :
If you need to do this on many types, and need it to be fast, you have two options:
In Go, you are correct. D, Nim, Rust, and C++ compilers generate specialized code for each type.
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.