Skip to content

Commit

Permalink
lang: types: Add a comparable helper to our types library
Browse files Browse the repository at this point in the history
  • Loading branch information
purpleidea committed Jul 1, 2024
1 parent cdc2439 commit dc33d9a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lang/types/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ func TypeStructTagToFieldName(st reflect.Type) (map[string]string, error) {
return result, nil
}

// IsComparableKind returns true if you pass it a comparable kind. These have a
// Cmp method on the Value interface that won't panic. Notably KindFunc and any
// other special kinds are not present in this list.
func IsComparableKind(kind Kind) bool {
switch kind {
case KindBool:
return true
case KindStr:
return true
case KindInt:
return true
case KindFloat:
return true
case KindList:
return true
case KindMap:
return true
case KindStruct:
return true
case KindFunc:
return false // not comparable!
}
return false // others
}

// Iter applies a function to each type in the top-level type. It stops if that
// function errors, and returns that error to the top-level caller. It panics if
// it encounters an invalid or partial type struct. This version starts at the
Expand Down

0 comments on commit dc33d9a

Please sign in to comment.