File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // @generated by hh_manual from manual/hack/10-types/80-case-types.md
2+ // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/
3+ async function example_snippet_wrapper (): Awaitable <void > {
4+ case type Name = Variant1 | Variant2 | ... ;
5+ case type Name < T1 , T2 > as UpperBound = Variant1 | Variant2 | ... ;
6+ }
Original file line number Diff line number Diff line change 1+ // @generated by hh_manual from manual/hack/10-types/80-case-types.md
2+ // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/
3+ async function example_snippet_wrapper (): Awaitable <void > {
4+ case type SimpleCaseType = int | MyClass ;
5+
6+ case type BoundedAndGenericCaseType < + Tk as arraykey > as nonnull =
7+ keyset < Tk > | int ;
8+ }
Original file line number Diff line number Diff line change 1+ // @generated by hh_manual from manual/hack/10-types/80-case-types.md
2+ // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/
3+ // Different primitives
4+ case type Good1 = int | string | bool ;
5+
6+ // Different array kinds
7+ case type Good2 = vec <int > | dict <int , int >;
8+
9+ // vec vs shape
10+ case type Good3 = vec <int > | shape (' x' => int );
11+
12+ // Final class vs interface - it is known that C does not implement I2
13+ final class C {}
14+ interface I2 <T > {}
15+ case type Good4 = C | I2 <int >;
16+
17+ // due to the `as arraykey` bound, we know that `T` is disjoint from `float`
18+ case type GenericCaseType <T as arraykey > = T | float ;
Original file line number Diff line number Diff line change 1+ // @generated by hh_manual from manual/hack/10-types/80-case-types.md
2+ // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/
3+ // vec and Traversable overlap (vec implements Traversable)
4+ case type Bad1 = vec <int > | Traversable <string >;
5+
6+ // Two interfaces can overlap (a class could implement both)
7+ interface I1 {}
8+ interface I2 {}
9+ case type Bad2 = I1 | I2 ;
10+
11+ // vec and tuple have overlapping runtime representation
12+ case type Bad3 = vec <int > | (int , int );
13+
14+ // shape fields are not considered
15+ case type Bad4 = shape (' x' => int ) | shape (' y' => string );
16+
17+ // E is a subtype of C, so they overlap
18+ class C {}
19+ class E extends C {}
20+ case type Bad5 = C | E ;
Original file line number Diff line number Diff line change 1+ // @generated by hh_manual from manual/hack/10-types/80-case-types.md
2+ // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/
3+ case type CT = int | string ;
4+
5+ // When CT is expected (super side), we can pass int or string
6+ function accept_ct (CT $x ): void {}
7+
8+ accept_ct (42 ); // OK: int <: (int | string)
9+ accept_ct (" hello" ); // OK: string <: (int | string)
Original file line number Diff line number Diff line change 1+ // @generated by hh_manual from manual/hack/10-types/80-case-types.md
2+ // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/
3+ case type CT_Bounded as arraykey = int ;
4+ case type CT_No_Bounds = int ;
5+
6+ function expect_int (int $x ): void {}
7+ function expect_arraykey (arraykey $x ): void {}
8+ function expect_mixed (mixed $x ): void {}
9+
10+ function test (CT_Bounded $bounded , CT_No_Bounds $unbounded ): void {
11+ expect_int ($bounded ); // ERROR: arraykey </: int
12+ expect_arraykey ($bounded ); // OK: arraykey <: arraykey
13+ expect_mixed ($bounded ); // OK: arraykey <: mixed
14+
15+ expect_int ($unbounded ); // ERROR: mixed </: int
16+ expect_arraykey ($unbounded );// ERROR: mixed </: arraykey
17+ expect_mixed ($unbounded ); // OK: mixed <: mixed
18+ }
Original file line number Diff line number Diff line change 1+ // @generated by hh_manual from manual/hack/10-types/80-case-types.md
2+ // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/
3+ case type MyCaseType = int | string | MyClass ;
4+
5+ function takes_case_type (MyCaseType $x ): void {
6+ if ($x is int ) {
7+ // $x : int
8+ } else {
9+ // $x : string | MyClass
10+ if ($x is string ) {
11+ // $x : string
12+ } else {
13+ // $x : MyClass
14+ }
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments