My current problem is that I am implementing data-structures and I have written iterators for them. I have visible interfaces for my data-structures, iterators, and all other required objects. In the back I have concrete implementations which I wish to hide from the end user.
This requires many of my functions to return interface{} objects so that I can store any type of object (and leave validation up to the end-user).
One problem I have is that I iterate over a Graph. My iterator{} for my graph implementation returns a concrete vertex type but the Iterator interface returns interface{}. Since the end-users can only my base Vertex interface I have to try to convert to a Vertex interface so they can use it.
Here is the smallest example that I could think of at this point which illustrates my issue:
package main
import (
"fmt"
"strconv"
)
type Base interface {
Required() string
}
type Concrete struct {
_data int
}
func (con *Concrete) Required() string {
return strconv.Itoa(con._data)
}
func convert(val interface{}) *Base {
if con,ok := val.(*Base); ok {
return con
}
return nil
}
func main() {
conc := new(Concrete)
conc._data = 5
base := convert(conc)
fmt.Println(base)
}
In the code above I really wish that convert would convert the type to *Base. The function convert will return the value nil instead of the lovely value I wish it to be.
Edit: Removed unused code, I thought I had already removed it but I guess not.