Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit 175a2c8

Browse files
committed
Add TypeSpec\of<reify T>() and type-structure based variant
- originally didn't add because naming is hard. Match FB naming - something like this is needed for potential release of FB stuff - would have made a load of the tests I recently added a lot clearer...
1 parent 264e2b3 commit 175a2c8

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

src/TypeAssert.hack

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,9 @@ function classname_of<T>(classname<T> $expected, string $what): classname<T> {
5656
}
5757

5858
function matches_type_structure<T>(TypeStructure<T> $ts, mixed $value): T {
59-
return TypeSpec\__Private\from_type_structure($ts)->assertType($value);
59+
return TypeSpec\of_type_structure($ts)->assertType($value);
6060
}
6161

6262
function matches<reify T>(mixed $value): T {
63-
return matches_type_structure(
64-
\HH\ReifiedGenerics\get_type_structure<T>(),
65-
$value,
66-
);
63+
return TypeSpec\of<T>()->assertType($value);
6764
}

src/TypeCoerce.hack

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ function arraykey(mixed $x): arraykey {
4141
}
4242

4343
function match_type_structure<T>(TypeStructure<T> $ts, mixed $value): T {
44-
return TypeSpec\__Private\from_type_structure($ts)->coerceType($value);
44+
return TypeSpec\of_type_structure($ts)->coerceType($value);
4545
}
4646

4747
function match<reify T>(mixed $value): T {
48-
return match_type_structure(
49-
\HH\ReifiedGenerics\get_type_structure<T>(),
50-
$value,
51-
);
48+
return TypeSpec\of<T>()->coerceType($value);
5249
}

src/TypeSpec.hack

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,11 @@ function varray_or_darray<Tv>(
176176
): TypeSpec<varray_or_darray<Tv>> {
177177
return new __Private\VArrayOrDArraySpec($inner);
178178
}
179+
180+
function of_type_structure<T>(TypeStructure<T> $ts): TypeSpec<T> {
181+
return __Private\from_type_structure($ts);
182+
}
183+
184+
function of<reify T>(): TypeSpec<T> {
185+
return of_type_structure(\HH\ReifiedGenerics\get_type_structure<T>());
186+
}

src/TypeSpec/__Private/ShapeSpec.hack

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ final class ShapeSpec extends TypeSpec<shape()> {
133133
$spec->toString(),
134134
),
135135
)
136-
|> $this->allowUnknownFields ? Vec\concat($$, vec['...']) : $$
136+
|> $this->allowUnknownFields ? Vec\concat($$, vec[' ...']) : $$
137137
|> Str\join($$, "\n")
138138
|> "shape(\n".$$."\n)";
139139
}

tests/ReifiedGenericsTest.hack

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Facebook\TypeAssert;
1212

13-
use namespace Facebook\TypeCoerce;
13+
use namespace Facebook\{TypeCoerce, TypeSpec};
1414

1515
use function Facebook\FBExpect\expect;
1616

@@ -48,4 +48,23 @@ final class ReifiedGenericsTest extends \Facebook\HackTest\HackTest {
4848
expect(() ==> TypeCoerce\match<this::TShapeOfVecAndDicts>('hello'))
4949
->toThrow(TypeCoercionException::class);
5050
}
51+
52+
public function testInlineTypes(): void {
53+
$valid = shape('foo' => 123);
54+
$coercable = shape('foo' => '123');
55+
expect(matches<shape('foo' => int)>($valid))->toEqual($valid);
56+
expect(matches<shape('foo' => int, ...)>($valid))->toEqual($valid);
57+
expect(TypeCoerce\match<shape('foo' => int, ...)>($valid))->toEqual($valid);
58+
expect(TypeCoerce\match<shape('foo' => int, ...)>($coercable))->toEqual(
59+
$valid,
60+
);
61+
}
62+
63+
public function testToString(): void {
64+
expect(TypeSpec\of<shape('foo' => vec<string>)>()->toString())->toEqual(
65+
"shape(\n 'foo' => HH\\vec<string>,\n)",
66+
);
67+
expect(TypeSpec\of<shape('foo' => vec<string>, ...)>()->toString())
68+
->toEqual("shape(\n 'foo' => HH\\vec<string>,\n ...\n)");
69+
}
5170
}

0 commit comments

Comments
 (0)