opt.Optional[T].IsNothing() opt.Optional[T].IsSomething()

master
Charles Iliya Krempeaux 2024-06-16 07:03:11 -07:00
parent f34a58dd58
commit 1cc2b9ffdb
4 changed files with 7 additions and 11 deletions

2
map.go
View File

@ -39,7 +39,7 @@ package opt
// //
// // result2 == opt.Nothing[int]() // // result2 == opt.Nothing[int]()
func Map[T1 any, T2 any](op Optional[T1], fn func(T1)T2) Optional[T2] { func Map[T1 any, T2 any](op Optional[T1], fn func(T1)T2) Optional[T2] {
if op.isnothing() { if op.IsNothing() {
return Nothing[T2]() return Nothing[T2]()
} }

View File

@ -107,7 +107,7 @@ func Something[T any](value T) Optional[T] {
// //
// op3 = op3.Filter(fn) // op3 = op3.Filter(fn)
func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] { func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] {
if receiver.isnothing() { if receiver.IsNothing() {
return Nothing[T]() return Nothing[T]()
} }
@ -118,10 +118,6 @@ func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] {
return receiver 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. // Get returns the value inside of the optional-type if it is holding something.
// //
// Example usage: // Example usage:
@ -157,7 +153,7 @@ func (receiver Optional[T]) Get() (T, bool) {
// fmt.Println("nothing") // fmt.Println("nothing")
// } // }
func (receiver Optional[T]) GetElse(alternative T) T { func (receiver Optional[T]) GetElse(alternative T) T {
if receiver.isnothing() { if receiver.IsNothing() {
return alternative return alternative
} }
@ -189,7 +185,7 @@ func (receiver Optional[T]) GetElse(alternative T) T {
// // Output: // // Output:
// // op = opt.Nothing[uint8]() // // op = opt.Nothing[uint8]()
func (receiver Optional[T]) GoString() string { func (receiver Optional[T]) GoString() string {
if receiver.isnothing() { if receiver.IsNothing() {
return fmt.Sprintf("opt.Nothing[%T]()", receiver.value) return fmt.Sprintf("opt.Nothing[%T]()", receiver.value)
} }
@ -210,7 +206,7 @@ func (receiver Optional[T]) GoString() string {
// //@TODO // //@TODO
// }) // })
func (receiver Optional[T]) WhenNothing(fn func()) { func (receiver Optional[T]) WhenNothing(fn func()) {
if receiver.isnothing() { if receiver.IsNothing() {
fn() fn()
} }
} }

View File

@ -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) 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) return nil, erorr.Errorf("opt: cannot marshal opt.Nothing[%T]() into JSON", receiver.value)
} }

View File

@ -36,7 +36,7 @@ package opt
// //
// // result2 == opt.Nothing[byte]() // // result2 == opt.Nothing[byte]()
func Then[T1 any, T2 any](op Optional[T1], fn func(T1)Optional[T2]) Optional[T2] { func Then[T1 any, T2 any](op Optional[T1], fn func(T1)Optional[T2]) Optional[T2] {
if op.isnothing() { if op.IsNothing() {
return Nothing[T2]() return Nothing[T2]()
} }