Re: Silent Types
| From: | Ben Ramsey | Date: | Wed, 04 Sep 2019 16:09:01 +0000 |
| Subject: | Re: Silent Types | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-106857@lists.php.net to get a copy of this message | ||
> Andreas Hennings wrote:
>
> In some other languages the mixed type is called "variant".
> https://en.m.wikipedia.org/wiki/Variant_type
> I mostly remember it from VisualBasic.
Union types are probably better than specifying a variant or mixed type. At present, parameters and
properties with no type specified are mixed by default. Nikita has just opened discussion on a new
union types proposal in another thread.
> Fwentish Aelondes wrote:
>
> Hello internals,
>
> Zeev's idea to bring peace to the galaxy seems like a good idea, but
> impossible to implement in practice.
>
> But it got me thinking about how one might introduce static typing
> into a dynamically typed language w/out breaking BC.
>
> And then I had this crazy idea:
>
> //int
> $i = 0;
>
> //string
> $c = 'c';
>
> //float
> $pi = 3.14;
>
> If static typing in php was *only* an opt-in kind-of-thing, would this
> work? Could the parser be built to identify 3 or 4 different keywords
> in comments and give warnings or fatal errors for type conversions of
> variables that have the type specified in the immediately preceding
> comment?
Static analyzers (like PHPStan) already honor types specified in comments above variables.
/* @param int */
$i = 0;
> Michał Brzuchalski wrote:
>
> IMO it's crazy idea and we should not change the way comments work
> especially inline comments which even aren't kept in opcache.
>
> I think better approach would be to put type in front of first variable
> declaration like:
>
> [type] $variable = $value;
I think specifying the type in front of the variable is the best option, and we have precedence for
this with typed properties in PHP 7.4.
Cheers,
Ben
Attachment: [application/pgp-signature] Message signed with OpenPGP signature.asc
Attachment: [application/pgp-signature] Message signed with OpenPGP signature.asc