32 lines
637 B
Go
32 lines
637 B
Go
|
package ucontainer
|
||
|
|
||
|
// Optional is an optional value.
|
||
|
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
|
||
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|
||
|
|
||
|
// Exists returns if the value is currently set.
|
||
|
func (this *Optional[T]) Exists () bool {
|
||
|
return this.exists
|
||
|
}
|