This repository was archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTypeSpec.hack
More file actions
155 lines (122 loc) · 3.75 KB
/
TypeSpec.hack
File metadata and controls
155 lines (122 loc) · 3.75 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2016, Fred Emmott
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\TypeSpec;
abstract class TypeSpec<+T> {
private ?Trace $trace = null;
abstract public function coerceType(mixed $value): T;
abstract public function assertType(mixed $value): T;
abstract public function toString(): string;
public function isOptional(): bool {
return false;
}
final protected function getTrace(): Trace {
return $this->trace ?? new Trace();
}
final protected function withTrace(Trace $trace): TypeSpec<T> {
$new = clone $this;
$new->trace = $trace;
return $new;
}
}
function arraykey(): TypeSpec<arraykey> {
return new __Private\ArrayKeySpec();
}
function bool(): TypeSpec<bool> {
return new __Private\BoolSpec();
}
function classname<T>(classname<T> $what): TypeSpec<classname<T>> {
return new __Private\ClassnameSpec($what);
}
function constmap<Tk as arraykey, Tv>(
TypeSpec<Tk> $tsk,
TypeSpec<Tv> $tsv,
): TypeSpec<\ConstMap<Tk, Tv>> {
return new __Private\MapSpec(\ConstMap::class, $tsk, $tsv);
}
function constset<Tv as arraykey>(TypeSpec<Tv> $tsv): TypeSpec<\ConstSet<Tv>> {
return new __Private\SetSpec(\ConstSet::class, $tsv);
}
function constvector<Tv>(TypeSpec<Tv> $inner): TypeSpec<\ConstVector<Tv>> {
return new __Private\VectorSpec(\ConstVector::class, $inner);
}
function dict<Tk as arraykey, Tv>(
TypeSpec<Tk> $tsk,
TypeSpec<Tv> $tsv,
): TypeSpec<dict<Tk, Tv>> {
return new __Private\DictSpec($tsk, $tsv);
}
function enum<T as arraykey>(\HH\enumname<T> $what): TypeSpec<T> {
return new __Private\EnumSpec($what);
}
function float(): TypeSpec<float> {
return new __Private\FloatSpec();
}
function instance_of<T>(classname<T> $what): TypeSpec<T> {
return new __Private\InstanceOfSpec($what);
}
function int(): TypeSpec<int> {
return new __Private\IntSpec();
}
function immmap<Tk as arraykey, Tv>(
TypeSpec<Tk> $tsk,
TypeSpec<Tv> $tsv,
): TypeSpec<ImmMap<Tk, Tv>> {
return new __Private\MapSpec(ImmMap::class, $tsk, $tsv);
}
function immset<Tv as arraykey>(TypeSpec<Tv> $tsv): TypeSpec<ImmSet<Tv>> {
return new __Private\SetSpec(ImmSet::class, $tsv);
}
function immvector<Tv>(TypeSpec<Tv> $inner): TypeSpec<ImmVector<Tv>> {
return new __Private\VectorSpec(ImmVector::class, $inner);
}
function keyset<Tk as arraykey>(TypeSpec<Tk> $inner): TypeSpec<keyset<Tk>> {
return new __Private\KeysetSpec($inner);
}
function map<Tk as arraykey, Tv>(
TypeSpec<Tk> $tsk,
TypeSpec<Tv> $tsv,
): TypeSpec<Map<Tk, Tv>> {
return new __Private\MapSpec(Map::class, $tsk, $tsv);
}
function mixed(): TypeSpec<mixed> {
return new __Private\MixedSpec();
}
function nonnull(): TypeSpec<nonnull> {
return new __Private\NonNullSpec();
}
function null(): TypeSpec<null> {
return new __Private\NullSpec();
}
function nullable<T>(TypeSpec<T> $inner): TypeSpec<?T> {
return new __Private\NullableSpec($inner);
}
function num(): TypeSpec<num> {
return new __Private\NumSpec();
}
function resource(?string $kind = null): TypeSpec<resource> {
return new __Private\ResourceSpec($kind);
}
function set<Tv as arraykey>(TypeSpec<Tv> $tsv): TypeSpec<Set<Tv>> {
return new __Private\SetSpec(Set::class, $tsv);
}
function string(): TypeSpec<string> {
return new __Private\StringSpec();
}
function vec<Tv>(TypeSpec<Tv> $inner): TypeSpec<vec<Tv>> {
return new __Private\VecSpec($inner);
}
function vector<Tv>(TypeSpec<Tv> $inner): TypeSpec<Vector<Tv>> {
return new __Private\VectorSpec(Vector::class, $inner);
}
function of<reify T>(): TypeSpec<T> {
return __Private\from_type_structure(
\HH\ReifiedGenerics\get_type_structure<T>(),
);
}