mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-06 17:47:01 +02:00
Merge pull request #9222 from ffranr/add-maybesome-opt
Improve Option documentation and add MaybeSome function
This commit is contained in:
15
fn/option.go
15
fn/option.go
@@ -2,8 +2,8 @@ package fn
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
// Option[A] represents a value which may or may not be there. This is very
|
// Option represents a value which may or may not be there. This is very often
|
||||||
// often preferable to nil-able pointers.
|
// preferable to nil-able pointers.
|
||||||
type Option[A any] struct {
|
type Option[A any] struct {
|
||||||
isSome bool
|
isSome bool
|
||||||
some A
|
some A
|
||||||
@@ -26,6 +26,17 @@ func None[A any]() Option[A] {
|
|||||||
return Option[A]{}
|
return Option[A]{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OptionFromPtr constructs an option from a pointer.
|
||||||
|
//
|
||||||
|
// OptionFromPtr : *A -> Option[A].
|
||||||
|
func OptionFromPtr[A any](a *A) Option[A] {
|
||||||
|
if a == nil {
|
||||||
|
return None[A]()
|
||||||
|
}
|
||||||
|
|
||||||
|
return Some[A](*a)
|
||||||
|
}
|
||||||
|
|
||||||
// ElimOption is the universal Option eliminator. It can be used to safely
|
// ElimOption is the universal Option eliminator. It can be used to safely
|
||||||
// handle all possible values inside the Option by supplying two continuations.
|
// handle all possible values inside the Option by supplying two continuations.
|
||||||
//
|
//
|
||||||
|
Reference in New Issue
Block a user