Re: RFC Proposal - Types for Inline Variables
| From: | Alexandru Pătrănescu | Date: | Wed, 08 Feb 2023 15:53:45 +0000 |
| Subject: | Re: RFC Proposal - Types for Inline Variables | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119488@lists.php.net to get a copy of this message | ||
On Tue, Feb 7, 2023 at 10:21 PM Mark Baker <mark@demon-angel.eu> wrote:
> On 07/02/2023 20:53, Olle Härstedt wrote:
> > 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.
>
> The big difference is that the current checking for function arguments
> is only necessary when a function is called; but that checking for
> local-scoped variables would be required on every assignment to a
> variable, or operation that can change a variable value; and that
> becomes more problematic with the potential need for union types.
>
>
Hi Mark,
I saw that you and Olle were discussing that type checking should be
similar with functions typed parameters .
But in reality, it should be implemented more like typed properties.
Actually that's already possible with something like https://3v4l.org/T6GFS
function &declare_int(int $value) {
static $references = [];
$valueWrapper = new class(5) {
public function __construct(public int $value) {
}
};
$references[] = $valueWrapper;
return $valueWrapper->value;
}
$intVariable = &declare_int(0);
$intVariable = 42; // works
$intVariable = 'test'; // fails
Even the PHP_INT_MAX works (it fails).
To give credit, I saw it discussed few years ago on twitter and here you
can see a nice playground implementation: https://github.com/azjezz/typed/
I don't see a bug technical performance downside, other than the wrapper
class references that you need to have a management for.
I think an implementation in C would be not less performant than how
properties types are checked.
Regards,
Alex