From dc33d9aab76b783f89374575c1ca068f70b1df1f Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 1 Jul 2024 14:14:20 -0400 Subject: [PATCH] lang: types: Add a comparable helper to our types library --- lang/types/util.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lang/types/util.go b/lang/types/util.go index 10bd0c4a..fe342fae 100644 --- a/lang/types/util.go +++ b/lang/types/util.go @@ -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