Dan Ackroyd [email protected] wrote:
I'm guessing you don't actually have ths function getIntOrNull() in your code-base? To help me understand where this would be useful, could you provide some 'real-world' code where this would be useful?
Hello, thanks for digging in :)
Here's a "real-world" situation I happened to come across (NOTE: simplified for sanity):
vendor/bar/api/src/Dest.php (code we don't own):
<?php
namespace Bar\Api;
interface Dest {
public function send(?string $reference, string $label, );
}
vendor/foo/api/src/Source.php (code we can't modify either):
<?php
namespace Foo\Api;
interface Source {
public function getNumber(): ?int;
public function getName(): string;
}
src/Service.php (our code):
<?php
declare(strict_types=1);
namespace App;
use Bar\Api\Dest;
use Foo\Api\Source;
class Service {
public function action(Dest $dest, Source $source) {
$dest->send( $source->getNumber(),
$source->getName(), /* ... */);
}
}
As you can see, it's actually pretty close to the RFC example; I'm not sure it's worth to add complexity...
But please can you post the actual use-case you need this for. Or possibly update the RFC with that example. Currently, imo, the RFC is just presenting an alternative syntax for something that is not 100% needed.
You have a point, but all the current alternatives I can think of (namely: use a ternary (or an if), possibly with a temporary variable; write (and autoload) custom casting functions; or give up on strict typing) suffer one or more of the following downsides:
- increased risk of mistakes
- verbosity / more maintenance
- performance overhead
I do kind of like the idea in the RFC, but I think it needs a better argument for it.
For your other points, I find that Claude Pache has given pretty good arguments in the meantime (thanks!) =) (By the way, I also thought about say, "nullable_intval()", "nullable_strval()" etc. but we're missing "arrayval()" and "objectval()"...)
Best regards,
--
Guilliam Xavier