[VOTE][RFC] Scalar Type Hinting with Cast

php.internals

Andrew Faulds

11 years ago
Good evening, I’ve given up on my plan B, so I’m putting this RFC, finally, to a vote. I would urge you to vote in favour. It is not going to please everyone, it is after all a compromise proposal. However, I have tried my best to strike a balance between complete weak typing and strict typing. If this passes, we will finally have userland type specifiers for scalar types. It’s not perfect, but I’d argue it’s far better than nothing. Voting starts today, 2014-09-14, and ends in a week’s time, 2014-09-21. Thanks! https://wiki.php.net/rfc/scalar_type_hinting_with_cast#vote
-- Andrea Faulds http://ajf.me/

Andrew Faulds

11 years ago
On 14 Sep 2014, at 00:30, Andrea Faulds <ajf@ajf.me> wrote:
> Good evening, > > I’ve given up on my plan B, so I’m putting this RFC, finally, to a vote. > > I would urge you to vote in favour. It is not going to please everyone, it is after all a compromise proposal. However, I have tried my best to strike a balance between complete weak typing and strict typing. If this passes, we will finally have userland type specifiers for scalar types. It’s not perfect, but I’d argue it’s far better than nothing. > > Voting starts today, 2014-09-14, and ends in a week’s time, 2014-09-21. > > Thanks! > > https://wiki.php.net/rfc/scalar_type_hinting_with_cast#vote
I have postponed the vote after further thought. It’s been a while since this was discussed on the list. However, it is my intent to open voting properly at some point soon, if all goes to plan.
-- Andrea Faulds http://ajf.me/

Andrew Faulds

11 years ago
On 14 Sep 2014, at 00:35, Andrea Faulds <ajf@ajf.me> wrote:
> I have postponed the vote after further thought. It’s been a while since this was discussed on the list. > > However, it is my intent to open voting properly at some point soon, if all goes to plan.
I have withdrawn the RFC. I do not feel anymore that it is the way forward with scalar type hints in PHP.
-- Andrea Faulds http://ajf.me/

Pierre Joye

11 years ago
hi Andrea, On Mon, Sep 15, 2014 at 3:07 PM, Andrea Faulds <ajf@ajf.me> wrote:
> > On 14 Sep 2014, at 00:35, Andrea Faulds <ajf@ajf.me> wrote: > >> I have postponed the vote after further thought. It’s been a while since this was discussed on the list. >> >> However, it is my intent to open voting properly at some point soon, if all goes to plan. > > I have withdrawn the RFC. I do not feel anymore that it is the way forward with scalar type hints in PHP.
Sigh. Here we go again. I do not think we will ever reach an agreement on the list before the RFC goes to the voting phase. That's why I think the RFC should contain the most requested options and go with them. Any further discussions will simply create more confusions and yet again move to a dead end, we all lose. It would be great if there are two sets of options: - One about having scalar typed arguements, hinted or not, simple yes/no, so we at least agree on having the feature - a set of options about how should it work . strict . what the rfc describes in its last state . matching, if possible at all, current casting, following what (int)$foo and co or whatever is considered as "the common casting php way" I am not saying these are the only options or only the correct ones, there may be other. I only would like to get this sorted out sooner rather than later. If we agree on the feature itself and we do not see a clear majority on one point or another, let take the top two options and try to figure what has to be changed to get a majority (2nd phase vote). To be more clear: We must get a decision here. I am convinced that this is a top requested feature, since very long. Delay it forever only because we are unable to find a concensus sounds pretty bad. Let get us together to finally have this feature implemented, in the best possible way. Cheers,
-- Pierre @pierrejoye | http://www.libgd.org

Patrick Schaaf

11 years ago
Hello, thinking a bit again on the whole topic, which I thought I was pretty much completely opposed to because of the arbitrary-new-set-of-rules issue in die validation details, I noticed that from my writing PHP code, there _is_ a single thing that I really _would_ like to be able to express as a type hint. It would be a different kind of compromise. I wonder whether that would please enough of the mysterious group of people who want such a feature somehow. Idea: have a "scalar" type hint. Have it admit null, int, float etc, but not array, not object, and maybe not resource. No type juggling, casting, or anything else that messes with the passed in zval - just a check that it is scalar. I come to this idea because in practise, what I often find myself checking inside functions, is "if (is_array($arg) || is_object($arg)) fail; - because these kinds of arguments tend to immediately mess up when treated like scalars in further expressions in the function - while the usual type juggling for scalar types works out fine in most other cases. What do you think? best regards Patrick

Stas Malyshev

11 years ago
Hi!
> - One about having scalar typed arguements, hinted or not, simple > yes/no, so we at least agree on having the feature
I'm not sure how that would work. We agree on having what feature? Having coercive typing and having strict typing are two different options, working in different way and implying different coding approaches. And then there's also a third option which is a "compromise" approach where it is sometimes strict and sometimes not depending on a matrix of choices, and different matrices would work in different way. If I agree, for example, that coercive model is helpful - that doesn't mean I want complete strict typing, and I certainly would prefer none for now - in hope I would succeed convincing the majority in the future in the merits of coercive typing approach - than having strict model implemented now and closing the door for the coercive one forever. So I don't see how "at least agree" without specifying what we agreeing on works out.
> To be more clear: We must get a decision here. I am convinced that > this is a top requested feature, since very long. Delay it forever
Is there any data to support this conviction that this is not merely requested now and then, but it is a *top* requested feature since very long? Note that none of the languages most close to PHP have it. Also, I did a poll one on stackoverflow: http://programmers.stackexchange.com/questions/27564/what-features-would-you-like-to-have-in-php and it's not on top at all, though it is among the requested ones. However, having no consensus on it, saying "let's get together and make something" does not look like a good idea to me, because there are opportunity costs to prematurely having "something" that is not satisfactory.
-- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/

