-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefinements.go
46 lines (35 loc) · 864 Bytes
/
refinements.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
39
40
41
42
43
44
45
46
package refined
import (
"fmt"
)
type Refined[A any, P Predicate] interface {
Value() A
_PedicateType() *P // always nil (used to forbid type casting)
}
type refined[A any, P Predicate] struct{ value A }
func (r refined[A, P]) Value() A { return r.value }
func (r refined[A, P]) _PedicateType() *P { return nil }
type Predicate interface {
applyPredicate(any) (bool, error)
}
func (rv refined[A, R]) String() string {
return fmt.Sprint(rv.value)
}
func Refine[T any, P Predicate](v T) (Refined[T, P], error) {
var predicate P
res, err := predicate.applyPredicate(v)
if err != nil {
return nil, err
}
if res {
return refined[T, P]{v}, nil
}
return nil, NonComplianceError[T, P]{v, predicate}
}
func RefineUnsafe[T any, P Predicate](v T) Refined[T, P] {
res, err := Refine[T, P](v)
if err != nil {
panic(err)
}
return res
}