From c69c35bef4aa6305700fa4bab77142b6afc799fc Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 18:16:01 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Replace=20type=20'any'=20with=20?= =?UTF-8?q?type=20'NonNilConcrete'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/types.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/utils/types.go b/utils/types.go index 109b3f0..6a070d3 100644 --- a/utils/types.go +++ b/utils/types.go @@ -5,7 +5,11 @@ import ( "errors" ) -type IOption[T any] interface { +type NonNilConcrete interface { + ~int | ~float64 | ~string | ~bool +} + +type IOption[T NonNilConcrete] interface { IsNone() bool ValueOrErr() (*T, error) ValueOr(def *T) *T @@ -14,7 +18,7 @@ type IOption[T any] interface { } // Don't call this struct directly, use NewOption[T] or NewNoneOption[T] instead. -type option[T any] struct { +type option[T NonNilConcrete] struct { // value holds the actual value of the Option if it is not None. value T // none indicates whether the Option is None (i.e., has no value). @@ -58,10 +62,10 @@ func (o *option[T]) UnmarshalJSON(data []byte) error { return json.Unmarshal(data, &o.value) } -func NewOption[T any](value T) *option[T] { +func NewOption[T NonNilConcrete](value T) *option[T] { return &option[T]{value: value} } -func NewNoneOption[T any]() *option[T] { +func NewNoneOption[T NonNilConcrete]() *option[T] { return &option[T]{none: true} }