Pierre Joye

11 years ago
On Sep 22, 2014 11:06 PM, "Stas Malyshev" <smalyshev@sugarcrm.com> wrote:
> > Hi! > > > - One about having scalar typed arguements, hinted or not, simple > > yes/no, so we at least agree on having the feature > > I'm not sure how that would work. We agree on having what feature? > Having coercive typing and having strict typing are two different > options, working in different way and implying different coding > approaches. And then there's also a third option which is a "compromise" > approach where it is sometimes strict and sometimes not depending on a > matrix of choices, and different matrices would work in different way. > If I agree, for example, that coercive model is helpful - that doesn't > mean I want complete strict typing, and I certainly would prefer none > for now - in hope I would succeed convincing the majority in the future > in the merits of coercive typing approach - than having strict model > implemented now and closing the door for the coercive one forever. So I > don't see how "at least agree" without specifying what we agreeing on > works out. > > > To be more clear: We must get a decision here. I am convinced that > > this is a top requested feature, since very long. Delay it forever > > Is there any data to support this conviction that this is not merely > requested now and then, but it is a *top* requested feature since very > long? Note that none of the languages most close to PHP have it.
Most? Python has it, java too, ruby and perl not. We can continue to list those having them or not, not sure that brings much to this discussion.
> Also, I did a poll one on stackoverflow: > >
http://programmers.stackexchange.com/questions/27564/what-features-would-you-like-to-have-in-php
> > and it's not on top at all, though it is among the requested ones. > However, having no consensus on it, saying "let's get together and make > something" does not look like a good idea to me, because there are > opportunity costs to prematurely having "something" that is not > satisfactory.
Everyone I asked wants it, be at conferences, UGs, or devolopers of one of the top tools or framework out there. There was also http://www.php-vote.com/browse/latest showing in the top requests. In any case, this endless discussion leads nowhere and we are not going to have anything done using such debates and arguing. Cheers,
-- Pierre

Stas Malyshev

11 years ago
Hi!
> Most? > > Python has it, java too, ruby and perl not. We can continue to list > those having them or not, not sure that brings much to this discussion.
Only Python 3 has it, and I'm not sure bringing up Python 3, given its known adoption issues, is the best example how we could plan PHP 7. In any case, python 3 seems to be rather an exception than the rule amongst scripting/dynamic languages, as it seems to me. That brings to this discussion an argument that if people that have no PHP baggage, no BC issues related to existing PHP code, etc. but working in roughly the same space as PHP under roughly the same paradigms chose to forego strict typing in that context - maybe it's not such a stupid idea. Maybe we can learn from their experience too. We've often borrowed from other languages, so consider it a case of negative borrowing :)
> Everyone I asked wants it, be at conferences, UGs, or devolopers of one > of the top tools or framework out there. There was > also http://www.php-vote.com/browse/latest showing in the top requests.
This site has whopping 44 votes for "type hints with scalar types" and 52 for "100% unicode". It is rather hard for me to take it as a data point that it is requested for everybody.
> In any case, this endless discussion leads nowhere and we are not going > to have anything done using such debates and arguing.
OK, how do you propose to make anything done? Just ram through whatever gets through the vote at random moment of time, whatever the disagreements and issues are? Use some process which would allow us to find a better solution? Which process?
-- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/

Alain Williams

11 years ago
On Sun, Sep 14, 2014 at 12:30:40AM +0100, Andrea Faulds wrote:
> Good evening, > > I’ve given up on my plan B, so I’m putting this RFC, finally, to a vote. > > I would urge you to vote in favour. It is not going to please everyone, it is after all a compromise proposal. However, I have tried my best to strike a balance between complete weak typing and strict typing. If this passes, we will finally have userland type specifiers for scalar types. It’s not perfect, but I’d argue it’s far better than nothing. > > Voting starts today, 2014-09-14, and ends in a week’s time, 2014-09-21. > > Thanks! > > https://wiki.php.net/rfc/scalar_type_hinting_with_cast#vote
You give an option for float to int casting to truncate. You give an example of truncating a positive float in that it rounds down towards 0 - ie 1.5 becomes 1. What happens with negaitve numbers ? Do you round towards zero or round in a negative direction ? So, should -1.5 become -1 or -2 ? Or should it depend on the floating point unit on the machine (not all do the same) ? Currently PHP (CentOS on an AMD CPU) a cast to int converts -1.5 to -1.
-- Alain Williams Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer. +44 (0) 787 668 0256 http://www.phcomp.co.uk/ Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php #include <std_disclaimer.h>

Andrew Faulds

11 years ago
On 14 Sep 2014, at 01:07, Alain Williams <addw@phcomp.co.uk> wrote:
> You give an option for float to int casting to truncate.
Where? oa-res-27-90:php-src ajf$ sapi/cli/php -r 'function foobar(int $a) { var_dump($a); } foobar(1.5);' Catchable fatal error: Argument 1 passed to foobar() must be of the type int, float given, called in Command line code on line 1 and defined in Command line code on line 1 If you mean under possible changes, it’s not a change that’s been made. It’d only be worth discussing within the context of making it truncate or round.
-- Andrea Faulds http://ajf.me/

