mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
26 lines
534 B
Go
26 lines
534 B
Go
package lazy
|
|
|
|
type ValueWithError[T any] struct {
|
|
inner Value[result[T]]
|
|
}
|
|
|
|
type result[T any] struct {
|
|
value T
|
|
err error
|
|
}
|
|
|
|
// NewWithError allows you to provide a lazy initializer that can fail.
|
|
func NewWithError[T any](fn func() (T, error)) *ValueWithError[T] {
|
|
return &ValueWithError[T]{
|
|
inner: Value[result[T]]{fn: func() result[T] {
|
|
value, err := fn()
|
|
return result[T]{value: value, err: err}
|
|
}},
|
|
}
|
|
}
|
|
|
|
func (v *ValueWithError[T]) Load() (T, error) {
|
|
result := v.inner.Load()
|
|
return result.value, result.err
|
|
}
|