2023-07-21 23:05:40 +02:00
|
|
|
package generics
|
|
|
|
|
2024-05-28 00:20:32 +02:00
|
|
|
// Ptr returns pointer for given value.
|
2023-07-21 23:05:40 +02:00
|
|
|
func Ptr[T any](val T) *T {
|
|
|
|
return &val
|
|
|
|
}
|
|
|
|
|
2024-05-28 00:20:32 +02:00
|
|
|
// Val returns value of given pointer or default value if pointer is nil.
|
2023-07-21 23:05:40 +02:00
|
|
|
func Val[T any](ptr *T) T {
|
|
|
|
var val T
|
|
|
|
if ptr != nil {
|
|
|
|
val = *ptr
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2024-05-28 00:20:32 +02:00
|
|
|
// Fallback returns value of given pointer or fallback value if pointer is nil.
|
2023-07-21 23:05:40 +02:00
|
|
|
func Fallback[T any](ptr *T, fallback T) T {
|
|
|
|
if ptr != nil {
|
|
|
|
return *ptr
|
|
|
|
}
|
|
|
|
return fallback
|
|
|
|
}
|