diff --git a/langext/coalesce.go b/langext/coalesce.go index 9b84c69..aa7d7be 100644 --- a/langext/coalesce.go +++ b/langext/coalesce.go @@ -60,3 +60,12 @@ func CoalesceStringer(s fmt.Stringer, def string) string { return s.String() } } + +func SafeCast[T any](v any, def T) T { + switch r := v.(type) { + case T: + return r + default: + return def + } +}