size should probably be consistent with length if implemented. Assuming the type support iteration implementing length will be very fair.
For sum, again, if it is iterable it’ll be strange if the sum is not summing the item in iteration. If iteration returns the weight it’ll be fair but otherwise I think you’ll be better off with a different function
When you say consistent, do you mean that size should return a tuple and length should return a single number? I was struggling due to the shape of the tree because it is neither linear nor multi-dimensional
The type supports iteration in the sense that it can be traversed depth-first or breath-first (currently depth-first only). The nodes have composite type so I am implementing sum(f, itr) interface and expects function f to return a numeric type.
Yes. And although I’m not sure right now what the official rule would be, my rule of thumb will be that if you support indexing then it has an interface that looks like an array that you can have size. If you don’t then don’t bother.
Put it another way, follow Set or Dict.
In that case I’d say if the iteration returns nodes that doesn’t support arithmetic then you probably should still not special case sum even though it does’t really conflict with anything. (But sincne there’s no conflict you can also do what you feel convinient without causing much trouble). sth like sum(node_weight, iter) shouldn’t be too hard to write either though…
This depends on whether you want to support any of the interfaces.
I am not sure that the array interface, which has size, makes much sense for a tree. But if you want to support iteration, you should define Base.length and the relevant traits if applicable. And, crucially, Base.iterate.
If you do not want to support either interface, you are free to do what you like. Some types in packages define length without supporting iteration.