Re: RFC Proposal - Types for Inline Variables
| From: | Sergii Shymko | Date: | Wed, 08 Feb 2023 00:13:08 +0000 |
| Subject: | Re: RFC Proposal - Types for Inline Variables | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119483@lists.php.net to get a copy of this message | ||
________________________________
From: Olle Härstedt <olleharstedt@gmail.com>
Sent: Tuesday, February 7, 2023 11:53 AM
To: Rowan Tommins <rowan.collins@gmail.com>
Cc: PHP Internals <internals@lists.php.net>
Subject: Re: [PHP-DEV] RFC Proposal - Types for Inline Variables
2023-02-07 17:21 GMT+01:00, Rowan Tommins <rowan.collins@gmail.com>:
> On 07/02/2023 14:07, Olle Härstedt wrote:
>> It should perhaps be mentioned that analyzers can use type annotations
>> during their process, instead of the more clunky /** @var string */ or
>> similar you have to use today for local variables.
>
> This sounds like you're reaching for Python's approach, where the
> language "supports" the type annotation syntax, but doesn't actually do
> anything with it.
No not really. I'd expect it behave similar to function argument
type-hinting in PHP, that is, runtime checks, but where the notation
can be used by external tools.
Olle
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php
Hi,
Wanted to add my two cents on potential usefulness of typed variables.
I think, typed variables would be a necessary prerequisite for typed arrays and eventually generics.
If my understanding is correct, the primary concern there is performance overhead of iterating items
for type checking.
With typed variables, type checking is done at assignment and passing typed array around becomes
lightweight.
It would be enough to type check against a known array item type without iterating over items.
For example:
function do_something(array<string> $strings) {
...
}
array<string> $names = ['John', 'Jane', 'Jake']; // items are
type checked similar to variadic argument unpacking
sort($names);
do_something($names); // lightweight type check as for scalar types
(I'm a recent mailing list subscriber and have no RFC voting rights whatsoever).
+1 to the proposal as it would nicely complete the type system of PHP.
It's pretty odd to have typed arguments and properties, but not variables.
Moving one step closer to retiring PHPDocs for good is very good as well.