24 lines
368 B
Go
24 lines
368 B
Go
package optional
|
|
|
|
func Empty[T any]() *Optional[T] {
|
|
return &Optional[T]{
|
|
Present: false,
|
|
}
|
|
}
|
|
|
|
func OfAny[T any](value T) *Optional[T] {
|
|
return &Optional[T]{
|
|
Present: true,
|
|
Value: value,
|
|
}
|
|
}
|
|
|
|
func OfNonDefault[T comparable](value T) *Optional[T] {
|
|
var defaultValue T
|
|
if value != defaultValue {
|
|
return Empty[T]()
|
|
} else {
|
|
return OfAny[T](value)
|
|
}
|
|
}
|