Hey, thanks for the feedback Illia and Hans.


Illia, I think that the main motivation for having the support of typing inline 
variables is that with that we can have more safety in the code.

The code can be more shielded from bugs and easier to maintain.



I don't dislike the idea of having something like const on JS, but I think that 
adding the support to add types to inline variables at least IMO is a step 
further on all the typing that was already added in the language.

We have today the ability to add types to function arguments, return and in 
class properties. So this is just another step towards adding support to 
optional typing on another place.



Types can bring more confidence when writing code, you can see that for example 
a lot of people in the JS community is now using TypeScript because of the 
types.

I love that PHP already added type support to a lot of places and adding the 
support for typing inline variables would be a game changer for the language I 
think.



Hans, you sent a good example.

I think that in this case, it should throw a TypeError, yes.

I know this would add a lot of breaking changes.

But if we want to avoid some breaking changes we for example make use of the 
declare(strict_types=1) to enforce this if this would means in less breaking 
changes.



---

Best Regards,

Wendell Adriel.

Software Engineer | Investor | Amateur Photographer | Musician | INFP​


https://wendelladriel.com










---- On Tue, 07 Feb 2023 08:34:07 +0000 Hans Henrik Bergan 
<divinit...@gmail.com> wrote ---



function f(int $value){ 
 $value="foo"; // should this be a TypeError? BC break all the things 
} 
 
On Mon, 6 Feb 2023 at 22:15, someniatko <mailto:somenia...@gmail.com> wrote: 
> 
> Hi there, 
> 
> I am not a core PHP language developer, just a regular PHP programmer, and 
> cannot speak for the whole community, so I'll just share my opinion. 
> 
> I believe a new language feature suggestion should contain not only its 
> description, but also motivation: i.e. what are we trying to achieve with 
> it. Will the development experience be worse without it, or maybe it 
> disallows some sneaky bugs to appear in your code, or maybe it acts as a 
> native documentation for your code etc. 
> 
> Personally it's hard for me to see what kind of improvement will 
> restricting a type of a variable bring. It may prevent repurposing the 
> variable with the same name for a different use somewhere down the 
> function, which can lead to bugs if a function is large enough. However, 
> for such cases I think better idea would be to introduce `const` variables 
> like in JavaScript - which can only be set once and cannot be reassigned 
> later. This way you'll also guarantee the type of the variable will be 
> preserved. 
> 
> > We can add types in a lot of places, but we still don't have a way to add 
> types to inline variables. 
> 
> > int $value = 10; 
> > $value = 'foo'; // TypeError 
> 
> Can you describe some use cases where this feature will be useful? I see 
> it's coming from statically typed / compiled languages like C++, but in 
> such languages compiler must know variable type in order to manage memory 
> properly. As PHP is an interpreted  language, it doesn't have this problem. 
> 
> Regards, 
> Illia / someniatko 
 
-- 
PHP Internals - PHP Runtime Development Mailing List 
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to