fn: add OptionFromPtr function
Add a function `OptionFromPtr` that constructs an `Option` from a pointer. The function signature is: `OptionFromPtr : *A -> Option[A]`. This utility has proven useful in the taproot-assets project.
This commit is contained in:
1 file changed
+11
@@ -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