Open earthboundkid opened 4 months ago
could this be a duplicate of #66626?
could this be a duplicate of https://github.com/golang/go/issues/66626?
No, that issue relates to the go/types package, not reflect.
ah, overlooked this crucial detail, thank you!
This was in fact inspired by #66626, but yes, it is for reflect, not types.
Alternatively, the method value could be the iterator as opposed to returning an iterator, like so:
package reflect
type Type interface {
...
// Fields is a go1.23 iterator over all the fields of a struct type (etc).
//
// Example: for ftype := range t.Fields { ... }
Fields(yield func(ftype *Func) bool)
}
func (t *rtype) Fields(yield func(ftype *Func) bool) {
for i := range t.NumFields() {
if !yield(t.Field(i)) {
break
}
}
}
// client code
var t reflect.Type = ...
for ftype := range t.Fields { ... }
Maybe. That keeps from introducing an import dependency we might not want, but it goes against the general conventions used elsewhere.
I used this in a project and the signature should be iter.Seq2[int, reflect.StructField]
so that you can do rv.Field(i).Set(whatever)
if the field matches some criteria. It's very convenient though and worth having.
I'd like an iterator version of VisibleFields()
, too. I had a need for that the other day, actually.
Isn't that just a filter on field.IsExported() or is there more to it than that? I did only want the exported fields for my thing, but I also needed a type and tag test for the field, so having VisibleFields() per se wouldn't have saved much coding.
VisibleFields()
gets the fields recursively into anonymous struct fields as well as unexported ones.
Got it. There's already a slice version of VisibleFields(), so I'm not sure how much the iterator version saves since you'll presumably cache the slice somehow if performance is a concern. I'm willing to be wrong though. There could be AllVisibleFields and VisibleFields could just call and collect that.
Proposal Details
reflect.Type.Fields() iter.Seq[StructField]
andreflect.Type.Methods() iter.Seq[Method]
would be more convenient than having to calli := range myType.NumFields()
andmyType.Field(i)
etc.You could also add iterators to
reflect.Value
as well but those are less obviously useful. Probably worth doing just for completeness though.