-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsome.go
38 lines (30 loc) · 795 Bytes
/
some.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package types
type Some[T any] struct {
Value T
}
// option implements Option[T].option
//
//lint:ignore U1000 method is used to implement Option[T]
func (Some[T]) option(t T) {}
// Deconstruct implements Option[T].Deconstruct()
func (s Some[T]) Deconstruct() (T, bool) {
return s.Value, true
}
// IsSome implementes Option[T].IsSome
func (s Some[T]) IsSome() bool { return true }
// IsNone implementes Option[T].IsNone
func (s Some[T]) IsNone() bool { return false }
// Unwrap impelements Option[T].Unwrap
func (s Some[T]) Unwrap() T {
return s.Value
}
// UnwrapOr implements Option[T].UnwrapOr
func (s Some[T]) UnwrapOr(other T) T {
return s.Value
}
// NewSome returns a Some[T] that wraps the value T
func NewSome[T any](value T) Option[T] {
return Some[T]{
Value: value,
}
}