opt.Optional[T].IsNothing() opt.Optional[T].IsSomething()
parent
f34a58dd58
commit
1cc2b9ffdb
2
map.go
2
map.go
|
@ -39,7 +39,7 @@ package opt
|
|||
//
|
||||
// // result2 == opt.Nothing[int]()
|
||||
func Map[T1 any, T2 any](op Optional[T1], fn func(T1)T2) Optional[T2] {
|
||||
if op.isnothing() {
|
||||
if op.IsNothing() {
|
||||
return Nothing[T2]()
|
||||
}
|
||||
|
||||
|
|
12
optional.go
12
optional.go
|
@ -107,7 +107,7 @@ func Something[T any](value T) Optional[T] {
|
|||
//
|
||||
// op3 = op3.Filter(fn)
|
||||
func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] {
|
||||
if receiver.isnothing() {
|
||||
if receiver.IsNothing() {
|
||||
return Nothing[T]()
|
||||
}
|
||||
|
||||
|
@ -118,10 +118,6 @@ func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] {
|
|||
return receiver
|
||||
}
|
||||
|
||||
func (receiver Optional[T]) isnothing() bool {
|
||||
return !receiver.something
|
||||
}
|
||||
|
||||
// Get returns the value inside of the optional-type if it is holding something.
|
||||
//
|
||||
// Example usage:
|
||||
|
@ -157,7 +153,7 @@ func (receiver Optional[T]) Get() (T, bool) {
|
|||
// fmt.Println("nothing")
|
||||
// }
|
||||
func (receiver Optional[T]) GetElse(alternative T) T {
|
||||
if receiver.isnothing() {
|
||||
if receiver.IsNothing() {
|
||||
return alternative
|
||||
}
|
||||
|
||||
|
@ -189,7 +185,7 @@ func (receiver Optional[T]) GetElse(alternative T) T {
|
|||
// // Output:
|
||||
// // op = opt.Nothing[uint8]()
|
||||
func (receiver Optional[T]) GoString() string {
|
||||
if receiver.isnothing() {
|
||||
if receiver.IsNothing() {
|
||||
return fmt.Sprintf("opt.Nothing[%T]()", receiver.value)
|
||||
}
|
||||
|
||||
|
@ -210,7 +206,7 @@ func (receiver Optional[T]) GoString() string {
|
|||
// //@TODO
|
||||
// })
|
||||
func (receiver Optional[T]) WhenNothing(fn func()) {
|
||||
if receiver.isnothing() {
|
||||
if receiver.IsNothing() {
|
||||
fn()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ func (receiver Optional[T]) MarshalJSON() ([]byte, error) {
|
|||
return nil, erorr.Errorf("opt: cannot marshal something of type %T into JSON because parameterized type is ‘%T’ rather than ‘bool’, ‘string’, or ‘json.Marshaler’", receiver, receiver.value)
|
||||
}
|
||||
|
||||
if receiver.isnothing() {
|
||||
if receiver.IsNothing() {
|
||||
return nil, erorr.Errorf("opt: cannot marshal opt.Nothing[%T]() into JSON", receiver.value)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue