Skip to content

Commit

Permalink
fn: add Option.String method
Browse files Browse the repository at this point in the history
Introduce the `String` method for `Option` to improve logging
capabilities.
  • Loading branch information
ffranr committed Jan 6, 2025
1 parent e1d9b1a commit 8a5bfad
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion fn/option.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package fn

// Option[A] represents a value which may or may not be there. This is very
import "fmt"

// 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
}

// String returns a string representation of the Option.
func (o Option[A]) String() string {
if o.isSome {
return fmt.Sprintf("Some(%v)", o.some)
}

return "None"
}

// Some trivially injects a value into an optional context.
//
// Some : A -> Option[A].
Expand Down

0 comments on commit 8a5bfad

Please sign in to comment.