graph: decide whether empty collections of nodes, edges and lines are nil or just empty
Created by: kortschak
With the old approach we could always return a nil
and people could still iterate over it without needing to check for nil
. This is no longer true since the container types returned by these methods are interface values now. The graph.<Types>Of
helpers hide this for the most part at the moment because they are guaranteed to work with nil
input, but user code is not.
We currently don't specify the nil
-ness of returns for collections, so this is not terrible, but it might be an idea to tighten that down.
There are, I think. three approaches:
- leave it as is and let users handle the
nil
values. - require that containers are always non-
nil
. This could be facilitated by providing an empty iterator type in graph.
// Empty is an empty iterator.
type Empty struct{}
func (Empty) Next() bool { return false }
func (Empty) Len() int { return 0 }
func (Empty) Reset() {}
func (Empty) Node() Node { return nil }
func (Empty) Edge() Edge { return nil }
func (Empty) WeightedEdge() WeightedEdge { return nil }
func (Empty) Line() Line { return nil }
func (Empty) WeightedLine() WeightedLine { return nil }
- provide an iterator helper functions that wraps
it.Next
and performs the check fornil
like so:
func Next(it Iterator) bool {
return it != nil && it.Next()
}