container: Give Option a "Default" method

This commit is contained in:
Sasha Koshka 2025-10-19 10:16:57 -04:00
parent 7f9b0617e0
commit 9f9d8fc661

View File

@ -29,3 +29,12 @@ func (option Option[T]) Exists () bool {
func (option Option[T]) Value () (T, bool) {
return option.value, option.exists
}
// Default returns the value if it exists, and the specified default value
// otherwise.
func (option Option[T]) Default(defaul T) T {
if value, ok := option.Value(); ok {
return value
}
return defaul
}