Stas Malyshev

11 years ago
Hi!
> oa-res-27-90:php-src ajf$ sapi/cli/php -r 'function foobar(int $a) { > var_dump($a); } foobar(1.5);' > > Catchable fatal error: Argument 1 passed to foobar() must be of the > type int, float given, called in Command line code on line 1 and > defined in Command line code on line 1
Calling this "scalar type hinting with cast" when it doesn't actually cast in wide variety of cases sounds a bit misleading to me. It should be called "scalar type checking with restricted casting" or something like that. I would also ask to clean up an RFC a bit and to clarify which of the proposed choices and "possible changes" are actually being voted for when somebody votes "yes". In any case, I call everybody to vote against this RFC in this form, since it introduces inconsistency between internal and user functions and makes PHP have two different arbitrary sets of rules for implicit conversions, and this can not be good for PHP.
-- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/

Andrew Faulds

11 years ago
> On 14 Sep 2014, at 07:32, Stas Malyshev <smalyshev@sugarcrm.com> wrote: > > Calling this "scalar type hinting with cast" when it doesn't actually > cast in wide variety of cases sounds a bit misleading to me. It should > be called "scalar type checking with restricted casting" or something > like that.
I'd agree it's not the best name. This is mainly a decision to make when this is documented or added to the language spec. If we change this thing's name, we should perhaps stop calling typed parameters 'type hints'.
> I would also ask to clean up an RFC a bit and to clarify which of the > proposed choices and "possible changes" are actually being voted for > when somebody votes "yes".
The vote would be for the RFC as it is. Possible changes are things in the RFC I was uncertain on. I might hold votes on some of them, actually.
> In any case, I call everybody to vote against this RFC in this form, > since it introduces inconsistency between internal and user functions > and makes PHP have two different arbitrary sets of rules for implicit > conversions, and this can not be good for PHP.
I had abandoned the RFC for this reason. However I gave up trying to improve zpp and make it consistent with user land hints, or adding user land hints which match zpp. The problem is that we *can't* be consistent. Internal functions already live in this completely different world from user land functions. Nullability is handled differently. They use implicit casts in places as if they were user land functions with no parameter types at all. In other places they have strongly typed parameters. If they are passed invalid data, they usually just return NULL and emit a warning, though this depends on the function. User land functions, by contrast, will emit a recoverable error if the wrong type is passed. To make these two consistent would require massive backwards-compatibility breaks. If we're just talking about casting, then this proposal does not alter casting behaviour, in fact I've been very careful not to touch it. It does, however, only accept a subset of the values zpp usually does. This is because it is a compromise between strict and weak parameter typing. While I understand you might like it to be fully weak like zpp, many userland developers are in favour of something stricter, thus the compromise. Unlike zpp's rules, at least these rules are clearly specified and prevent data loss. Thanks!
-- Andrea Faulds http://ajf.me/

Zeev Suraski

11 years ago
> -----Original Message----- > From: Andrea Faulds [mailto:ajf@ajf.me] > Sent: Sunday, September 14, 2014 3:08 PM > To: Stas Malyshev > Cc: internals@lists.php.net > Subject: Re: [PHP-DEV] [VOTE][RFC] Scalar Type Hinting with Cast > > > The problem is that we *can't* be consistent. Internal functions already
live in
> this completely different world from user land functions. Nullability is
handled
> differently. They use implicit casts in places as if they were user land
functions
> with no parameter types at all. In other places they have strongly typed > parameters. If they are passed invalid data, they usually just return
NULL and
> emit a warning, though this depends on the function. User land
functions, by
> contrast, will emit a recoverable error if the wrong type is passed. To
make
> these two consistent would require massive backwards-compatibility
breaks. I honestly don't see why we can't be consistent for the simple types, or at least strive to be as consistent as possible as opposed to introducing a complete parallel universe for userland functions, that's inconsistent with the entirety of the rest of PHP. We don't have to be consistent with ALL internal functions, which obviously have the option of doing custom checks and return failures not only if you fail to, say, pass on a string - but also if that string is not of a special format. But then, you have the option of doing that also in userland functions using custom code. The question is not the customized cases - it's the standard behavioral cases - comparing zpp rules for scalars and the newly-introduced rules for userland scalars. We could definitely strive to be consistent; Introduce some changes to the implicit casting rules for internal functions and implement the very same rules for this brand new userland functions. I'm definitely against this RFC in its current form. Zeev

Andrew Faulds

11 years ago
On 14 Sep 2014, at 13:17, Zeev Suraski <zeev@zend.com> wrote:
> I honestly don't see why we can't be consistent for the simple types, or > at least strive to be as consistent as possible as opposed to introducing > a complete parallel universe for userland functions, that's inconsistent > with the entirety of the rest of PHP. We don't have to be consistent with > ALL internal functions, which obviously have the option of doing custom > checks and return failures not only if you fail to, say, pass on a string > - but also if that string is not of a special format. But then, you have > the option of doing that also in userland functions using custom code. > The question is not the customized cases - it's the standard behavioral > cases - comparing zpp rules for scalars and the newly-introduced rules for > userland scalars.
OK, we could go for exactly what zpp does already. However, then we don’t have a compromise. We are siding with what you want, but most userland developers would prefer a strict system. While this would please you and perhaps some other folk on internals, and sure, it’d be consistent, it would not be popular with the wider PHP community. Similarly, we could go for a fully strict approach, which might please some userland developers, but wouldn’t please you. I don’t want to go down this route. I’d prefer we compromise and keep PHP’s weakly-typed nature (an int can be accepted as a float or a string), but make it stricter (no data loss), thus arriving at a middle way.
> We could definitely strive to be consistent; Introduce some changes to > the implicit casting rules for internal functions and implement the very > same rules for this brand new userland functions. I'm definitely against > this RFC in its current form.
I wanted to do that, but it has its problems. I do not have the time to go through and update literally thousands of tests that would be broken by zpp changes. Also, any change to zpp’s behaviour has backwards compatibility implications. There are some minor things I would like to change in zpp, which I will make an RFC for regardless of the success of this RFC (integer overflow should fail or at least raise a notice). Also, as I mentioned above, the resulting proposal would probably not please people who want strict types.
-- Andrea Faulds http://ajf.me/

