option
1<?php
2
3declare(strict_types=1);
4
5namespace Upside\Std;
6
7/**
8* @implements Option<null>
9*/
10final readonly class None implements Option
11{
12#[\Override]
13public function is_none(): bool
14{
15return true;
16}
17
18#[\Override]
19public function is_some(): bool
20{
21return false;
22}
23
24#[\Override]
25public function is_some_and(callable $op): bool
26{
27return false;
28}
29
30#[\Override]
31public function expect_or(\Exception $e): never
32{
33throw $e;
34}
35
36#[\Override]
37public function expect(string $msg): never
38{
39throw new \RuntimeException($msg);
40}
41
42#[\Override]
43public function unwrap(): never
44{
45throw new \RuntimeException('The option is none');
46}
47
48/**
49* @template D of callable|object|array<mixed>|string|float|int|bool
50* @phpstan-param D $default
51* @phpstan-return D
52*/
53#[\Override]
54public function unwrap_or(callable|object|iterable|string|float|int|bool $default): callable|object|iterable|string|float|int|bool
55{
56return $default;
57}
58
59/**
60* @template D of callable|object|array<mixed>|string|float|int|bool
61* @phpstan-param callable():D $op
62* @phpstan-return D
63*/
64#[\Override]
65public function unwrap_or_else(callable $op): callable|object|iterable|string|float|int|bool
66{
67return $op();
68}
69}
70