Commit 5a3cd4b
pkg/context/key.go
@@ -16,5 +16,5 @@ func (self Key[T]) From(ctx context.Context) T {
if value := ctx.Value(self); value != nil {
return value.(T)
}
- return x.Default[T]()
+ return x.Zero[T]()
}
pkg/x/types.go
@@ -3,7 +3,7 @@ package x
import "reflect"
func Default[T any]() T {
- var item T
+ item := Zero[T]()
if IsPtr[T](item) {
return reflect.New(reflect.TypeOf(item).Elem()).Interface().(T)
@@ -12,6 +12,11 @@ func Default[T any]() T {
return item
}
+func Zero[T any]() T {
+ var item T
+ return item
+}
+
func IsPtr[T any](item T) bool {
return Is[T](item, reflect.Pointer)
}