Move io_tests to folly/io/async/test
[hiphop-php.git] / hphp / hack / test / typecheck / immvector.php
blob8216fa610d20e43a07a1c038948f534a06a2227a
1 <?hh
2 /**
3 * Copyright (c) 2014, Facebook, Inc.
4 * All rights reserved.
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the "hack" directory of this source tree.
12 // Test that the typechecker can correctly handle ImmVector.
14 // Helpers
16 function h1(): ImmVector<string> {
17 return ImmVector {'hello', 'world'};
20 function h2(int $k) : void {}
22 function h3(string $v) : void {}
24 // Test that ImmVector {} : ImmVector<int>
26 function emptyfv(): ImmVector<int> {
27 return ImmVector {};
30 // Test array-like access.
32 function simple(ImmVector<int> $fv) : int {
33 return $fv[0];
36 // Nested FVs.
38 function nested(): ImmVector<ImmVector<string>> {
39 return ImmVector {h1(), ImmVector {'a', 'b', 'c'}, ImmVector {}};
42 // Foreach over a FV.
44 function sum(): int {
45 $v = ImmVector {1, 2, 3, 4, 5};
46 $s = 0;
47 foreach ($v as $k) {
48 $s += $k;
50 return $s;
53 // Foreach over a FV with both key and value.
55 function feach(ImmVector<string> $vec) : void {
56 foreach ($vec as $k => $v) {
57 h2($k);
58 h3($v);
62 // List syntax
64 function lsyntax(ImmVector<int> $fv) : void {
65 list($a, $b) = $fv;
68 // Generic FVs are covariant
70 class A {
73 class B extends A {
76 function covariance(ImmVector<B> $fvb) : ImmVector<A> {
77 return $fvb;