diff options
author | Keuin <[email protected]> | 2022-09-07 02:48:46 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-09-07 02:48:46 +0800 |
commit | 8e15d802865ed57db0018c15ea5559c8bd44c01f (patch) | |
tree | 48f4632a1ad044bd7f7f8da3ebe2bb03ab4ca6fe /common/orelse.go | |
parent | 88234ca8fffc4e120adbe0d38071b625ad2f43c7 (diff) |
First working version. Just a POC.
Diffstat (limited to 'common/orelse.go')
-rw-r--r-- | common/orelse.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/common/orelse.go b/common/orelse.go new file mode 100644 index 0000000..a96bde7 --- /dev/null +++ b/common/orelse.go @@ -0,0 +1,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 +} |