/
ConstrainedType_Value.resi
90 lines (78 loc) · 2.7 KB
/
ConstrainedType_Value.resi
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
A value of type 'value that satisfies the constraint with id 'id.
ConstraintType.Value.t<'value, 'id> is implemented as 'value. While this is
an implementation detail as far as the Rescript compiler is concerned, it
is part of the contract of this module, and as such, it is safe to assume
in your code. This is useful in Javascript bindings when you want to
constraint the parameters of an external JS function.
For example, suppose you have an external function "foo" that takes a single
number parameter. You could interop with this function in Rescript as
follows:
```rescript
module MyConstraint = Constraint.Make({
type t = int
let isSatisfied = ...
})
type fooResult = ...
external foo: t<int, MyConstraint.identity> => fooResult = "foo"
```
WARNING:
If the 'value type of of a ConstraintType.Value.t<'value, 'id> object is
mutable, then instances of ConstraintType.Value.t<'value, 'id> may not
actually satisfy the constraint specified by 'id. This could be true even
if all instances of ConstraintType.Value.t<'value, 'id> are created with
make or makeExn.
*/
type t<'value, 'id>
/**
@param value
@param ~constraint_
@return Some(value) if ~constraint_(value) returns true. None otherwise
@throws Any exception thrown by constraint_
*/
let make: (
'value,
~constraint_: ConstrainedType_Constraint.t<'value, 'id>,
) => option<t<'value, 'id>>
exception ConstraintUnsatisfied
/**
@param value
@param ~constraint_
@return value if ~constraint_(value) returns true
@throws ConstraintUnsatisfied if ~constraint_(value) returns false
@throws Any exception thrown by constraint_
*/
let makeExn: ('value, ~constraint_: ConstrainedType_Constraint.t<'value, 'id>) => t<'value, 'id>
/**
@param value
@param ~constraint_
@return value
@property Does not execute constraint_ on value
*/
let makeUnsafe: ('value, ~constraint_: ConstrainedType_Constraint.t<'value, 'id>) => t<'value, 'id>
/**
@param constrainedValue
@return The argument, as an unconstrained value
*/
let value: t<'value, 'id> => 'value
exception AssertionFailure
/**
Re-asserts that the constraint is still satisfied on the value. This is useful when the underlying type
is mutable and you want to verify that you haven't accidentally mutated it in a way that violates.
the constraint.
It is pointless calling this function if the underlying type is immutable.
@param value
@throws AssertionFailure if ~constraint_(value) returns false
@throws Any exception thrown by constraint_
*/
let assertConstraint: (
t<'value, 'id>,
~constraint_: ConstrainedType_Constraint.t<'value, 'id>,
) => unit
module All: {
/**
@param value
@return value, constrained with the "All" constraint
*/
let make: 'value => t<'value, ConstrainedType_Constraint.All.identity>
}