Skip to content

Improve Option documentation and add MaybeSome function #9222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading