Skip to content

Commit 48b414e

Browse files
committed
feat: option type
1 parent 2bcb27e commit 48b414e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

internal/option/option.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package option
2+
3+
type Optional[T any] struct {
4+
value T
5+
isSet bool
6+
}
7+
8+
func (o Optional[T]) Unwrap() T {
9+
if !o.isSet {
10+
panic("option is None")
11+
}
12+
return o.value
13+
}
14+
15+
func (o Optional[T]) UnwrapOr(orValue T) T {
16+
if !o.isSet {
17+
return orValue
18+
}
19+
return o.value
20+
}
21+
22+
func (o Optional[T]) IsSome() bool {
23+
return o.isSet
24+
}
25+
26+
func (o Optional[T]) IsNone() bool {
27+
return !o.isSet
28+
}
29+
30+
func Some[T any](value T) Optional[T] {
31+
return Optional[T]{
32+
value: value,
33+
isSet: true,
34+
}
35+
}
36+
37+
func None[T any]() Optional[T] {
38+
return Optional[T]{isSet: false}
39+
}

0 commit comments

Comments
 (0)