container: Rename Optional to Option
This commit is contained in:
parent
417eb2f962
commit
7f9b0617e0
31
container/option.go
Normal file
31
container/option.go
Normal file
@ -0,0 +1,31 @@
|
||||
package ucontainer
|
||||
|
||||
// Option can either hold a value, or nothing.
|
||||
type Option[T any] struct {
|
||||
value T
|
||||
exists bool
|
||||
}
|
||||
|
||||
// O creates a new option with the specified value.
|
||||
func O[T any] (value T) Option[T] {
|
||||
return Option[T] {
|
||||
value: value,
|
||||
exists: true,
|
||||
}
|
||||
}
|
||||
|
||||
// Void returns an option with no value.
|
||||
func Void[T any] () Option[T] {
|
||||
return Option[T] { }
|
||||
}
|
||||
|
||||
// Exists returns if the value is currently set.
|
||||
func (option Option[T]) Exists () bool {
|
||||
return option.exists
|
||||
}
|
||||
|
||||
// Value returns the value and true if the value exists. If not, it returns the
|
||||
// zero value and false.
|
||||
func (option Option[T]) Value () (T, bool) {
|
||||
return option.value, option.exists
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
package ucontainer
|
||||
|
||||
// Optional can either hold a value, or nothing.
|
||||
type Optional[T any] struct {
|
||||
value T
|
||||
exists bool
|
||||
}
|
||||
|
||||
// O creates a new optional with the specified value.
|
||||
func O[T any] (value T) Optional[T] {
|
||||
return Optional[T] {
|
||||
value: value,
|
||||
exists: true,
|
||||
}
|
||||
}
|
||||
|
||||
// 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 (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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user