Derick Rethans

11 years ago
On Sun, 14 Sep 2014, Andrea Faulds wrote:
> > On 14 Sep 2014, at 13:17, Zeev Suraski <zeev@zend.com> wrote: > > > I honestly don't see why we can't be consistent for the simple > > types, or at least strive to be as consistent as possible as opposed > > to introducing a complete parallel universe for userland functions, > > that's inconsistent with the entirety of the rest of PHP. We don't > > have to be consistent with ALL internal functions, which obviously > > have the option of doing custom checks and return failures not only > > if you fail to, say, pass on a string - but also if that string is > > not of a special format. But then, you have the option of doing > > that also in userland functions using custom code. The question is > > not the customized cases - it's the standard behavioral cases - > > comparing zpp rules for scalars and the newly-introduced rules for > > userland scalars. > > OK, we could go for exactly what zpp does already. However, then we > don’t have a compromise. We are siding with what you want, but most > userland developers would prefer a strict system. While this would > please you and perhaps some other folk on internals, and sure, it’d be > consistent, it would not be popular with the wider PHP community. > Similarly, we could go for a fully strict approach, which might please > some userland developers, but wouldn’t please you. > > I don’t want to go down this route. I’d prefer we compromise and keep > PHP’s weakly-typed nature (an int can be accepted as a float or a > string), but make it stricter (no data loss), thus arriving at a > middle way.
But this "compromise" introduces yet another set of casting rules. And that is why will advocate and vote against this. We can either have casting where: function bar(int $foo) { } bar("42"); Means the same as: function bar($foo) { } bar((int) "42"); Or we can have it as a strict cast, or we can have it like it currently (ie, not at all). I can live with those three options, but not an option where casting/checking does something different again. cheers, Derick

Andrew Faulds

11 years ago
On 14 Sep 2014, at 21:22, Derick Rethans <derick@php.net> wrote:
> But this "compromise" introduces yet another set of casting rules. And > that is why will advocate and vote against this. > > We can either have casting where: > > function bar(int $foo) { } > bar("42"); > > Means the same as: > > function bar($foo) { } > bar((int) "42”); >
Is anyone really advocating for that? I thought people wanted zpp-style casting.
-- Andrea Faulds http://ajf.me/

Rowan Collins

11 years ago
On 14/09/2014 21:22, Derick Rethans wrote:
> Or we can have it as a strict cast, or we can have it like it currently > (ie, not at all). I can live with those three options, but not an > option where casting/checking does something different again.
Is there an existing, documented, definition of "strict cast" which you would approve of?
-- Rowan Collins [IMSoP]

Matthew Leverton

11 years ago
On Sun, Sep 14, 2014 at 3:22 PM, Derick Rethans <derick@php.net> wrote:
> We can either have casting where: > > function bar(int $foo) { } > bar("42"); > > Means the same as: > > function bar($foo) { } > bar((int) "42"); > > Or we can have it as a strict cast, or we can have it like it currently > (ie, not at all). I can live with those three options, but not an > option where casting/checking does something different again. >
I totally agree with this. I can understand any of the following: 1) function f(int $a) { } == f( (int) $a); -- with NO notices 2) function f(int $a) {} means $a must be exactly type int -- or recoverable error is thrown 3) current behavior (no scalar type hints) But a new set of rules that require a 23x7 table to describe what's going on ... not a big fan.
-- Matthew Leverton

Andrey Andreev

11 years ago
Hi, As a userland guy, I'm against this one. It seems a bit political ... A lot of people want either strict type hints or type casting hints, but because the people on this list can't get to agree on any of the two, this RFC tries to make a compromise that, IMO, satisfies nobody. It's done not for the benefit of everybody, but just so we can say that PHP has "scalar type hints". I've said this in the RFC discussion thread: In order to satisfy both sides, instead of arguing which is the one and true way, just implement both. That is also why I suggested altering the proposed syntax here from: function foo(int $bar) to: function foo((int) $bar) Whether it applies more strict rules or not, this RFC is clearly about type _casting_ hints, while the used syntax has so far only been used for strict type hints. I know some would argue that this is a side effect because you can't cast to a specific class type, but that's not the point - the result is a strict type hint and that's what people are used to. This is both inconsistent AND prevents PHP from having strict scalar type hints in the future (because of the used syntax). I (and I'm sure many others) would love both features separately, not a hacky mix between the two. Cheers, Andrey.

Andrew Faulds

