Skip to content

Commit

Permalink
Merge pull request #9222 from ffranr/add-maybesome-opt
Browse files Browse the repository at this point in the history
Improve Option documentation and add MaybeSome function
  • Loading branch information
guggero authored Oct 30, 2024
2 parents 7ceb3d6 + 71298f7 commit 4c48402
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions fn/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package fn

import "testing"

// Option[A] represents a value which may or may not be there. This is very
// often preferable to nil-able pointers.
// Option represents a value which may or may not be there. This is very often
// preferable to nil-able pointers.
type Option[A any] struct {
isSome bool
some A
Expand All @@ -26,6 +26,17 @@ func None[A any]() 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
// handle all possible values inside the Option by supplying two continuations.
//
Expand Down

0 comments on commit 4c48402

Please sign in to comment.