Просмотр исходного кода

Added a generic Pair type based on C++ std::pair

Matt Bolt 4 лет назад
Родитель
Сommit
1375004c82
1 измененных файлов с 23 добавлено и 0 удалено
  1. 23 0
      pkg/kubecost/common.go

+ 23 - 0
pkg/kubecost/common.go

@@ -0,0 +1,23 @@
+package kubecost
+
+// Pair is a generic struct containing a pair of instances, one of each type similar to std::pair
+type Pair[T any, U any] struct {
+	First  T
+	Second U
+}
+
+// Creates a new pair struct containing the provided parameters. This is useful for creating types
+// capable of representing common paired types (result, error), (result, bool), etc...
+func NewPair[T any, U any](first T, second U) Pair[T, U] {
+	return Pair[T, U]{
+		First:  first,
+		Second: second,
+	}
+}
+
+// DefaultValue[T] returns the default value for any generic type. This is helpful for generic
+// types where a type parameter can be a value type or pointer.
+func DefaultValue[T any]() T {
+	var t T
+	return t
+}