diff --git a/container/optional.go b/container/optional.go index 8814358..614c522 100644 --- a/container/optional.go +++ b/container/optional.go @@ -1,31 +1,31 @@ package ucontainer -// Optional is an optional value. +// Optional can either hold a value, or nothing. type Optional[T any] struct { value T exists bool } -// Value returns the value and true if the value exists. If not, it returns the -// last set value and false. -func (this *Optional[T]) Value () (T, bool) { - return this.value, this.exists +// O creates a new optional with the specified value. +func O[T any] (value T) Optional[T] { + return Optional[T] { + value: value, + exists: true, + } } -// Set sets the value. -func (this *Optional[T]) Set (value T) { - this.value = value - this.exists = true -} - -// Unset unsets the value. -func (this *Optional[T]) Unset () { - var zero T - this.value = zero - this.exists = false +// Void returns an optional with no value. +func Void[T any] () Optional[T] { + return Optional[T] { } } // Exists returns if the value is currently set. -func (this *Optional[T]) Exists () bool { - return this.exists +func (optional Optional[T]) Exists () bool { + return optional.exists +} + +// Value returns the value and true if the value exists. If not, it returns the +// zero value and false. +func (optional Optional[T]) Value () (T, bool) { + return optional.value, optional.exists }