No error on use of `unsafe_cast`
[hiphop-php.git] / hphp / hack / test / typecheck / xor_params.php
blobc50b26a6762e19182d918c615aca3a8daae269b7
1 <?hh // strict
2 // Copyright 2004-present Facebook. All Rights Reserved.
4 function nullthrows<T>(?T $x): T {
5 return $x as nonnull;
8 final class XorParam<Tx, Ty> {
9 private function __construct(private ?Tx $x, private ?Ty $y) {
10 invariant(
11 ($x === null) !== ($y === null),
12 "Exactly one input value must be null!",
16 public static function fromFirstValue(?Tx $x, ?Ty $y): XorParam<Tx, Ty> {
17 if ($x !== null) {
18 return new XorParam($x, null);
20 return new XorParam(null, $y);
23 public function getX(): ?Tx {
24 return $this->x;
27 public function atX(): Tx {
28 return nullthrows($this->x);
31 public function getY(): ?Ty {
32 return $this->y;
35 public function atY(): Ty {
36 return nullthrows($this->y);
39 // Returns the single non-null value
40 // TODO: is it possible to augment the type info within the generics so we
41 // can type this return value?
42 public function getValue(): mixed {
43 $z = $this->x ?? $this->y;
44 $w = nullthrows($z);
45 return $w;