11 years ago
On 14 Sep 2014, at 15:56, Andrey Andreev <narf@devilix.net> wrote:
> It seems a bit political ... A lot of people want either strict type > hints or type casting hints, but because the people on this list can't > get to agree on any of the two, this RFC tries to make a compromise > that, IMO, satisfies nobody. It's done not for the benefit of > everybody, but just so we can say that PHP has "scalar type hints". > I've said this in the RFC discussion thread: In order to satisfy both > sides, instead of arguing which is the one and true way, just > implement both.
I’m not sure it satisfies nobody. I realise a lot of people would prefer strict types, but plenty are still in favour of this as a compromise: http://strawpoll.me/2463199/r - Might not be representative, but I held a poll on Twitter and StackOverflow. There are the results. 67 would vote in favour of strict hints (à la Hack), and 36 would vote in favour of this RFC, with only 5 willing to vote against it, versus 10 voting against strict hints. Note that the options aren’t mutually exclusive.
> That is also why I suggested altering the proposed syntax here from: > > function foo(int $bar) > > to: > > function foo((int) $bar)
But it’s not a cast. Such a syntax would be misleading. You would reasonably expect the following two functions to be equivalent: function foo($bar) { $bar = (int)$bar; } function foo((int) $bar) { } Yet they are not equivalent at all.
> Whether it applies more strict rules or not, this RFC is clearly about > type _casting_ hints, while the used syntax has so far only been used > for strict type hints.
Well, we haven’t had any scalar hints so far. It’s also worth noting that the documentation for PHP uses this style even though internal functions are not strict. Furthermore, scalar types in PHP are fundamentally different from non-scalar types. We do not have weak typing for non-scalars: there are no implicit casts between non-scalar types. However, we implicitly convert and juggle the scalar types all the time.
> I know some would argue that this is a side > effect because you can't cast to a specific class type, but that's not > the point - the result is a strict type hint and that's what people > are used to. This is both inconsistent AND prevents PHP from having > strict scalar type hints in the future (because of the used syntax).
Strict hints would still be possible with different syntax. However, I’m not sure they’re a good idea. Strict hints would be abandoning PHP’s weakly-typed nature, and would segregate userland functions into two kinds: strict and non-strict. I also don’t expect that would pass internals anyway. I don’t think permitting multiple options is really the way forward, that sounds like the worst of both worlds.
-- Andrea Faulds http://ajf.me/

Andrey Andreev

11 years ago
Hi, On Sun, Sep 14, 2014 at 6:09 PM, Andrea Faulds <ajf@ajf.me> wrote:
> > On 14 Sep 2014, at 15:56, Andrey Andreev <narf@devilix.net> wrote: > >> It seems a bit political ... A lot of people want either strict type >> hints or type casting hints, but because the people on this list can't >> get to agree on any of the two, this RFC tries to make a compromise >> that, IMO, satisfies nobody. It's done not for the benefit of >> everybody, but just so we can say that PHP has "scalar type hints". >> I've said this in the RFC discussion thread: In order to satisfy both >> sides, instead of arguing which is the one and true way, just >> implement both. > > I’m not sure it satisfies nobody. I realise a lot of people would prefer strict types, but plenty are still in favour of this as a compromise: > > http://strawpoll.me/2463199/r - Might not be representative, but I held a poll on Twitter and StackOverflow. There are the results. 67 would vote in favour of strict hints (à la Hack), and 36 would vote in favour of this RFC, with only 5 willing to vote against it, versus 10 voting against strict hints. Note that the options aren’t mutually exclusive.
So, basically ... the poll shows nothing. :) I was actually looking for that link (I took part in the poll), but couldn't find it.
>> That is also why I suggested altering the proposed syntax here from: >> >> function foo(int $bar) >> >> to: >> >> function foo((int) $bar) > > But it’s not a cast. Such a syntax would be misleading. You would reasonably expect the following two functions to be equivalent: > > function foo($bar) { $bar = (int)$bar; } > function foo((int) $bar) { } > > Yet they are not equivalent at all.
They are not equivalent, because of the different rules that you're proposing here, but it is a cast. And the fact that with this RFC _both_ would mean some kind of cast is happening, is simply bad.
>> Whether it applies more strict rules or not, this RFC is clearly about >> type _casting_ hints, while the used syntax has so far only been used >> for strict type hints. > > Well, we haven’t had any scalar hints so far. It’s also worth noting that the documentation for PHP uses this style even though internal functions are not strict. Furthermore, scalar types in PHP are fundamentally different from non-scalar types. We do not have weak typing for non-scalars: there are no implicit casts between non-scalar types. However, we implicitly convert and juggle the scalar types all the time.
... and I responded to this in the next paragraph. Userland doesn't case about internal differences, that's why they're called internal and why people outside of php-internals have for so long been puzzled why we only have array and object type hints.
>> I know some would argue that this is a side >> effect because you can't cast to a specific class type, but that's not >> the point - the result is a strict type hint and that's what people >> are used to. This is both inconsistent AND prevents PHP from having >> strict scalar type hints in the future (because of the used syntax). > > Strict hints would still be possible with different syntax. However, I’m not sure they’re a good idea. Strict hints would be abandoning PHP’s weakly-typed nature, and would segregate userland functions into two kinds: strict and non-strict. I also don’t expect that would pass internals anyway.
That's exactly what I'm talking about ... we already have a syntax for strict typing, (and I want to put an emphais on this) *regardless of the reasons why, we do have strict type hints*. I don't understand why everybody is pretending that PHP doesn't have that feature just because we're talking about scalars here. Speaking strictly from a userland perspective, using that same syntax for something different will be inconsistent and will cause confusion. That another syntax could be used for strict typing in the future is just not a serious thing to say. As for the language's nature/philosophy/direction/whatever, I had something written about that, but chose to discard it in order to avoid that "discussion". Everybody is tired of talking about that.
> I don’t think permitting multiple options is really the way forward, that sounds like the worst of both worlds.
You can't say it's the worst of both worlds when you have both worlds in their entirety. And most importantly, they have always been two separate worlds because they just don't make sense otherwise. I could argue that this compromise RFC here is a mistake exactly because it is trying to mix them. Cheers, Andrey.

