From 3a1528b0d3a92e598566f1722f1a97d2c90fe4ee Mon Sep 17 00:00:00 2001 From: xuanhong Date: Fri, 18 Oct 2024 14:35:36 +0800 Subject: [PATCH 1/2] docs: clarify `Empty` returns zero value https://go.dev/ref/spec#The_zero_value does not define "empty value". It informally mentions "empty value", meaning (non-nil && 0-length) slice or map. > Note that the zero value for a slice or map type is not the same as an initialized but empty value of the same type. --- type_manipulation.go | 2 +- type_manipulation_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/type_manipulation.go b/type_manipulation.go index 1fdd37b8..6e8d50e1 100644 --- a/type_manipulation.go +++ b/type_manipulation.go @@ -106,7 +106,7 @@ func FromAnySlice[T any](in []any) (out []T, ok bool) { return result, true } -// Empty returns an empty value. +// Empty returns the zero value (https://go.dev/ref/spec#The_zero_value). func Empty[T any]() T { var zero T return zero diff --git a/type_manipulation_test.go b/type_manipulation_test.go index 3a63013d..b2de25db 100644 --- a/type_manipulation_test.go +++ b/type_manipulation_test.go @@ -180,6 +180,8 @@ func TestEmpty(t *testing.T) { is.Empty(Empty[int64]()) is.Empty(Empty[test]()) is.Empty(Empty[chan string]()) + is.Nil(Empty[[]int]()) + is.Nil(Empty[map[string]int]()) } func TestIsEmpty(t *testing.T) { From 61401630dd70a1e506db09e6765a132943d77c34 Mon Sep 17 00:00:00 2001 From: xuanhong Date: Fri, 18 Oct 2024 20:10:53 +0800 Subject: [PATCH 2/2] docs: other "empty value" -> "zero value" --- README.md | 4 ++-- intersect.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 73c33223..5b777763 100644 --- a/README.md +++ b/README.md @@ -2099,7 +2099,7 @@ subset := lo.Without([]int{0, 2, 10}, 0, 1, 2, 3, 4, 5) ### WithoutEmpty -Returns slice excluding empty values. +Returns slice excluding zero values. ```go subset := lo.WithoutEmpty([]int{0, 2, 10}) @@ -2788,7 +2788,7 @@ elements, ok := lo.FromAnySlice([]any{"foobar", "42"}) ### Empty -Returns an empty value. +Returns the [zero value](https://go.dev/ref/spec#The_zero_value). ```go lo.Empty[int]() diff --git a/intersect.go b/intersect.go index b2cf0727..e0431742 100644 --- a/intersect.go +++ b/intersect.go @@ -176,7 +176,7 @@ func Without[T comparable, Slice ~[]T](collection Slice, exclude ...T) Slice { return result } -// WithoutEmpty returns slice excluding empty values. +// WithoutEmpty returns slice excluding zero values. // // Deprecated: Use lo.Compact instead. func WithoutEmpty[T comparable, Slice ~[]T](collection Slice) Slice {