3 * Copyright (c) 2014, Facebook, Inc.
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 * This file provides type information for some of hack's reactive interfaces
14 * YOU SHOULD NEVER INCLUDE THIS FILE ANYWHERE!!!
19 /* See documentation for \Traversable */
21 namespace\KeyedTraversable::class,
22 namespace\Iterator::class,
23 namespace\IteratorAggregate::class,
26 interface Traversable<+Tv> extends \Traversable<Tv> {}
28 /* See documentation for \KeyedTraversable */
30 namespace\KeyedIterable::class,
31 namespace\KeyedIterator::class,
32 \KeyedContainer::class
34 interface KeyedTraversable<+Tk, +Tv>
35 extends namespace\Traversable<Tv>, \KeyedTraversable<Tk, Tv> {}
37 /* See documentation for \Iterator */
38 interface Iterator<+Tv> extends namespace\Traversable<Tv>, \Iterator<Tv> {
40 * Return the current value at the current iterator position.
42 * @return - The current value of type `Tv`.
44 <<__Rx, __MaybeMutable>>
45 public function current(): Tv;
47 * Move the iterator position to the next element.
51 public function next(): void;
53 * Rewind the iterator position to its beginning.
55 * This rewinds back to the first element of the `Iterator`.
58 public function rewind(): void;
60 * Checks to see if the current iterator position is valid.
62 * This method is called after `rewind()` and `next()` to check if the
63 * current iterator position is valid.
65 * @return - `true` if the position is valid; `false` otherwise.
67 <<__Rx, __MaybeMutable>>
68 public function valid(): bool;
71 interface KeyedIterator<+Tk, +Tv>
73 namespace\KeyedTraversable<Tk, Tv>,
74 namespace\Iterator<Tv>,
75 \KeyedIterator<Tk, Tv> {
77 * Return the current key at the current iterator position.
79 * @return - The current key of type `Tk`.
81 <<__Rx, __MaybeMutable>>
82 public function key(): Tk;
85 /* See documentation for \AsyncIterator */
86 interface AsyncIterator<+Tv> extends \AsyncIterator<Tv> {
88 * Move the async iterator to the next `Awaitable` position.
91 * foreach ($async_iter await $async_iter->next() $value)
94 * The above is the longhand syntax for `await as $value`.
96 * @return - The next `Awaitable` in the iterator sequence.
99 public function next(): Awaitable<?(mixed, Tv)>;
102 /* See documentation for \AsyncKeyedIterator */
103 interface AsyncKeyedIterator<+Tk, +Tv>
104 extends namespace\AsyncIterator<Tv>, \AsyncKeyedIterator<Tk, Tv> {
106 * Move the async iterator to the next `Awaitable` position.
109 * foreach ($async_iter await $async_iter->next() $key=>$value)
112 * The above is the longhand syntax for `await as $key=>$value`.
114 * @return - The next `Awaitable` in the iterator sequence.
117 public function next(): Awaitable<?(Tk, Tv)>;
120 /* See documentation for \IteratorAggregate */
121 interface IteratorAggregate<+Tv>
122 extends namespace\Traversable<Tv>, \IteratorAggregate<Tv> {
124 * Returns an iterator to be used to iterate over the object's elements.
126 * @return - An `Iterator` for iteration.
128 <<__Rx, __MutableReturn, __MaybeMutable>>
129 public function getIterator(): namespace\Iterator<Tv>;
132 interface Iterable<+Tv>
133 extends namespace\IteratorAggregate<Tv>, \Iterable<Tv> {
135 * Returns an iterator that points to beginning of the current `Iterable`.
137 * @return - An `Iterator` that allows you to traverse the current `Iterable`.
139 <<__Rx, __MutableReturn, __MaybeMutable>>
140 public function getIterator(): namespace\Iterator<Tv>;
142 * Returns an `array` converted from the current `Iterable`.
144 * @return - an array converted from the current `Iterable`.
146 <<__Rx, __MaybeMutable, __PHPStdLib>>
147 public function toArray(): array;
149 * Returns an `array` with the values from the current `Iterable`.
151 * The keys in the current `Iterable` are discarded and replaced with integer
152 * indices, starting with 0.
154 * @return - an `array` containing the values from the current `Iterable`.
156 <<__Rx, __MaybeMutable>>
157 public function toValuesArray(): varray;
159 * Returns a `Vector` converted from the current `Iterable`.
161 * Any keys in the current `Iterable` are discarded and replaced with integer
162 * indices, starting with 0.
164 * @return - a `Vector` converted from the current `Iterable`.
166 <<__Rx, __MutableReturn, __MaybeMutable>>
167 /* HH_FIXME[4120]: While this violates our variance annotations, we are
168 * returning a copy of the underlying collection, so it is actually safe
170 public function toVector(): Vector<Tv>;
172 * Returns an immutable vector (`ImmVector`) converted from the current
175 * Any keys in the current `Iterable` are discarded and replaced with integer
176 * indices, starting with 0.
178 * @return - an `ImmVector` converted from the current `Iterable`.
180 <<__Rx, __MaybeMutable>>
181 public function toImmVector(): ImmVector<Tv>;
183 * Returns a `Set` converted from the current `Iterable`.
185 * Any keys in the current `Iterable` are discarded.
187 * @return - a `Set` converted from the current `Iterable`.
189 <<__Rx, __MutableReturn, __MaybeMutable>>
190 /* HH_FIXME[4120]: While this violates our variance annotations, we are
191 * returning a copy of the underlying collection, so it is actually safe.
193 public function toSet(): Set<Tv>;
195 * Returns an immutable set (`ImmSet`) converted from the current `Iterable`.
197 * Any keys in the current `Iterable` are discarded.
199 * @return - an `ImmSet` converted from the current `Iterable`.
201 <<__Rx, __MaybeMutable>>
202 public function toImmSet(): ImmSet<Tv>;
204 * Returns a lazy, access elements only when needed view of the current
207 * Normally, memory is allocated for all of the elements of the `Iterable`.
208 * With a lazy view, memory is allocated for an element only when needed or
209 * used in a calculation like in `map()` or `filter()`.
211 * @return - an `Iterable` representing the lazy view into the current
214 * @guide /hack/collections/examples
216 <<__Rx, __MutableReturn, __MaybeMutable>>
217 public function lazy(): namespace\Iterable<Tv>;
219 * Returns an `Iterable` containing the current `Iterable`'s values.
221 * Any keys are discarded.
223 * @return An `Iterable` with the values of the current `Iterable`.
225 <<__Rx, __MutableReturn, __MaybeMutable>>
226 public function values(): namespace\Iterable<Tv>;
228 * Returns an `Iterable` containing the values after an operation has been
229 * applied to each value in the current `Iterable`.
231 * Every value in the current `Iterable` is affected by a call to `map()`,
232 * unlike `filter()` where only values that meet a certain criteria are
235 * @param $fn - The callback containing the operation to apply to the
238 * @return - an `Iterable` containing the values after a user-specified
239 * operation is applied.
241 * @guide /hack/collections/examples
243 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
244 public function map<Tu>(
245 <<__AtMostRxAsFunc>>(function(Tv): Tu) $fn,
246 ): namespace\Iterable<Tu>;
248 * Returns an `Iterable` containing the values of the current `Iterable` that
249 * meet a supplied condition.
251 * Only values that meet a certain criteria are affected by a call to
252 * `filter()`, while all values are affected by a call to `map()`.
254 * @param $fn - The callback containing the condition to apply to the
257 * @return - an `Iterable` containing the values after a user-specified
258 * condition is applied.
260 * @guide /hack/collections/examples
262 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
263 public function filter(
264 <<__AtMostRxAsFunc>>(function(Tv): bool) $fn,
265 ): namespace\Iterable<Tv>;
267 * Returns an `Iterable` where each element is a `Pair` that combines the
268 * element of the current `Iterable` and the provided `Traversable`.
270 * If the number of elements of the `Iterable` are not equal to the number of
271 * elements in the `Traversable`, then only the combined elements up to and
272 * including the final element of the one with the least number of elements
275 * @param $traversable - The `Traversable` to use to combine with the
276 * elements of the current `Iterable`.
278 * @return - The `Iterable` that combines the values of the current
279 * `Itearable` with the provided `Traversable`.
281 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
282 public function zip<Tu>(
283 <<__OnlyRxIfImpl(namespace\Traversable::class)>> \Traversable<Tu> $traversable,
284 ): namespace\Iterable<Pair<Tv, Tu>>;
286 * Returns an `Iterable` containing the first `n` values of the current
289 * The returned `Iterable` will always be a proper subset of the current
292 * `$n` is 1-based. So the first element is 1, the second 2, etc.
294 * @param $n - The last element that will be included in the returned
297 * @return - An `Iterable that is a proper subset of the current `Iterable`
298 * up to `n` elements.
300 <<__Rx, __MutableReturn, __MaybeMutable>>
301 public function take(int $n): namespace\Iterable<Tv>;
303 * Returns an `Iterable` containing the values of the current `Iterable` up
304 * to but not including the first value that produces `false` when passed to
305 * the specified callback.
307 * The returned `Iterable` will always be a proper subset of the current
310 * @param $fn - The callback that is used to determine the stopping
313 * @return - An `Iterable` that is a proper subset of the current `Iterable`
314 * up until the callback returns `false`.
316 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
317 public function takeWhile(
318 <<__AtMostRxAsFunc>>(function(Tv): bool) $fn,
319 ): namespace\Iterable<Tv>;
321 * Returns an `Iterable` containing the values after the `n`-th element of the
322 * current `Iterable`.
324 * The returned `Iterable` will always be a proper subset of the current
327 * `$n` is 1-based. So the first element is 1, the second 2, etc.
329 * @param $n - The last element to be skipped; the `$n+1` element will be
330 * the first one in the returned `Iterable`.
332 * @return - An `Iterable` that is a proper subset of the current `Iterable`
333 * containing values after the specified `n`-th element.
335 <<__Rx, __MutableReturn, __MaybeMutable>>
336 public function skip(int $n): namespace\Iterable<Tv>;
338 * Returns an `Iterable` containing the values of the current `Iterable`
339 * starting after and including the first value that produces `true` when
340 * passed to the specified callback.
342 * The returned `Iterable` will always be a proper subset of the current
345 * @param $fn - The callback used to determine the starting element for the
346 * returned `Iterable`.
348 * @return - An `Iterable` that is a proper subset of the current `Iterable`
349 * starting after the callback returns `true`.
351 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
352 public function skipWhile(
353 <<__AtMostRxAsFunc>>(function(Tv): bool) $fn,
354 ): namespace\Iterable<Tv>;
356 * Returns a subset of the current `Iterable` starting from a given key up
357 * to, but not including, the element at the provided length from the
360 * `$start` is 0-based. `$len` is 1-based. So `slice(0, 2)` would return the
361 * elements at key 0 and 1.
363 * The returned `Iterable` will always be a proper subset of the current
366 * @param $start - The starting key of the current `Iterable` to begin the
367 * returned `Iterable`.
368 * @param $len - The length of the returned `Iterable`.
370 * @return - An `Iterable` that is a proper subset of the current `Iterable`
371 * starting at `$start` up to but not including the element
374 <<__Rx, __MutableReturn, __MaybeMutable>>
375 public function slice(int $start, int $len): namespace\Iterable<Tv>;
377 * Returns an `Iterable` that is the concatenation of the values of the
378 * current `Iterable` and the values of the provided `Traversable`.
380 * The values of the provided `Traversable` is concatenated to the end of the
381 * current `Iterable` to produce the returned `Iterable`.
383 * @param $traversable - The `Traversable` to concatenate to the current
386 * @return - The concatenated `Iterable`.
388 * @guide /hack/generics/constraints
390 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
391 public function concat<Tu super Tv>(
392 <<__OnlyRxIfImpl(namespace\Traversable::class)>> \Traversable<Tu> $traversable,
393 ): namespace\Iterable<Tu>;
395 * Returns the first value in the current `Iterable`.
397 * @return - The first value in the current `Iterable`, or `null` if the
398 * current `Iterable` is empty.
400 <<__Rx, __MaybeMutable>>
401 public function firstValue(): ?Tv;
403 * Returns the last value in the current `Iterable`.
405 * @return - The last value in the current `Iterable`, or `null` if the
406 * current `Iterable` is empty.
408 <<__Rx, __MaybeMutable>>
409 public function lastValue(): ?Tv;
412 interface KeyedIterable<Tk, +Tv>
414 namespace\KeyedTraversable<Tk, Tv>,
415 namespace\Iterable<Tv>,
416 \KeyedIterable<Tk, Tv> {
418 * Returns an iterator that points to beginning of the current
421 * @return - A `KeyedIterator` that allows you to traverse the current
424 <<__Rx, __MutableReturn, __MaybeMutable>>
425 public function getIterator(): namespace\KeyedIterator<Tk, Tv>;
427 * Returns an `array` with the keys from the current `KeyedIterable`.
429 * @return - an `array` containing the values from the current
432 <<__Rx, __MaybeMutable>>
433 public function toKeysArray(): varray;
435 * Returns a `Map` based on the keys and values of the current
438 * @return - a `Map` that has the keys and associated values of the current
441 <<__Rx, __MutableReturn, __MaybeMutable>>
442 /* HH_FIXME[4120]: While this violates our variance annotations, we are
443 * returning a copy of the underlying collection, so it is actually safe
445 public function toMap(): \Map<Tk, Tv>;
447 * Returns an immutable map (`ImmMap`) based on the keys and values of the
448 * current `KeyedIterable`.
450 * @return - an `ImmMap` that has the keys and associated values of the
451 * current `KeyedIterable`.
453 <<__Rx, __MaybeMutable>> /* HH_FIXME[4110] T40426954 */
454 public function toImmMap(): \ImmMap<Tk, Tv>;
456 * Returns a lazy, access elements only when needed view of the current
459 * Normally, memory is allocated for all of the elements of the
460 * `KeyedIterable`. With a lazy view, memory is allocated for an element only
461 * when needed or used in a calculation like in `map()` or `filter()`.
463 * @return - a `KeyedIterable` representing the lazy view into the current
466 * @guide /hack/collections/examples
468 <<__Rx, __MutableReturn, __MaybeMutable>>
469 public function lazy(): namespace\KeyedIterable<Tk, Tv>;
471 * Returns an `Iterable` containing the current `KeyedIterable`'s values.
473 * Any keys are discarded.
475 * @return An `Iterable` with the values of the current `KeyedIterable`.
477 <<__Rx, __MutableReturn, __MaybeMutable>>
478 public function values(): namespace\Iterable<Tv>;
480 * Returns an `Iterable` containing the current `KeyedIterable`'s keys.
482 * Any values are discarded.
484 * @return An `Iterable` with the keys of the current `KeyedIterable`.
486 <<__Rx, __MutableReturn, __MaybeMutable>>
487 public function keys(): namespace\Iterable<Tk>;
489 * Returns a `KeyedIterable` containing the values after an operation has been
490 * applied to each value in the current `KeyedIterable`.
492 * Every value in the current `KeyedIterable` is affected by a call to
493 * `map()`, unlike `filter()` where only values that meet a certain criteria
496 * @param $fn - The callback containing the operation to apply to the
497 * `KeyedIterable` values.
499 * @return - a `KeyedIterable` containing the values after a user-specified
500 * operation is applied.
502 * @guide /hack/collections/examples
504 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
505 public function map<Tu>(
506 <<__AtMostRxAsFunc>>(function(Tv): Tu) $fn,
507 ): namespace\KeyedIterable<Tk, Tu>;
509 * Returns a `KeyedIterable` containing the values after an operation has
510 * been applied to each key and value in the current `KeyedIterable`.
512 * Every key and value in the current `KeyedIterable` is affected by a call to
513 * `mapWithKey()`, unlike `filterWithKey()` where only values that meet a
514 * certain criteria are affected.
516 * @param $fn - The callback containing the operation to apply to the
517 * `KeyedIterable` keys and values.
519 * @return - a `KeyedIterable` containing the values after a user-specified
520 * operation on the current `KeyedIterable`'s keys and values is
523 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
524 public function mapWithKey<Tu>(
525 <<__AtMostRxAsFunc>>(function(Tk, Tv): Tu) $fn,
526 ): namespace\KeyedIterable<Tk, Tu>;
528 * Returns a `KeyedIterable` containing the values of the current
529 * `KeyedIterable` that meet a supplied condition.
531 * Only values that meet a certain criteria are affected by a call to
532 * `filter()`, while all values are affected by a call to `map()`.
534 * @param $fn - The callback containing the condition to apply to the
535 * `KeyedItearble` values.
537 * @return - a `KeyedIterable` containing the values after a user-specified
538 * condition is applied.
540 * @guide /hack/collections/examples
542 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
543 public function filter(
544 <<__AtMostRxAsFunc>>(function(Tv): bool) $fn,
545 ): namespace\KeyedIterable<Tk, Tv>;
547 * Returns a `KeyedIterable` containing the values of the current
548 * `KeyedIterable` that meet a supplied condition applied to its keys and
551 * Only keys and values that meet a certain criteria are affected by a call to
552 * `filterWithKey()`, while all values are affected by a call to
555 * @param $fn - The callback containing the condition to apply to the
556 * `KeyedIterable` keys and values.
558 * @return - a `KeyedIterable` containing the values after a user-specified
559 * condition is applied to the keys and values of the current
563 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
564 public function filterWithKey(
565 <<__AtMostRxAsFunc>>(function(Tk, Tv): bool) $fn,
566 ): namespace\KeyedIterable<Tk, Tv>;
568 * Returns a `KeyedIterable` where each element is a `Pair` that combines the
569 * element of the current `KeyedIterable` and the provided `Traversable`.
571 * If the number of elements of the `KeyedIterable` are not equal to the
572 * number of elements in the `Traversable`, then only the combined elements
573 * up to and including the final element of the one with the least number of
574 * elements is included.
576 * @param $traversable - The `Traversable` to use to combine with the
577 * elements of the current `KeyedIterable`.
579 * @return - The `KeyedIterable` that combines the values of the current
580 * `KeyedItearable` with the provided `Traversable`.
582 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
583 public function zip<Tu>(
584 <<__OnlyRxIfImpl(namespace\Traversable::class)>> \Traversable<Tu> $traversable,
585 ): namespace\KeyedIterable<Tk, Pair<Tv, Tu>>;
587 * Returns a `KeyedIterable` containing the first `n` values of the current
590 * The returned `KeyedIterable` will always be a proper subset of the current
593 * `$n` is 1-based. So the first element is 1, the second 2, etc.
595 * @param $n - The last element that will be included in the returned
598 * @return - A `KeyedIterable that is a proper subset of the current
599 * `KeyedIterable` up to `n` elements.
601 <<__Rx, __MutableReturn, __MaybeMutable>>
602 public function take(int $n): namespace\KeyedIterable<Tk, Tv>;
604 * Returns a `KeyedIterable` containing the values of the current
605 * `KeyedIterable` up to but not including the first value that produces
606 * `false` when passed to the specified callback.
608 * The returned `KeyedIterable` will always be a proper subset of the current
611 * @param $fn - The callback that is used to determine the stopping
614 * @return - A `KeyedIterable` that is a proper subset of the current
615 * `KeyedIterable` up until the callback returns `false`.
617 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
618 public function takeWhile(
619 <<__AtMostRxAsFunc>>(function(Tv): bool) $fn,
620 ): namespace\KeyedIterable<Tk, Tv>;
622 * Returns a `KeyedIterable` containing the values after the `n`-th element
623 * of the current `KeyedIterable`.
625 * The returned `KeyedIterable` will always be a proper subset of the current
628 * `$n` is 1-based. So the first element is 1, the second 2, etc.
630 * @param $n - The last element to be skipped; the `$n+1` element will be
631 * the first one in the returned `KeyedIterable`.
633 * @return - A `KeyedIterable` that is a proper subset of the current
634 * `KeyedIterable` containing values after the specified `n`-th
637 <<__Rx, __MutableReturn, __MaybeMutable>>
638 public function skip(int $n): namespace\KeyedIterable<Tk, Tv>;
640 * Returns a `KeyedIterable` containing the values of the current
641 * `KeyedIterable` starting after and including the first value that produces
642 * `true` when passed to the specified callback.
644 * The returned `KeyedIterable` will always be a proper subset of the current
647 * @param $fn - The callback used to determine the starting element for the
648 * returned `KeyedIterable`.
650 * @return - A `KeyedIterable` that is a proper subset of the current
651 * `KeyedIterable` starting after the callback returns `true`.
653 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
654 public function skipWhile(
655 <<__AtMostRxAsFunc>>(function(Tv): bool) $fn,
656 ): namespace\KeyedIterable<Tk, Tv>;
658 * Returns a subset of the current `KeyedIterable` starting from a given key
659 * up to, but not including, the element at the provided length from the
662 * `$start` is 0-based. `$len` is 1-based. So `slice(0, 2)` would return the
663 * elements at key 0 and 1.
665 * The returned `KeyedIterable` will always be a proper subset of the current
668 * @param $start - The starting key of the current `KeyedIterable` to begin
669 * the returned `KeyedIterable`.
670 * @param $len - The length of the returned `KeyedIterable`.
672 * @return - A `KeyedIterable` that is a proper subset of the current
673 * `KeyedIterable` starting at `$start` up to but not including the
674 * element `$start + $len`.
676 <<__Rx, __MutableReturn, __MaybeMutable>>
677 public function slice(int $start, int $len): namespace\KeyedIterable<Tk, Tv>;
679 * Returns an `Iterable` that is the concatenation of the values of the
680 * current `KeyedIterable` and the values of the provided `Traversable`.
682 * The values of the provided `Traversable` is concatenated to the end of the
683 * current `KeyedIterable` to produce the returned `Iterable`.
685 * @param $traversable - The `Traversable` to concatenate to the current
688 * @return - The concatenated `Iterable`.
690 * @guide /hack/generics/constraints
692 <<__Rx, __AtMostRxAsArgs, __MutableReturn, __MaybeMutable>>
693 public function concat<Tu super Tv>(
694 <<__OnlyRxIfImpl(namespace\Traversable::class)>> \Traversable<Tu> $traversable,
695 ): namespace\Iterable<Tu>;
697 * Returns the first value in the current `KeyedIterable`.
699 * @return - The first value in the current `KeyedIterable`, or `null` if the
700 * current `KeyedIterable` is empty.
702 <<__Rx, __MaybeMutable>>
703 public function firstValue(): ?Tv;
705 * Returns the first key in the current `KeyedIterable`.
707 * @return - The first key in the current `KeyedIterable`, or `null` if the
708 * current `KeyedIterable` is empty.
710 <<__Rx, __MaybeMutable>>
711 public function firstKey(): ?Tk;
713 * Returns the last value in the current `KeyedIterable`.
715 * @return - The last value in the current `KeyedIterable`, or `null` if the
716 * current `KeyedIterable` is empty.
718 <<__Rx, __MaybeMutable>>
719 public function lastValue(): ?Tv;
721 * Returns the last key in the current `KeyedIterable`.
723 * @return - The last key in the current `KeyedIterable`, or `null` if the
724 * current `KeyedIterable` is empty.
726 <<__Rx, __MaybeMutable>>
727 public function lastKey(): ?Tk;
730 interface Countable extends \Countable {
731 <<__Rx, __MaybeMutable>>
732 public function count(): int;