From 1547391126c325348821a73e9f1a529174e53330 Mon Sep 17 00:00:00 2001 From: "sashakoshka@tebibyte.media" Date: Tue, 10 Sep 2024 16:47:20 -0400 Subject: [PATCH] Add optional container --- container/optional.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 container/optional.go diff --git a/container/optional.go b/container/optional.go new file mode 100644 index 0000000..8814358 --- /dev/null +++ b/container/optional.go @@ -0,0 +1,31 @@ +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 +}