On 21/10/2019 21:18, Mike Schinkel wrote:
But to follow up to clarify what I was thinking consider the
following/ (hypothetical) /PHP syntax. Note that the return value
has two comma separated values, and the assignment can accept
those multiple values into two comma separated variables in the
assignment:
function read_file(string $filepath):string,PHP\Error {
$content = file_get_contents( $filepath ); $error = false ===
$content ? new PHP\Error( "failed to open '%s'", $filepath ) :
null; return $content, $error; }$content, $err= read_file(
__DIR__ . '/myfile.txt' ); if (!is_null( $err ) ) {
error_log($err->message); }
Ah, I see. Yes, multiple return values would be nice sometimes.
Note that you can get a lot closer to this in current versions of PHP
than your example, because you can use the [...] syntax for both
creating and destructuring arrays:
function read_file(string $filepath):array {
$content = file_get_contents( $filepath );
$error = false === $content
? new PHP\Error( "failed to open '%s'", $filepath )
: null;
return [ $content, $error ];
}
[ $content, $err ] = read_file( __DIR__ . '/myfile.txt' );
if (!is_null( $err ) ) {
error_log($err->message);
}
With the first example PHP could type check at load time vs.
having to wait for coverage of this code at runtime, and `:array`
type hint in the latter is not as good as the `:string, PHP\Error`
type hint of the former.
The ability to typehint multiple return values like that would be indeed
be useful, but I'm not sure it would need to reserve that syntax. One
way it could be achieved is using a "tuple" declaration, like in Hack:
https://docs.hhvm.com/hack/built-in-types/tuples
In general, grouping things with commas and no parentheses is always
going to *look* ambiguous IMO, even if you can limit where it's used to
not be *technically* ambiguous.
Regards,
--
Rowan Tommins (né Collins)
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php