summaryrefslogtreecommitdiff
path: root/common/orelse.go
blob: a96bde76a18f89525f5af137b36facf81f475c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package common

type Opt[T any] struct {
	thing T
	err   error
}

func Optional[T any](thing T, err error) Opt[T] {
	return Opt[T]{
		thing: thing,
		err:   err,
	}
}

func (o Opt[T]) OrElse(thing T) T {
	if o.err != nil {
		return thing
	}
	return o.thing
}