IsIterType returns true if the underlying type is an iterator func: func(func() bool) func(func(K) bool) func(func(K, V) bool) See more at https://tip.golang.org/doc/go1.23.
(t types.Type)
| 260 | // |
| 261 | // See more at https://tip.golang.org/doc/go1.23. |
| 262 | func IsIterType(t types.Type) bool { |
| 263 | // Ensure it is a function signature. |
| 264 | sig, ok := t.Underlying().(*types.Signature) |
| 265 | if !ok { |
| 266 | return false |
| 267 | } |
| 268 | |
| 269 | // Ensure it has exactly one parameter (the yield func). |
| 270 | params := sig.Params() |
| 271 | if params.Len() != 1 { |
| 272 | return false |
| 273 | } |
| 274 | |
| 275 | // Ensure the single parameter is a function type (the yield func). |
| 276 | paramType, ok := params.At(0).Type().Underlying().(*types.Signature) |
| 277 | if !ok { |
| 278 | return false |
| 279 | } |
| 280 | |
| 281 | // Ensure the yield func takes fewer than 2 arguments and returns exactly one boolean value. |
| 282 | res := paramType.Results() |
| 283 | if paramType.Params().Len() > 2 || res.Len() != 1 { |
| 284 | return false |
| 285 | } |
| 286 | |
| 287 | // Final check: ensure the return type of the yield func is a boolean. |
| 288 | basic, ok := res.At(0).Type().Underlying().(*types.Basic) |
| 289 | return ok && basic.Kind() == types.Bool |
| 290 | } |
| 291 | |
| 292 | // GetFuncSignature returns the signature of a function or an anonymous function. |
| 293 | func GetFuncSignature(t types.Type) *types.Signature { |