Skip to content

Commit 8a5bfad

Browse files
committed
fn: add Option.String method
Introduce the `String` method for `Option` to improve logging capabilities.
1 parent e1d9b1a commit 8a5bfad

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

fn/option.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package fn
22

3-
// Option[A] represents a value which may or may not be there. This is very
3+
import "fmt"
4+
5+
// Option represents a value which may or may not be there. This is very
46
// often preferable to nil-able pointers.
57
type Option[A any] struct {
68
isSome bool
79
some A
810
}
911

12+
// String returns a string representation of the Option.
13+
func (o Option[A]) String() string {
14+
if o.isSome {
15+
return fmt.Sprintf("Some(%v)", o.some)
16+
}
17+
18+
return "None"
19+
}
20+
1021
// Some trivially injects a value into an optional context.
1122
//
1223
// Some : A -> Option[A].

0 commit comments

Comments
 (0)