@@ -10,6 +10,7 @@ import (
1010 "internal/goarch"
1111 "internal/itoa"
1212 "internal/unsafeheader"
13+ "iter"
1314 "math"
1415 "runtime"
1516 "unsafe"
@@ -2620,6 +2621,47 @@ func (v Value) UnsafePointer() unsafe.Pointer {
26202621 panic (& ValueError {"reflect.Value.UnsafePointer" , v .kind ()})
26212622}
26222623
2624+ // Fields yields each field of v, along with its description.
2625+ //
2626+ // It panics if v's Kind is not Struct.
2627+ //
2628+ // The i'th field yielded by Fields is the same as [Type.Field(i)] and [Value.Field(i)].
2629+ func (v Value ) Fields () iter.Seq2 [StructField , Value ] {
2630+ return func (yield func (StructField , Value ) bool ) {
2631+ rtype := v .Type ()
2632+ for i := range v .NumField () {
2633+ if ! yield (rtype .Field (i ), v .Field (i )) {
2634+ return
2635+ }
2636+ }
2637+ }
2638+ }
2639+
2640+ // Methods yields a function value corresponding to each method of v,
2641+ // along with a description of the method.
2642+ //
2643+ // The arguments to a Call on the yielded function value should not include a receiver;
2644+ // the returned function will always use v as the receiver. Note that [Method.Type]
2645+ // in each yielded [Method] does include the receiver, and thus is not the same as
2646+ // [Value.Type].
2647+ //
2648+ // Methods panics if v is a nil interface value.
2649+ //
2650+ // The i'th method yielded by Methods is the same as [Type.Method(i)] and [Value.Method(i)].
2651+ //
2652+ // Calling this method will force the linker to retain all exported methods in all packages.
2653+ // This may make the executable binary larger but will not affect execution time.
2654+ func (v Value ) Methods () iter.Seq2 [Method , Value ] {
2655+ return func (yield func (Method , Value ) bool ) {
2656+ rtype := v .Type ()
2657+ for i := range v .NumMethod () {
2658+ if ! yield (rtype .Method (i ), v .Method (i )) {
2659+ return
2660+ }
2661+ }
2662+ }
2663+ }
2664+
26232665// StringHeader is the runtime representation of a string.
26242666// It cannot be used safely or portably and its representation may
26252667// change in a later release.
@@ -3221,8 +3263,8 @@ func (v Value) Comparable() bool {
32213263 return v .IsNil () || v .Elem ().Comparable ()
32223264
32233265 case Struct :
3224- for i := 0 ; i < v . NumField (); i ++ {
3225- if ! v . Field ( i ) .Comparable () {
3266+ for _ , value := range v . Fields () {
3267+ if ! value .Comparable () {
32263268 return false
32273269 }
32283270 }
0 commit comments