diff --git a/README.md b/README.md index f28649f..22d27d6 100644 --- a/README.md +++ b/README.md @@ -224,7 +224,7 @@ All Result types (both Ok and Err) implement these methods: - `inspectErr(callable $fn): Result` - Calls a function with the error value if Err #### Pattern Matching -- `match(callable $okFn, callable $errFn): mixed` - Pattern match on the Result +- `match(callable $ok, callable $err): mixed` - Pattern match on the Result ## License diff --git a/src/Err.php b/src/Err.php index 968d589..6e08215 100644 --- a/src/Err.php +++ b/src/Err.php @@ -147,15 +147,15 @@ public function mapOr(mixed $default, callable $fn): mixed * @template U * @template V * - * @param callable(): U $default_fn + * @param callable(): U $defaultFn * @param callable(never): V $fn * * @return U */ #[Override] - public function mapOrElse(callable $default_fn, callable $fn): mixed + public function mapOrElse(callable $defaultFn, callable $fn): mixed { - return $default_fn(); + return $defaultFn(); } /** @@ -189,8 +189,8 @@ public function orElse(callable $fn): Result } #[Override] - public function match(callable $ok_fn, callable $err_fn): mixed + public function match(callable $ok, callable $err): mixed { - return $err_fn($this->value); + return $err($this->value); } } diff --git a/src/Ok.php b/src/Ok.php index fea4b7b..53565c0 100644 --- a/src/Ok.php +++ b/src/Ok.php @@ -146,13 +146,13 @@ public function mapOr(mixed $default, callable $fn): mixed * @template U * @template V * - * @param callable(): V $default_fn + * @param callable(): V $defaultFn * @param callable(T): U $fn * * @return U */ #[Override] - public function mapOrElse(callable $default_fn, callable $fn): mixed + public function mapOrElse(callable $defaultFn, callable $fn): mixed { return $fn($this->value); } @@ -188,8 +188,8 @@ public function orElse(callable $fn): Result } #[Override] - public function match(callable $ok_fn, callable $err_fn): mixed + public function match(callable $ok, callable $err): mixed { - return $ok_fn($this->value); + return $ok($this->value); } } diff --git a/src/Result.php b/src/Result.php index 67f2fd4..c2752cc 100644 --- a/src/Result.php +++ b/src/Result.php @@ -146,12 +146,12 @@ public function mapOr(mixed $default, callable $fn): mixed; * * @template U * - * @param callable(): U $default_fn + * @param callable(): U $defaultFn * @param callable(T): U $fn * * @return U */ - public function mapOrElse(callable $default_fn, callable $fn): mixed; + public function mapOrElse(callable $defaultFn, callable $fn): mixed; /** * 成功の場合は第2の結果を返し、失敗の場合は最初のエラーを返します. @@ -206,15 +206,15 @@ public function or(self $res): self; public function orElse(callable $fn): self; /** - * 成功の場合はok_fnを、失敗の場合はerr_fnを適用します. + * 成功の場合はokを、失敗の場合はerrを適用します. * * @template U * @template V * - * @param callable(T): U $ok_fn 成功値に適用する関数 - * @param callable(E): V $err_fn エラー値に適用する関数 + * @param callable(T): U $ok 成功値に適用する関数 + * @param callable(E): V $err エラー値に適用する関数 * * @return U|V 適用された関数の結果 */ - public function match(callable $ok_fn, callable $err_fn): mixed; + public function match(callable $ok, callable $err): mixed; } diff --git a/tests/ErrTest.php b/tests/ErrTest.php index 30b5aa9..e53ba0e 100644 --- a/tests/ErrTest.php +++ b/tests/ErrTest.php @@ -276,6 +276,25 @@ public function match_withDifferentReturnTypes_returns_err_branch(): void $this->assertSame(['status' => 'error', 'code' => 404], $result); } + #[Test] + public function match_supportsNamedArguments(): void + { + $err = new Err('error'); + $result = $err->match( + ok: fn ($value) => "Success: $value", + err: fn ($error) => "Error: $error", + ); + $this->assertSame('Error: error', $result); + } + + #[Test] + public function mapOrElse_supportsNamedArguments(): void + { + $err = new Err('error'); + $result = $err->mapOrElse(defaultFn: fn () => 100, fn: fn ($x) => $x * 2); + $this->assertSame(100, $result); + } + #[Test] public function err_withNullValue_handles_null(): void { diff --git a/tests/OkTest.php b/tests/OkTest.php index 309282c..db780ab 100644 --- a/tests/OkTest.php +++ b/tests/OkTest.php @@ -267,6 +267,25 @@ public function match_withDifferentReturnTypes_returns_ok_branch(): void $this->assertSame(5, $result); } + #[Test] + public function match_supportsNamedArguments(): void + { + $ok = new Ok(42); + $result = $ok->match( + ok: fn ($value) => "Success: $value", + err: fn ($error) => "Error: $error", + ); + $this->assertSame('Success: 42', $result); + } + + #[Test] + public function mapOrElse_supportsNamedArguments(): void + { + $ok = new Ok(10); + $result = $ok->mapOrElse(defaultFn: fn () => 100, fn: fn ($x) => $x * 2); + $this->assertSame(20, $result); + } + #[Test] public function ok_withNullValue_handles_null(): void {