Andrew Faulds

11 years ago
On 14 Sep 2014, at 16:40, Andrey Andreev <narf@devilix.net> wrote:
> ... and I responded to this in the next paragraph. Userland doesn't > case about internal differences, that's why they're called internal > and why people outside of php-internals have for so long been puzzled > why we only have array and object type hints.
"Internal functions” refers to functions from extensions. It has no relation to internal*s*, the mailing list.
> That's exactly what I'm talking about ... we already have a syntax for > strict typing, (and I want to put an emphais on this) *regardless of > the reasons why, we do have strict type hints*. I don't understand why > everybody is pretending that PHP doesn't have that feature just > because we're talking about scalars here.
Nobody’s arguing we don’t already have strict type hints. I’m arguing strict hints don’t make sense for scalars. Note that we document strict (array, object) parameter types and non-strict (scalar) parameter types in the same way in our documentation.
> Speaking strictly from a userland perspective, using that same syntax > for something different will be inconsistent and will cause confusion. > That another syntax could be used for strict typing in the future is > just not a serious thing to say.
Why is it "not a serious thing to say”? I don’t understand.
> You can't say it's the worst of both worlds when you have both worlds > in their entirety.
No, you can. Allowing both options can be “the worst of both worlds”. This is just a trivial matter of semantics, though.
> And most importantly, they have always been two > separate worlds because they just don't make sense otherwise.
Have they? We already mix strict and non-strict typing in PHP. Functions taking objects, internal or not, will error if you pass the wrong type of object. Internal functions taking scalars will not error and instead cast, at least most of the time.
-- Andrea Faulds http://ajf.me/

Stas Malyshev

11 years ago
Hi!
> The vote would be for the RFC as it is. Possible changes are things > in the RFC I was uncertain on. I might hold votes on some of them, > actually.
I'm sorry, but this makes little sense to me. There are a number of mutually contradictory options here, how you can vote for them "as it is"? Only one of them can be implemented in code. If everybody agrees that one of them should be so, then just remove the others. If there's disagreement then how people can vote "yes" having contradictory ideas in mind? One of them would be voting "yes" for the opposite of what he thought he's voting. If the specific choices do not matter, just choose one of the options - if they're so insignificant nobody would care if you end up with another. In any case, there should be a clear understanding of what is actually proposed, instead of having 5 branching points for potentially 32 different RFCs. Or, if you absolutely can't make up your mind you can offer the options for the vote. But there should be some specificity. Otherwise the vote is meaningless - it's not the vote for specific implementation but just for some vague "let's do something I don't care what it is" and I don't think such votes make sense. Please choose something and put it to a vote. If it doesn't work, we can have different try or maybe we can just see there's no consensus on this for now, but at least we'd be clear and not think we have consensus on something but not know what actually it is.
> The problem is that we *can't* be consistent. Internal functions > already live in this completely different world from user land > functions. Nullability is handled differently. They use implicit
If we can't make it work well, maybe we should clean it up first instead of making it worse.
> If we're just talking about casting, then this proposal does not > alter casting behaviour, in fact I've been very careful not to touch > it. It does, however, only accept a subset of the values zpp usually
Casting while passing parameters is part of the casting behavior.
> does. This is because it is a compromise between strict and weak > parameter typing. While I understand you might like it to be fully
Again, my opinion is that bad compromise is worse than not having any implementation, because it closes future possibilities for better solution. Of course, there's a risk better one would never come, so everyone has to decide if they want "at least something" now or have a better one latter.
> weak like zpp, many userland developers are in favour of something > stricter, thus the compromise. Unlike zpp's rules, at least these > rules are clearly specified and prevent data loss.
I'm not sure why is this attention to data loss. In most scenarios when we're not talking about banking applications etc. if you expect a number and you've got a string "123abc", treating it as 123 is fine. No *useful* data is lost, since "abc" part passed to a numeric function is not useful data, it's garbage. Of course, for some app if you have garbage in data you're supposed to stop the app and wait for the human to arrive and clean up because it's too sensitive to trust a computer to figure it out. But in more common case just ignoring the garbage would work fine.
-- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/

Andrew Faulds

11 years ago
On 14 Sep 2014, at 18:29, Stas Malyshev <smalyshev@sugarcrm.com> wrote:
> Hi! > >> The vote would be for the RFC as it is. Possible changes are things >> in the RFC I was uncertain on. I might hold votes on some of them, >> actually. > > I'm sorry, but this makes little sense to me. There are a number of > mutually contradictory options here, how you can vote for them "as it > is”?
The “possible changes” section only details possible *changes*, that is, changes to the RFC that could be made, but have not been. So you would be voting for the RFC without any of those changes. If those changes were made, they would cease to be possible changes.
-- Andrea Faulds http://ajf.me/

Stas Malyshev

11 years ago
Hi!
> The “possible changes” section only details possible *changes*, that > is, changes to the RFC that could be made, but have not been. So you
OK, so by "possible changes" you mean things that could have been into RFC, but were rejected and are not part of the RFC? Then I agree it is clear but I'd feel better if it was explicitly stated there that these things are not part of the RFC and the vote.
-- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/

