Turn Option type into an alias
This commit is contained in:
parent
6b9db4c2a1
commit
70fb106b48
27
option.go
27
option.go
@ -2,29 +2,6 @@ package hopp
|
||||
|
||||
import "git.tebibyte.media/sashakoshka/go-util/container"
|
||||
|
||||
// Option allows an optional value to be defined without using a pointer.
|
||||
// TODO make generic alias once go 1.24 releases
|
||||
type Option[T any] ucontainer.Optional[T]
|
||||
// Option is an alias for ucontainer.Option, defined here for convenience
|
||||
type Option[T any] = ucontainer.Option[T]
|
||||
|
||||
func O[T any](value T) Option[T] {
|
||||
return Option[T](ucontainer.O(value))
|
||||
}
|
||||
|
||||
func Void[T any]() Option[T] {
|
||||
return Option[T](ucontainer.Void[T]())
|
||||
}
|
||||
|
||||
func (option Option[T]) Ok() bool {
|
||||
return (ucontainer.Optional[T])(option).Exists()
|
||||
}
|
||||
|
||||
func (option Option[T]) Get() (T, bool) {
|
||||
return (ucontainer.Optional[T])(option).Value()
|
||||
}
|
||||
|
||||
func (option Option[T]) Default(defaul T) T {
|
||||
if value, ok := option.Get(); ok {
|
||||
return value
|
||||
}
|
||||
return defaul
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user