Andrew Faulds

11 years ago
On 14 Sep 2014, at 18:34, Stas Malyshev <smalyshev@sugarcrm.com> wrote:
> Hi! > >> The “possible changes” section only details possible *changes*, that >> is, changes to the RFC that could be made, but have not been. So you > > OK, so by "possible changes" you mean things that could have been into > RFC, but were rejected and are not part of the RFC? Then I agree it is > clear but I'd feel better if it was explicitly stated there that these > things are not part of the RFC and the vote.
The section now says "For points I'm unsure on, this section lists possible future changes to the RFC.”.
-- Andrea Faulds http://ajf.me/

Alain Williams

11 years ago
On Sun, Sep 14, 2014 at 01:10:20AM +0100, Andrea Faulds wrote:
> > On 14 Sep 2014, at 01:07, Alain Williams <addw@phcomp.co.uk> wrote: > > > You give an option for float to int casting to truncate. > > Where?
Possible Changes Float to Int Casting Rules ... This could be relaxed for semi-representable values. So 1.5 could be allowed for an int parameter (casted to 1).
-- Alain Williams Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer. +44 (0) 787 668 0256 http://www.phcomp.co.uk/ Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php #include <std_disclaimer.h>

Pierre Joye

11 years ago
On Sun, Sep 14, 2014 at 11:12 AM, Alain Williams <addw@phcomp.co.uk> wrote:
> On Sun, Sep 14, 2014 at 01:10:20AM +0100, Andrea Faulds wrote: >> >> On 14 Sep 2014, at 01:07, Alain Williams <addw@phcomp.co.uk> wrote: >> >> > You give an option for float to int casting to truncate. >> >> Where? > > Possible Changes > > Float to Int Casting Rules > > ... > > This could be relaxed for semi-representable values. So 1.5 could be allowed > for an int parameter (casted to 1).
That's one thing I do not like much. If one accepts both int and float, numeric should be used instead. But allowing float for int and raises warnings on data losses is just awkward imho.
-- Pierre @pierrejoye | http://www.libgd.org

Pierre Joye

11 years ago
hi Andrea, On Sun, Sep 14, 2014 at 1:30 AM, Andrea Faulds <ajf@ajf.me> wrote:
> Good evening, > > I’ve given up on my plan B, so I’m putting this RFC, finally, to a vote. > > I would urge you to vote in favour. It is not going to please everyone, it is after all a compromise proposal. However, I have tried my best to strike a balance between complete weak typing and strict typing. If this passes, we will finally have userland type specifiers for scalar types. It’s not perfect, but I’d argue it’s far better than nothing. > > Voting starts today, 2014-09-14, and ends in a week’s time, 2014-09-21.
Great job! It brings also some clarity and sense to type casting, at least for this area. I do not see much of a problem with the small delta with existing casting. I am not sure about the recoverable error, especially when old codes will use new implementations, it may have a bad impact depending on how the errors are a managed in a given library. But there is no BC per se in existing codes due to this. All in all, I will vote +1. No matter what we think what PHP should do, remain or be, this addition has been a long due request from the waste majority of our users. Let see what they think about it. Hopefully some lead devs of the major tools/libs/frameworks will post their views here. Cheers.
-- Pierre @pierrejoye | http://www.libgd.org

Thomas .

11 years ago
> From: ajf@ajf.me > Date: Sun, 14 Sep 2014 00:30:40 +0100 > To: internals@lists.php.net > Subject: [PHP-DEV] [VOTE][RFC] Scalar Type Hinting with Cast > > Good evening, > > I’ve given up on my plan B, so I’m putting this RFC, finally, to a vote. > > I would urge you to vote in favour. It is not going to please everyone, it is after all a compromise proposal. However, I have tried my best to strike a balance between complete weak typing and strict typing. If this passes, we will finally have userland type specifiers for scalar types. It’s not perfect, but I’d argue it’s far better than nothing. > > Voting starts today, 2014-09-14, and ends in a week’s time, 2014-09-21. > > Thanks! > > https://wiki.php.net/rfc/scalar_type_hinting_with_cast#vote > -- > Andrea Faulds > http://ajf.me/ > >
RE: [PHP-DEV] [VOTE][RFC] Scalar Type Hinting with CastThomas Punt 11:23 To: Andrea Faulds> From: ajf@ajf.me
> Date: Sun, 14 Sep 2014 00:30:40 +0100 > To: internals@lists.php.net > Subject: [PHP-DEV] [VOTE][RFC] Scalar Type Hinting with Cast > > Good evening, > > I’ve given up on my plan B, so I’m putting this RFC, finally, to a vote. > > I would urge you to vote in favour. It is not going to please everyone, it is after all a compromise proposal. However, I have tried my best to strike a balance between complete weak typing and strict typing. If this passes, we will finally have userland type specifiers for scalar types. It’s not perfect, but I’d argue it’s far better than nothing. > > Voting starts today, 2014-09-14, and ends in a week’s time, 2014-09-21. > > Thanks! > > https://wiki.php.net/rfc/scalar_type_hinting_with_cast#vote > -- > Andrea Faulds > http://ajf.me/ > >
From a user-land developer perspective, I really detest this feature being added into PHP. If PHP has features that will make it a stronger-typed language, then it will undoubtedly lose its loosely-typed and forgiving nature eventually. The evangelists for making PHP a strongly-typed language will boast of its benefits (which will have some truth, in all fairness), and this will only lead to the condemnation of PHP code that doesn't use these stricter language features (slowly converting PHP into a stricter language). This isn't the easy-going and generally-liked nature of the PHP language in _any_ way. Whilst I can see there are benefits of making PHP stricter, I for one would rather keep PHP as a loosely-typed and forgiving language. After all, if I wanted to use a strongly-typed language for building websites, then I'd be using Java instead. The only part of the discussion I liked on this topic was the ability to use explicit type casts as part of the function signature. Whilst it is a bit of a syntactic sugar (which will cut out on some PHP code in the function body too), it does give the function parameters some sort of signature that will show their expected set of values (assuming developers know the casting rules, of course). -Tom (P.s sorry for the double email Andrea, I accidentally sent this to you only the first time...)>

Dan Ackroyd

11 years ago
Hi Andrea, When you re-open the voting, please can you have an option that is 'Yes - without numeric typehint' ? Although I like the idea of the RFC, having a type-hint that matches multiple scalar types seems distinctly weird to me. It really needs a separate case making for it, separate from the 'should we have scalar type hints for the current types' question. cheers Dan On 14 September 2014 00:30, Andrea Faulds <ajf@ajf.me> wrote:

Andrew Faulds

11 years ago
On 14 Sep 2014, at 18:23, Dan Ackroyd <danack@basereality.com> wrote:
> When you re-open the voting, please can you have an option that is > 'Yes - without numeric typehint’ ?
I was already planning that, actually. :)
> Although I like the idea of the RFC, having a type-hint that matches > multiple scalar types seems distinctly weird to me. > > It really needs a separate case making for it, separate from the > 'should we have scalar type hints for the current types' question.
Yeah. While me and Anthony thought it was a no-brainer, I’ve since seen some opposition to it, so I’d have its own vote.
-- Andrea Faulds http://ajf.me/

Unnamed Person

11 years ago
Hello Andrea, I find the rfc a bit difficult to understand: e.g. is_float('1') gives false, but foo('1') with function foo(float $a){} is ok. Regards, Thomas Andrea Faulds wrote on 14.09.2014 01:30:

Sebastian Bergmann

11 years ago
Am 14.09.2014 um 01:30 schrieb Andrea Faulds:
> https://wiki.php.net/rfc/scalar_type_hinting_with_cast
If I understand the "Syntax" section correctly, then int, float, numeric, bool, and string are added as new reserved words (keywords). This would break existing code, for instance userland classes named "String". However, looking at the patch in http://bit.ly/1uADQnD I do not see an addition of new tokens. So maybe there is no problem at all. In any case, I think it would be best to build on the great work Nikita did with the AST-based parser for PHP 7 and refactor the compiler to allow reserved words as names for namespaces, classes, interfaces, ... *before* we introduce new reserved words (and thus BC breaks).

Andrew Faulds

11 years ago
On 15 Sep 2014, at 17:42, Sebastian Bergmann <sebastian@php.net> wrote:
> Am 14.09.2014 um 01:30 schrieb Andrea Faulds: >> https://wiki.php.net/rfc/scalar_type_hinting_with_cast > > If I understand the "Syntax" section correctly, then int, float, numeric, > bool, and string are added as new reserved words (keywords). This would > break existing code, for instance userland classes named "String". > > However, looking at the patch in http://bit.ly/1uADQnD I do not see > an addition of new tokens. So maybe there is no problem at all.
Why are you looking at the initial commit from two years ago? Many, many changes have been made since then. Did you not look at the branch? Anyhow, this RFC is withdrawn, so there’s little point commenting on it further.
-- Andrea Faulds http://ajf.me/

Sebastian Bergmann

11 years ago
Am 15.09.2014 um 18:45 schrieb Andrea Faulds:
> Why are you looking at the initial commit from two years ago?
Sorry, my bad. No idea how that I happened, I should have looked at http://bit.ly/1AQLqdD instead. And in there I see rules such as %token T_STRING_TYPE that introduce new reserved words.
> Anyhow, this RFC is withdrawn, so there’s little point commenting on > it further.
Again, sorry. Going through a huge backlog of internals@ mail. I was not aware of the fact that the RFC was withdrawn. But be that as it may, I still think that the PHP compiler should allow reserved words as names for namespaces, classes, interfaces. And only once this is allowed should we introduce new reserved words.

Rowan Collins

11 years ago
Sebastian Bergmann wrote (on 15/09/2014):
> In any case, I think it would be best to build on the great work Nikita > did with the AST-based parser for PHP 7 and refactor the compiler to > allow reserved words as names for namespaces, classes, interfaces, ... > *before* we introduce new reserved words (and thus BC breaks).
Surely, in this case, the reserved word would have to be banned as a class/interface name *anyway*, even if the parser generally allowed reserved words there? Otherwise the following is ambiguous: class Int { public function test(int $foo) { // Is this function requiring an instance of class Int, or a scalar of type int? } } If the interpretation is as a class name, then defining that class breaks all instances of scalar hinting everywhere in the current execution. Not to mention how it would interact with autoloading, reinterpreting already compiled functions, etc. If the interpretation is as an int, then the BC break exists anyway, because the keyword has to be "partially reserved", which is probably more confusing than just reserving it. I'm all for keeping the number of reserved words in a language down (cf Douglas Crockford on why JSON requires quoting on all key names), but it makes things very messy if you try not to have any at all.
-- Rowan Collins [IMSoP]