Hi all,
I've updated the patch and the RFC that proposes the return type-hint implementation (Engine + Reflection).
The proposed implementation is working just like the last changes in the parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric" pseudo-types.
http://wiki.php.net/rfc/returntypehint
Thoughts?
--
Regards,
Felipe Pena
I would like to see this in the next major/minor release, together with the traits and the type-hint implementation. This would be a great language improvement.
From my end-developer standpoint a big +++
On Wed, 28 Jul 2010 22:49:03 -0300, Felipe Pena felipensp@gmail.com wrote:
Quoted text · 6 lines
Hi all,
I've updated the patch and the RFC that proposes the return type-hint implementation (Engine + Reflection).
The proposed implementation is working just like the last changes in the parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric"
pseudo-types.http://wiki.php.net/rfc/returntypehint
Thoughts?
Quoted text · 8 lines
Hi all,
I've updated the patch and the RFC that proposes the return type-hint implementation (Engine + Reflection).
The proposed implementation is working just like the last changes in the parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric" pseudo-types.http://wiki.php.net/rfc/returntypehint
Thoughts?
--
Regards,
Felipe Pena
Hi, I would love this feature.
But I have some concerns.
AFAIK you can declare classes with the name int ,scalar, etc. so hinting anything else than class names can be ambiguous. Or we have to set these keywords as reserved, so nobody could declare classes with such names.
But in this case, this would be a big BC problem, especially, because I think that there is many project out there which uses type names(int, object, etc.) as classname.
Try to search "class Object{" or "class Object extends" on google codesearch.
Maybe this was resolved with the last scalar type hinting patch, which got merged to the trunk, I don't know.
ps: the spam filter rejected my previous email because of the codesearch links...
Tyrael
Quoted text · 11 lines
Hi all,
I've updated the patch and the RFC that proposes the return type-hint implementation (Engine + Reflection).
The proposed implementation is working just like the last changes in the parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric" pseudo-types.http://wiki.php.net/rfc/returntypehint
Thoughts?
--
Regards,
Felipe PenaHi, I would love this feature.
But I have some concerns.
AFAIK you can declare classes with the name int ,scalar, etc. so hinting anything else than class names can be ambiguous. Or we have to set these keywords as reserved, so nobody could declare classes with such names.
Weren't they already made reserved with the parameter type hinting patch?
Quoted text · 6 lines
But in this case, this would be a big BC problem, especially, because I think that there is many project out there which uses type names(int, object, etc.) as classname.
Try to search "class Object{" or "class Object extends" on google codesearch.
Maybe this was resolved with the last scalar type hinting patch, which got merged to the trunk, I don't know.
ps: the spam filter rejected my previous email because of the codesearch links...
Tyrael
--
--
Daniel Egeberg
Quoted text · 4 lines
Hi, I would love this feature.
But I have some concerns.
AFAIK you can declare classes with the name int ,scalar, etc. so hinting anything else than class names can be ambiguous. Or we have to set these keywords as reserved, so nobody could declare classes with such names.Weren't they already made reserved with the parameter type hinting patch?
As I said:
Maybe this was resolved with the last scalar type hinting patch, which got merged to the trunk, I don't know.
Tyrael
Hi
2010/7/29 Ferenc Kovacs info@tyrael.hu
Quoted text · 6 lines
Hi, I would love this feature.
But I have some concerns.
AFAIK you can declare classes with the name int ,scalar, etc. so hinting anything else than class names can be ambiguous. Or we have to set these keywords as reserved, so nobody could declare classes with such names.Weren't they already made reserved with the parameter type hinting patch?
As I said:
Maybe this was resolved with the last scalar type hinting patch, which got merged to the trunk, I don't know.
Currently on trunk version, if you use the php native types as type-hint it will check for the php type, not the user class.
Trunk:
<?php
class int { }
function test(int $a) { }
test(1); // ok
test(new int); // error
Which is just like the return type-hint is working. I do prefer offer a way to continue using the user class 'Int', as we not going to add new keywords... My suggestion (I guess already told it in some mail...) is to identify the native php type just when it's lowercased (case-sensitive). (Java-style)
Patch: http://felipe.ath.cx/diff/param-typehint-native-type.diff
Example:
<?php
class int { }
function test(Int $a) { } // user class 'Int' function test2(int $a) { } // native type 'int'
test(new int); // ok
test2(1); // ok
Thanks for the feedbacks. :-)
--
Regards,
Felipe Pena
My suggestion (I guess already told it in some mail...) is to identify the native php type just when it's lowercased (case-sensitive).
Alternatively, one could use the full qualified name to refer to the class name, e.g.
function expectsScalar(string $str) {}
function expectsObject(\string $obj) {}
-JD
+1 for "one could use the full qualified name to refer to the class name".
Making the developer care about the case of characters in one special case - that's the sort of changes that lead to chaos. Remember that type conversion works in a case-insensitive manner and so does most of the language constructs:
(int) $x === (INT) $x
intvav($x) === INTVAL($x)
and so on
2010/7/29 Josh Davis phpwnd@gmail.com
Quoted text · 6 lines
My suggestion (I guess already told it in some mail...) is to identify the native php type just when it's lowercased (case-sensitive).
Alternatively, one could use the full qualified name to refer to the class name, e.g.
function expectsScalar(string $str) {}
function expectsObject(\string $obj) {}-JD
--
--
С уважением,
Виктор
Quoted text · 9 lines
Hi all,
I've updated the patch and the RFC that proposes the return type-hint implementation (Engine + Reflection).
The proposed implementation is working just like the last changes in the parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric"
pseudo-types.http://wiki.php.net/rfc/returntypehint
Thoughts?
--
Regards,
Felipe Pena
for the upcoming 5.4 release, I think it would be extremely useful, even without the scalar stuff.
Tyrael
Quoted text · 11 lines
Hi all,
I've updated the patch and the RFC that proposes the return type-hint implementation (Engine + Reflection).
The proposed implementation is working just like the last changes in the parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric"
pseudo-types.http://wiki.php.net/rfc/returntypehint
Thoughts?
--
Regards,
Felipe Penafor the upcoming 5.4 release, I think it would be extremely useful, even without the scalar stuff.
Tyrael
Hi,
Now that someone else necromanced this thread back to life, may I ask which variant of Type Hinting have emerged from the debate as the candidate for implementation?
Also, I seem to remember a variant with both strict and weak type hinting/checking, using parentheses to distinguish behavior. What happened to that, never made it to an RFC?
--
Pas
Quoted text · 18 lines
On Thu, Jul 29, 2010 at 3:49 AM, Felipe Pena felipensp@gmail.com wrote:
Hi all,
I've updated the patch and the RFC that proposes the return type-hint implementation (Engine + Reflection).
The proposed implementation is working just like the last changes in the parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric"
pseudo-types.http://wiki.php.net/rfc/returntypehint
Thoughts?
--
Regards,
Felipe Penafor the upcoming 5.4 release, I think it would be extremely useful, even without the scalar stuff.
Tyrael
Hi,
Now that someone else necromanced this thread back to life, may I ask which variant of Type Hinting have emerged from the debate as the candidate for implementation?
Also, I seem to remember a variant with both strict and weak type hinting/checking, using parentheses to distinguish behavior. What happened to that, never made it to an RFC?
--
Pas--
Quoted text · 18 lines
On Thu, Jul 29, 2010 at 3:49 AM, Felipe Pena felipensp@gmail.com wrote:
Hi all,
I've updated the patch and the RFC that proposes the return type-hint implementation (Engine + Reflection).
The proposed implementation is working just like the last changes in the parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric"
pseudo-types.http://wiki.php.net/rfc/returntypehint
Thoughts?
--
Regards,
Felipe Penafor the upcoming 5.4 release, I think it would be extremely useful, even without the scalar stuff.
Tyrael
Hi,
Now that someone else necromanced this thread back to life, may I ask which variant of Type Hinting have emerged from the debate as the candidate for implementation?
Also, I seem to remember a variant with both strict and weak type hinting/checking, using parentheses to distinguish behavior. What happened to that, never made it to an RFC?
--
Pas--
https://wiki.php.net/rfc/typechecking
Ilia's proposal was implemented, then reverted, and Derick's proposal was commited, but judging from the mails on internals, this will/could bereverted from the next release.
the other proposals was never implemented.
ps: we have a(many) different thread for scalar type-hint discussion
Tyrael
There was no consensus on that, but as i remember the tendency was more to the weak type hinting because of dynamic nature of PHP. You know - all the http data comes as strings, most db data comes as strings and so on. 27.04.2011 11:30 пользователь "Pas" pasthelod@gmail.com написал:
Quoted text · 16 lines
Hi all,
I've updated the patch and the RFC that proposes the return type-hint implementation (Engine + Reflection).
The proposed implementation is working just like the last changes in the parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric"
pseudo-types.http://wiki.php.net/rfc/returntypehint
Thoughts?
--
Regards,
Felipe Penafor the upcoming 5.4 release, I think it would be extremely useful, even without the scalar stuff.
Tyrael
Hi,
Now that someone else necromanced this thread back to life, may I ask which variant of Type Hinting have emerged from the debate as the candidate for implementation?
Also, I seem to remember a variant with both strict and weak type hinting/checking, using parentheses to distinguish behavior. What happened to that, never made it to an RFC?
--
Pas
Hi!
http://wiki.php.net/rfc/returntypehint
for the upcoming 5.4 release, I think it would be extremely useful, even without the scalar stuff.
Personally, I see even less point in having strict return typing (please stop using "hint" terminology, it confuses the whole matter, it's not "hint" if it describes mandatory restriction on the data) than strict argument typing. The RFC doesn't explain it either beyond "here how you can have some nice errors".
But why would I want to see these errors? How they would make anything easier or better?
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Am 28.04.2011 10:23, schrieb Stas Malyshev:
But why would I want to see these errors? How they would make anything easier or better?
to find problems before your users?
i do not know what you want, but i want with "error_reporting = E_ALL | E_STRICT" get noticed if something possible is unclean
it is the same as for functions - so what is the difference between the following? the first one can get a empty string and will not notice you and ignore the foreach
the second one will produce an error if empty string is given while an empty array would be accepted
function bla($arr)
{
if(!empty($arr))
{
foreach($arr)
{
}
}
}
function bla(array $arr)
{
if(!empty($arr))
{
foreach($arr)
{
}
}
}
Hi!
to find problems before your users?
Sorry, this doesn't say much. It's a generic, of course we agree it's good to find bugs sooner rather than later. The question is why would that specific thing that is being proposed help - in which specific way?
the second one will produce an error if empty string is given while an empty array would be accepted
And it is better because?..
Also please note we are discussing strict return typing.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
to find problems before your users?
Sorry, this doesn't say much. It's a generic, of course we agree it's good to find bugs sooner rather than later. The question is why would that specific thing that is being proposed help - in which specific way?
It would mean that the code that follows could benefit from the PHP run time checking the return type -- at the point of return. Thus eliminating following errors where the user does not check, eg:
function array foo() { .... }
$ret = foo();
foreach($ret as $val)
....
I must admit that this is much less useful than checking of argument types going into functions, where the writer of the function (part of a libarary perhaps) does not trust the function user to get validation/... right. But this is another discussion.
The other way in that this could be very helpful is with run time optimisation, especially for things like HipHop - think of the optimisation that it could make if it knew the real return type, eg:
function int bar() { ... }
(The argument about argument types is even more so here.)
the second one will produce an error if empty string is given while an empty array would be accepted
And it is better because?..
Also please note we are discussing strict return typing.
I am also mindful that the proposed syntax does not allow description of functions that return more than one type; where they return something to indicate a failure (or end) - typically FALSE or NULL. So maybe something like the following could cope with them:
function (array | bool) baz() { ... }
or even:
function (array | false) baz() { ... }
I can see that some may prefer commas:
function (array, false) baz() { ... }
although, to me, that looks like a list - I prefer vertical bar.
HipHop would be able to optimise things like:
if( ($ret = baz()) !== `FALSE` ) {
...
}
[**] https://github.com/facebook/hiphop-php/wiki/
--
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
Quoted text · 5 lines
Hi!
to find problems before your users?
Sorry, this doesn't say much. It's a generic, of course we agree it's good to find bugs sooner rather than later. The question is why would that specific thing that is being proposed help - in which specific way?
It would mean that the code that follows could benefit from the PHP run time checking the return type -- at the point of return. Thus eliminating following errors where the user does not check, eg:
function array foo() { .... } $ret = foo(); foreach($ret as $val) ....
I don't see this being useful at all. All you are doing is duplicating a runtime warning. We can't possibly know at compile-time whether this return value is an array or not, so there is no compile-time benefit.
eg.
function array foo() {
$ret = bar();
return $ret;
}
So in this case we get a warning on the return and then another warning on the foreach(). We have not gained anything except an extra warning for the same problem in this code example.
And this gets even worse if you start doing scalar return type checking. Now you are adding warnings for types that are safely interchangeable.
-Rasmus
function array foo() { .... } $ret = foo(); foreach($ret as $val) ....I don't see this being useful at all. All you are doing is duplicating a runtime warning. We can't possibly know at compile-time whether this return value is an array or not, so there is no compile-time benefit.
The check is not done where foo() is called but within foo() itself, to ensure that it only returns an array.
There will also be advantages for HipHop which can afford to spend the time to do static analysis of code -- I know that HipHop is not your baby but you now need to recognise that there is more than one PHP implementation and features that may not had much advantage with Zend may be useful elsewhere.
--
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
Hey All,
I'm really interested on this topic.
What would be the result of this?
function Foo foo($something) {
if ( $something ) {
return new Foo;
}
// no return implies return null
}
How do you force to return a Type with null allowed?
Martin Scotta
Quoted text · 12 lines
function array foo() { .... } $ret = foo(); foreach($ret as $val) ....I don't see this being useful at all. All you are doing is duplicating a runtime warning. We can't possibly know at compile-time whether this return value is an array or not, so there is no compile-time benefit.
The check is not done where foo() is called but within foo() itself, to ensure
that it only returns an array.There will also be advantages for HipHop which can afford to spend the time to
do static analysis of code -- I know that HipHop is not your baby but you now need to recognise that there is more than one PHP implementation
and features that may not had much advantage with Zend may be useful elsewhere.--
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
Quoted text · 10 lines
Hey All,
I'm really interested on this topic.What would be the result of this?
function Foo foo($something) {
if ( $something ) {
return new Foo;
}
// no return implies return null
}How do you force to return a Type with null allowed?
That is why I suggested, in addition to the above, a syntax like:
function (Foo | NULL) foo($something) {
to be able to specify where a function can return more than one type. The 'extra' types/values are nearly always either NULL or FALSE.
Regards
--
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
Hi!
There will also be advantages for HipHop which can afford to spend the time to do static analysis of code -- I know that HipHop is not your baby but you now need to recognise that there is more than one PHP implementation and features that may not had much advantage with Zend may be useful elsewhere.
If you want a statically typed language, there are tons of these on the market. However, unless you change PHP into another - statically typed - language, I do not see how it really helps. What you talking about here is creating a dialect of PHP targeted for statically-compiled environment. I personally see it a bit pointless, since there are already many perfectly good static languages on the market, with excellent compilers and environments - so why not use any of these? But that shouldn't deter you - my personal opinion is just that, if you want to create your own static PHP, go ahead. But I do not believe doing it in little tweaks would work - if you want reliable typing, you need it everywhere.
However, I seriously doubt PHP as a whole would benefit from it. Most uses of PHP are very dynamic and would not yield serious benefits from introducing static typing constructs beyond very specific cases in very specific environments. Changing the whole language to benefit these narrow scenarios does not seem beneficial to me.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Here's my issue:
We're borrowing a feature from strongly typed languages and forcing it on a loosely typed language. I'm fine with this, if we're true to the concept.
In a regular language, if you type something to return an object of type Foo, you might still get back null, and appropriately need to check for this. NULL is a perfectly valid state for an object.
The objection I'm hearing to this is that because in PHP, null is a value and not a state, and since variables don't have types (values do), null should be an explicitly specified hint.
To me, this is a nuclear bomb waiting to go off. If we allow the syntax described earlier (Foo | Bar | Null) we're violating the concept entirely, and there's no point in even having the feature. If a developer can type hint a function in such a way that you don't actually know what you're getting besides a subset of types, this has the exact opposite effect that return hinting is supposed to have. Now, I have to check for what type the value returned is, on top of null. Why even use return hinting?
The other suggestion I've heard is to only allow for Object || Null, but this too seems ridiculous. The idea would be that if you're in a situation where you can't feasibly return the specified object, you should throw an exception. So now your code is going to be riddled with things like ObjectIsNullException, or generic handlers that don't know what to do besides go "OOPS!" when this happens. OR, you force default values down your object's throats. This is bad when the object you're using is volatile, and writes to a database or something.
The complain is that implicitly allowing null returns means you have to check for null even if you're expecting an object. But you have to do this anyway if you're not hinting.
+1 for return type hinting
-1 for explicitly specifying null
-100000000 for specifying multiple return types
Quoted text · 7 lines
Hi!
There will also be advantages for HipHop which can afford to spend the time to do static analysis of code -- I know that HipHop is not your baby but you now need to recognise that there is more than one PHP implementation and features that may not had much advantage with Zend may be useful elsewhere.
If you want a statically typed language, there are tons of these on the market. However, unless you change PHP into another - statically typed - language, I do not see how it really helps. What you talking about here is creating a dialect of PHP targeted for statically-compiled environment. I personally see it a bit pointless, since there are already many perfectly good static languages on the market, with excellent compilers and environments - so why not use any of these? But that shouldn't deter you - my personal opinion is just that, if you want to create your own static PHP, go ahead. But I do not believe doing it in little tweaks would work - if you want reliable typing, you need it everywhere.
However, I seriously doubt PHP as a whole would benefit from it. Most uses of PHP are very dynamic and would not yield serious benefits from introducing static typing constructs beyond very specific cases in very specific environments. Changing the whole language to benefit these narrow scenarios does not seem beneficial to me.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Martin Scotta
Here's my issue:
We're borrowing a feature from strongly typed languages and forcing it on a loosely typed language. I'm fine with this, if we're true to the concept.
In a regular language, if you type something to return an object of type Foo, you might still get back null, and appropriately need to check for this.
NULLis a perfectly valid state for an object.
Probably core folks may know how this works better than me but I think that null is not an object, scalar or anything at all. It just means the absence of value.
That's why I always thought that NULL meant "no value".
function foo(Foo $f1, Foo $f2=null) {
// $f1 can't be null
// $f2 can be null
}
Quoted text · 16 lines
The objection I'm hearing to this is that because in PHP, null is a value and not a state, and since variables don't have types (values do), null should be an explicitly specified hint.
To me, this is a nuclear bomb waiting to go off. If we allow the syntax described earlier (Foo | Bar | Null) we're violating the concept entirely, and there's no point in even having the feature. If a developer can type hint a function in such a way that you don't actually know what you're getting besides a subset of types, this has the exact opposite effect that return hinting is supposed to have. Now, I have to check for what type the value returned is, on top of null. Why even use return hinting?
The other suggestion I've heard is to only allow for Object || Null, but this too seems ridiculous. The idea would be that if you're in a situation where you can't feasibly return the specified object, you should throw an exception. So now your code is going to be riddled with things like ObjectIsNullException, or generic handlers that don't know what to do besides go "OOPS!" when this happens. OR, you force default values down your object's throats. This is bad when the object you're using is volatile, and writes to a database or something.
The complain is that implicitly allowing null returns means you have to check for null even if you're expecting an object. But you have to do this anyway if you're not hinting.
+1 for return type hinting
-1 for explicitly specifying null
-100000000 for specifying multiple return typesHi!
There will also be advantages for HipHop which can afford to spend the time to
do static analysis of code -- I know that HipHop is not your baby but you now need to recognise that there is more than one PHP implementation
and features that may not had much advantage with Zend may be useful elsewhere.If you want a statically typed language, there are tons of these on the market. However, unless you change PHP into another - statically typed - language, I do not see how it really helps. What you talking about here is creating a dialect of PHP targeted for statically-compiled environment. I personally see it a bit pointless, since there are already many perfectly good static languages on the market, with excellent compilers and environments - so why not use any of these? But that shouldn't deter you - my personal opinion is just that, if you want to create your own static PHP, go ahead. But I do not believe doing it in little tweaks would work - if you want reliable typing, you need it everywhere.
However, I seriously doubt PHP as a whole would benefit from it. Most uses of PHP are very dynamic and would not yield serious benefits from introducing static typing constructs beyond very specific cases in very specific environments. Changing the whole language to benefit these narrow scenarios does not seem beneficial to me.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Right, that's what null means. The difference in php is only in implementation -- you actually just made my point :)
The only reason to have explicit null hints is if you get hung up on the whole issue of how php actually implements NULL. The concept is "non value", distinct from "empty". The implementation in php is slightly difference, since you're not actually working with, say, a null pointer.
Quoted text · 24 lines
Martin Scotta
Here's my issue:
We're borrowing a feature from strongly typed languages and forcing it on a loosely typed language. I'm fine with this, if we're true to the concept.
In a regular language, if you type something to return an object of type Foo, you might still get back null, and appropriately need to check for this.
NULLis a perfectly valid state for an object.Probably core folks may know how this works better than me but I think that null is not an object, scalar or anything at all. It just means the absence of value.
That's why I always thought that
NULLmeant "no value".function foo(Foo $f1, Foo $f2=null) {
// $f1 can't be null
// $f2 can be null
}The objection I'm hearing to this is that because in PHP, null is a value and not a state, and since variables don't have types (values do), null should be an explicitly specified hint.
To me, this is a nuclear bomb waiting to go off. If we allow the syntax described earlier (Foo | Bar | Null) we're violating the concept entirely, and there's no point in even having the feature. If a developer can type hint a function in such a way that you don't actually know what you're getting besides a subset of types, this has the exact opposite effect that return hinting is supposed to have. Now, I have to check for what type the value returned is, on top of null. Why even use return hinting?
The other suggestion I've heard is to only allow for Object || Null, but this too seems ridiculous. The idea would be that if you're in a situation where you can't feasibly return the specified object, you should throw an exception. So now your code is going to be riddled with things like ObjectIsNullException, or generic handlers that don't know what to do besides go "OOPS!" when this happens. OR, you force default values down your object's throats. This is bad when the object you're using is volatile, and writes to a database or something.
The complain is that implicitly allowing null returns means you have to check for null even if you're expecting an object. But you have to do this anyway if you're not hinting.
+1 for return type hinting
-1 for explicitly specifying null
-100000000 for specifying multiple return typesHi!
There will also be advantages for HipHop which can afford to spend the time to do static analysis of code -- I know that HipHop is not your baby but you now need to recognise that there is more than one PHP implementation and features that may not had much advantage with Zend may be useful elsewhere.
If you want a statically typed language, there are tons of these on the market. However, unless you change PHP into another - statically typed - language, I do not see how it really helps. What you talking about here is creating a dialect of PHP targeted for statically-compiled environment. I personally see it a bit pointless, since there are already many perfectly good static languages on the market, with excellent compilers and environments - so why not use any of these? But that shouldn't deter you - my personal opinion is just that, if you want to create your own static PHP, go ahead. But I do not believe doing it in little tweaks would work - if you want reliable typing, you need it everywhere.
However, I seriously doubt PHP as a whole would benefit from it. Most uses of PHP are very dynamic and would not yield serious benefits from introducing static typing constructs beyond very specific cases in very specific environments. Changing the whole language to benefit these narrow scenarios does not seem beneficial to me.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
On Thu, Apr 28, 2011 at 10:23 AM, Stas Malyshev smalyshev@sugarcrm.comwrote:
Quoted text · 8 lines
Hi!
http://wiki.php.net/rfc/returntypehint
for the upcoming 5.4 release, I think it would be extremely useful, even without the scalar stuff.
Personally, I see even less point in having strict return typing (please stop using "hint" terminology, it confuses the whole matter, it's not "hint" if it describes mandatory restriction on the data) than strict argument typing. The RFC doesn't explain it either beyond "here how you can have some nice errors".
But why would I want to see these errors? How they would make anything easier or better?Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
do you aware that without the scalar support, the return type hinting would behave just as "strict" as the argument type hinting is currently. http://php.net/manual/en/language.oop5.typehinting.php
so maybe type hinting wasn't a good name at the first place, but we should be consistent with the current naming convention.
Tyrael
do you aware that without the scalar support, the return type hinting would behave just as "strict" as the argument type hinting is currently. http://php.net/manual/en/language.oop5.typehinting.php
so maybe type hinting wasn't a good name at the first place, but we should be consistent with the current naming convention.
Yes, it was a poor choice of words. I think that it should be renamed 'type checking' with a transition note, eg:
Type checking (formerly called type hinting)
--
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
Quoted text · 5 lines
The RFC doesn't explain it either beyond "here how you can have some nice errors".
But why would I want to see these errors? How they would make anything easier or better?Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
maybe we should improve on the rfc.
I see many advantages of the introduction of return type hinting.
- you can guarantee that your methods returns only what is explicitly stated (or else it blows up, the same as for arguments)
- it can save you from having to define the return type in phpdoc, your code can define it's behavior
- I think maybe the language itself could use that information, for performance, etc. improvements (we know that the return value will be created runtime, but we can throw it away if it doesn't match the return type or something), maybe we can use that information to throw errors on compile time if we find a non runtime decided return value which doesn't match with the return type.
I'm sure that others have and will come up with more use cases.
Tyrael
Hi!
I see many advantages of the introduction of return type hinting.
- you can guarantee that your methods returns only what is explicitly stated (or else it blows up, the same as for arguments)
You can't, because function resolution happens in runtime, so the only point you know what the function called "foo" returns is the moment where you're already calling it, not a moment before.
- it can save you from having to define the return type in phpdoc, your code can define it's behavior
Why I need being "saved" from documenting my code properly? Anyway, the function code defines the behavior, declaration of return type just ensures function would fail in runtime if your code tries to return unexpected data - but how is it helpful? The client of this function doesn't even know that before actually calling it!
- I think maybe the language itself could use that information, for performance, etc. improvements (we know that the return value will be
That would need static compile-time binding of functions, which implies static binding of includes, etc. - basically, compiling PHP code as a whole in one monolitic application.
created runtime, but we can throw it away if it doesn't match the return type or something), maybe we can use that information to throw errors on compile time if we find a non runtime decided return value which doesn't match with the return type.
Sorry, what is "non runtime decided return value"? And how is "compile time" different from "runtime" in your opinion - when exactly that "compile time" happens?
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
On Thu, Apr 28, 2011 at 7:45 PM, Stas Malyshev smalyshev@sugarcrm.comwrote:
Quoted text · 4 lines
Hi!
I see many advantages of the introduction of return type hinting.
- you can guarantee that your methods returns only what is explicitly stated (or else it blows up, the same as for arguments)
You can't, because function resolution happens in runtime, so the only point you know what the function called "foo" returns is the moment where you're already calling it, not a moment before.
it's the same as for the current argument type hinting. you can access that information via Reflection http://hu2.php.net/manual/en/reflectionparameter.getclass.php
Quoted text · 4 lines
- it can save you from having to define the return type in phpdoc, your
code can define it's behavior
Why I need being "saved" from documenting my code properly? Anyway, the function code defines the behavior, declaration of return type just ensures function would fail in runtime if your code tries to return unexpected data
- but how is it helpful? The client of this function doesn't even know that before actually calling it!
why did we added ppp if people can define in the documentation(or in the method name :/) that which method is public and which isn't
- I think maybe the language itself could use that information, for
performance, etc. improvements (we know that the return value will be
That would need static compile-time binding of functions, which implies static binding of includes, etc. - basically, compiling PHP code as a whole in one monolitic application.
I see
created runtime, but we can throw it away if it doesn't match the return
type or something), maybe we can use that information to throw errors on compile time if we find a non runtime decided return value which doesn't match with the return type.
Sorry, what is "non runtime decided return value"? And how is "compile time" different from "runtime" in your opinion - when exactly that "compile time" happens?
Just throwing ideas around.
when we parse the php code to opcode, the interpreter could use the typehint information to catch trivial cases(eg. we cannot tell that return $foo is valid, but we can tell that return FALSE is not valid if the typehint doesn't specify that).
I hope this clears that up.
Tyrael
Hi!
it's the same as for the current argument type hinting. you can access that information via Reflection http://hu2.php.net/manual/en/reflectionparameter.getclass.php
Really? That's how you write your code - do a reflection check before every call? Nobody does that.
why did we added ppp if people can define in the documentation(or in the method name :/) that which method is public and which isn't
Well, this is a good argument, PPP case seems close. Still, PPP is not really as dynamic as variable types are - PPP picture is much simpler and it also serves the call target, not the caller - i.e., the caller does not rely on pretty much anything in PPP, while the target relies on its protected functions not being called by strangers. So my main objection is that return typing doesn't actually serve the caller.
Also, the fact that for PHP is extremely common to return null/false from methods on error makes a big hole in this concept even if we accepted the return typing as a counterpart to existing argument typing. E.g., seeing something like:
function (Foo|Bar|false|null) ugly()
makes me fill dizzy. This doesn't look like something we'd want.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
On Thu, Apr 28, 2011 at 8:49 PM, Stas Malyshev smalyshev@sugarcrm.com wrote:
Quoted text · 4 lines
Hi!
it's the same as for the current argument type hinting.
you can access that information via Reflection http://hu2.php.net/manual/en/reflectionparameter.getclass.php
Really? That's how you write your code - do a reflection check before every call? Nobody does that.
I didn't said that, probably I shouldn't have mention that, we are both aware of the capabilities of Reflection imo. but it's still true, that it would behave the same way as it does now for the argument type hinting.
you didn't reply on that, so I think we agree on that.
why did we added ppp if people can define in the documentation(or in the
method name :/) that which method is public and which isn't
Well, this is a good argument, PPP case seems close. Still, PPP is not really as dynamic as variable types are - PPP picture is much simpler and it also serves the call target, not the caller - i.e., the caller does not rely on pretty much anything in PPP, while the target relies on its protected functions not being called by strangers. So my main objection is that return typing doesn't actually serve the caller.
true, and Johannes brought this up also, but I didn't wanted to compare the ppp to return type hinting.
I'm just saying that you argument "Why I need being "saved" from documenting my code properly?" isn't apply here.
Because by this logic we could remove a lot of things, things which allows the developers to express their code more explicitly.
"Anyway, the function code defines the behavior, declaration of return type just ensures function would fail in runtime if your code tries to return unexpected data - but how is it helpful?" as I mentioned I can imagine cases when we can spot errors before actually executing the code, but I don't know enough about the internals to tell that this could be viable or not.
Also, the fact that for PHP is extremely common to return null/false from methods on error makes a big hole in this concept even if we accepted the return typing as a counterpart to existing argument typing. E.g., seeing something like:
function (Foo|Bar|false|null) ugly()
makes me fill dizzy. This doesn't look like something we'd want.
yeah, but usually you return null/false in case of error when you write procedural code.
but since we wouldn't support the return typehint for scalars, maybe we could assume that the only sane people who would use this feature would also do the error handling will through Exceptions (@throws :P).
Tyrael
Hi!
yeah, but usually you return null/false in case of error when you write procedural code.
Nope. I use it all the time when writing OO code and when "nothing" or "no object" or "can't do this" is a valid answer for a function to return.
but since we wouldn't support the return typehint for scalars, maybe we could assume that the only sane people who would use this feature would also do the error handling will through Exceptions (@throws :P).
Exceptions are for exceptional situations. If you query a DB for an object and there's no object like this, you don't want an exception if this situation is normal and handled in your code - you want a null or false or something like that. Exception is for situations that you code can not foresee or handle, they are not for regular data flow control.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
On Fri, Apr 29, 2011 at 7:04 AM, Stas Malyshev smalyshev@sugarcrm.comwrote:
Quoted text · 7 lines
Hi!
yeah, but usually you return null/false in case of error when you write
procedural code.
Nope. I use it all the time when writing OO code and when "nothing" or "no object" or "can't do this" is a valid answer for a function to return.
but since we wouldn't support the return typehint for scalars, maybe we
could assume that the only sane people who would use this feature would also do the error handling will through Exceptions (@throws :P).
Exceptions are for exceptional situations. If you query a DB for an object and there's no object like this, you don't want an exception if this situation is normal and handled in your code - you want a null or false or something like that. Exception is for situations that you code can not foresee or handle, they are not for regular data flow control.
some agree, some disagree with that.
http://barelyenough.org/blog/2007/11/when-to-use-exceptions/
Tyrael
Why I need being "saved" from documenting my code properly? Anyway, the function code defines the behavior, declaration of return type just ensures function would fail in runtime if your code tries to return unexpected data
- but how is it helpful? The client of this function doesn't even know that before actually calling it!
why did we added ppp if people can define in the documentation(or in the method name :/) that which method is public and which isn't
with ppp i prevent the client code from doing something the function/class author didn't expect. With this return hint the function auther can force a runtime error when doing something wrong himself. (which he has to check one way or the other using tests etc.)
johannes
Martin Scotta
2011/4/28 Johannes Schlüter johannes@schlueters.de
Quoted text · 6 lines
Why I need being "saved" from documenting my code properly? Anyway, the function code defines the behavior, declaration of return type just ensures
function would fail in runtime if your code tries to return unexpected data
- but how is it helpful? The client of this function doesn't even know that
before actually calling it!why did we added ppp if people can define in the documentation(or in the method name :/) that which method is public and which isn't
with ppp i prevent the client code from doing something the function/class author didn't expect. With this return hint the function auther can force a runtime error when doing something wrong himself. (which he has to check one way or the other using tests etc.)
Return types are meant to ensure that you get what you asked for. This is really useful when you need to delegate to some component, specially if it's not of your own, although there are ways to avoid that patterns.
IMHO I would not trust on any return value, as PHP did not ensure anything about them.
Even more, I do not write code that depend on return values, I prefer to use input/output parameters,
This is the safer way I've found to achieve the same behavior
function no_return(ReturnStatus $returnStatus) {
doSomething();
if ( itWasOk() )
{
$returnStatus->setReturnValue( new Foo ); // return type hint $returnStatus->success(); // return status }
elseif ( isWasCritical() )
{
$returnStatus->setException( new RuntimeException );
$returnStatus->exception(); // return status }
else
{
$returnStatus->fail(); // return status
}
}
Of course I don't write all functions like this. This is pattern I use when I need to ensure that the type returned is valid.
johannes
2011/4/28 Martin Scotta martinscotta@gmail.com:
- snip *
IMHO I would not trust on any return value, as PHP did not ensure anything about them.
Even more, I do not write code that depend on return values, I prefer to use input/output parameters,
I cannot help but wonder why PHP is your language of choice. I mean no disrespect, but PHP does not have strict typing (for a reason) and forcing it upon PHP seems strange to me. Seeing as a) you'll either have to implement the return type hinting yourself in which case you'd know anyway what the return value is or b) you'd have to have others implement it in their libraries in which case you'd have to wait years for any noticeable effect, the feature seems useful only in edge cases (such as Hiphop, which I think one can question the mainstream usefulness of, currently).
Personally, I read code if I'm not sure what a given function/method will return - or just test. I actually thought unit tests took care of issues like this. I don't think having a list of possible return values would make things simpler than the good old "try it and see" - which is one of the greatest assets of PHP.
Quoted text · 16 lines
This is the safer way I've found to achieve the same behavior
function no_return(ReturnStatus $returnStatus) {
doSomething();if ( itWasOk() )
{
$returnStatus->setReturnValue( new Foo ); // return type hint $returnStatus->success(); // return status }
elseif ( isWasCritical() )
{
$returnStatus->setException( new RuntimeException );
$returnStatus->exception(); // return status }
else
{
$returnStatus->fail(); // return status
}
}Of course I don't write all functions like this. This is pattern I use when I need to ensure that the type returned is valid.
I just return what I need to from the function/method or throw an exception. Indicating that you want to throw an exception instead of actually throwing one seems ... overly polite, at best. As far as I can tell, you'd have to check the ReturnStatus object anyway, so I don't see how you're any safer (PHP has functions for checking null values, false values, try/catch blocks and other things too). But maybe we're dealing with very different development environments and I just cannot see the usefulness of the suggested as it wouldn't make a positive difference in mine.
Just a perspective from a typical webdev. Regards
Peter
--
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype
Martin Scotta
Quoted text · 6 lines
2011/4/28 Martin Scotta martinscotta@gmail.com:
- snip *
IMHO I would not trust on any return value, as PHP did not ensure anything
about them.
Even more, I do not write code that depend on return values, I prefer to use input/output parameters,I cannot help but wonder why PHP is your language of choice. I mean no disrespect, but PHP does not have strict typing (for a reason) and forcing it upon PHP seems strange to me. Seeing as a) you'll either have to implement the return type hinting yourself in which case you'd know anyway what the return value is or b) you'd have to have others implement it in their libraries in which case you'd have to wait years for any noticeable effect, the feature seems useful only in edge cases (such as Hiphop, which I think one can question the mainstream usefulness of, currently).
I understand why PHP does not have strict typing as much as I understand why PHP do not have good-qualities third-party libraries. I'm not saying that there aren't good libraries, but the language has many holes that makes almost impossible to build solid library (ie: include)
All the strictness of ppp, typehint and the like are not restrict things, they are meant as boundaries for client code, to ensure the correct and well defined behavior.
It's ok if you write all you code as procedural, or you don't like to write classes, but IMHO the language should provide a solid foundation for them, this way you can rely on libraries for common task and make your procedural code even simpler, shorter and safer.
I'm not sure if return typehint will be a good idea, but I'm not against it. I believe the language has to deal with other "pending" stuff prior to this. PHP has many blind areas, whenever you call something, you don't know if you will return or it will just die in a non-sense Fatal error.
Quoted text · 27 lines
Personally, I read code if I'm not sure what a given function/method will return - or just test. I actually thought unit tests took care of issues like this. I don't think having a list of possible return values would make things simpler than the good old "try it and see" - which is one of the greatest assets of PHP.
This is the safer way I've found to achieve the same behavior
function no_return(ReturnStatus $returnStatus) {
doSomething();if ( itWasOk() )
{
$returnStatus->setReturnValue( new Foo ); // return type hint $returnStatus->success(); // return status }
elseif ( isWasCritical() )
{
$returnStatus->setException( new RuntimeException );
$returnStatus->exception(); // return status }
else
{
$returnStatus->fail(); // return status
}
}Of course I don't write all functions like this. This is pattern I use when I need to ensure that the type returned is valid.
I just return what I need to from the function/method or throw an exception. Indicating that you want to throw an exception instead of actually throwing one seems ... overly polite, at best. As far as I can tell, you'd have to check the ReturnStatus object anyway, so I don't see how you're any safer (PHP has functions for checking null values, false values, try/catch blocks and other things too). But maybe we're dealing with very different development environments and I just cannot see the usefulness of the suggested as it wouldn't make a positive difference in mine.
Just a perspective from a typical webdev. Regards
Peter--
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype
Quoted text · 10 lines
Martin Scotta
On Thu, Apr 28, 2011 at 5:15 PM, Peter Lind peter.e.lind@gmail.com wrote:
2011/4/28 Martin Scotta martinscotta@gmail.com:
- snip *
IMHO I would not trust on any return value, as PHP did not ensure anything
about them.
Even more, I do not write code that depend on return values, I prefer to
use input/output parameters,I cannot help but wonder why PHP is your language of choice. I mean no disrespect, but PHP does not have strict typing (for a reason) and forcing it upon PHP seems strange to me. Seeing as a) you'll either have to implement the return type hinting yourself in which case you'd know anyway what the return value is or b) you'd have to have others implement it in their libraries in which case you'd have to wait years for any noticeable effect, the feature seems useful only in edge cases (such as Hiphop, which I think one can question the mainstream usefulness of, currently).
I understand why PHP does not have strict typing as much as I understand why PHP do not have good-qualities third-party libraries. I'm not saying that there aren't good libraries, but the language has many holes that makes almost impossible to build solid library (ie: include)
I have come across good php libraries. In fact, I haven't used ant really bad ones. Again, maybe its a question of different environments - but I cannot see any reason why building a library should be inherently harder in php than strictly-typed languages.
All the strictness of ppp, typehint and the like are not restrict things, they are meant as boundaries for client code, to ensure the correct and well defined behavior.
There's a very big difference between ppp and typehinting. As Stas noted, they serve very different purposes - and while ppp typically goes with oop (though not in every language), strict typing is not in any way necessary to do oop. That should be obvious given the large amount of working oop code out there.
It's ok if you write all you code as procedural, or you don't like to write classes, but IMHO the language should provide a solid foundation for them, this way you can rely on libraries for common task and make your procedural code even simpler, shorter and safer.
My background was assembler before languages like php. To me, strict typing makes it harder, not easier, to make or use a good library. That's not to say that strict typing is bad - just that what you need depends on your experience. I've never had a need for strict typing in php, I don't find myself making the errors that strict typing solves in, say c#.
I'm not sure if return typehint will be a good idea, but I'm not against it.
I believe the language has to deal with other "pending" stuff prior to this. PHP has many blind areas, whenever you call something, you don't know if you will return or it will just die in a non-sense Fatal error.
Honestly, I've never had this problem, and I'd like to hear mire of your dev environment to see what leads to them :)
Regards
Peter
Quoted text · 27 lines
Personally, I read code if I'm not sure what a given function/method will return - or just test. I actually thought unit tests took care of issues like this. I don't think having a list of possible return values would make things simpler than the good old "try it and see" - which is one of the greatest assets of PHP.
This is the safer way I've found to achieve the same behavior
function no_return(ReturnStatus $returnStatus) {
doSomething();if ( itWasOk() )
{
$returnStatus->setReturnValue( new Foo ); // return type hint $returnStatus->success(); // return status }
elseif ( isWasCritical() )
{
$returnStatus->setException( new RuntimeException );
$returnStatus->exception(); // return status }
else
{
$returnStatus->fail(); // return status
}
}Of course I don't write all functions like this. This is pattern I use when I need to ensure that the type returned is valid.
I just return what I need to from the function/method or throw an exception. Indicating that you want to throw an exception instead of actually throwing one seems ... overly polite, at best. As far as I can tell, you'd have to check the ReturnStatus object anyway, so I don't see how you're any safer (PHP has functions for checking null values, false values, try/catch blocks and other things too). But maybe we're dealing with very different development environments and I just cannot see the usefulness of the suggested as it wouldn't make a positive difference in mine.
Just a perspective from a typical webdev. Regards
Peter--
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype
Quoted text · 4 lines
Hi!
I see many advantages of the introduction of return type hinting.
- you can guarantee that your methods returns only what is explicitly stated (or else it blows up, the same as for arguments)
You can't, because function resolution happens in runtime, so the only point you know what the function called "foo" returns is the moment where you're already calling it, not a moment before.
The Zend engine does function resolution at run time - others (eg HipHop) can afford to do it at compile time and so will know at compile time.
I will accept that sometimes it must always be done run time, eg:
$funcName($arg)
but that still allows most calls at compile time.
Anyway - the check for return type would be done in the called function, not the calling function.
- I think maybe the language itself could use that information, for performance, etc. improvements (we know that the return value will be
That would need static compile-time binding of functions, which implies static binding of includes, etc. - basically, compiling PHP code as a whole in one monolitic application.
Which might be acceptable for some applications. For those where it is not then this feature may not be useful. This is an optional declaration, not mandatory - so if it does not suit your purpose -- don't use it.
--
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
Hi!
I will accept that sometimes it must always be done run time, eg:
$funcName($arg)but that still allows most calls at compile time.
This is not the issue. This is the issue: $foo->bar($arg). There's no way to resolve it in "compile time", whatever that means, without having a type on $foo.
Anyway - the check for return type would be done in the called function, not the calling function.
That's the point. You don't get any extra safety on the caller's side, because you don't actually know much about the called function. With checking arguments, it makes more sense as you are serving the called function - so you know the code that ensures types and the code that relies on this assurance is together and can rely on each other. There are other problems with it, but not this one at least. Here you have the assurance on one side and the code that relies on it on the other.
Which might be acceptable for some applications. For those where it is not then this feature may not be useful. This is an optional declaration, not mandatory - so if it does not suit your purpose -- don't use it.
You are talking about pretty big change in language nature - introducing strict typing. "It's just option, don't use it if you don't want" doesn't really sound good in this case. If it's in the language, it should make sense and sit well with the rest of the language.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Quoted text · 5 lines
Hi!
I will accept that sometimes it must always be done run time, eg:
$funcName($arg)but that still allows most calls at compile time.
This is not the issue. This is the issue: $foo->bar($arg). There's no way to resolve it in "compile time", whatever that means, without having a type on $foo.
Which a good compiler with time to burn (think HipHop not Zend) could do, eg:
function joe(SomeClass $foo)
or:
$foo = new SomeClass();
or:
function SomeClass fred() { ... }
...
$foo = fred();
Anyway - the check for return type would be done in the called function, not the calling function.
That's the point. You don't get any extra safety on the caller's side, because you don't actually know much about the called function. With
You could by resolving the 'requires' at compile time, most of which you can. Yes you can find some examples when it won't, but don't ignore the most for the sake of a few.
You are talking about pretty big change in language nature - introducing strict typing. "It's just option, don't use it if you don't want" doesn't really sound good in this case. If it's in the language, it should make sense and sit well with the rest of the language.
And it will.
Objects are a big thing in the language - but their use is not mandated, many programs don't use them.
--
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
Hi!
Which a good compiler with time to burn (think HipHop not Zend) could do, eg:
- I think you overestimating the capabilities of HipHop, but I can be mistaken.
- HipHop represents a minority of use cases for PHP, I do not think it is practical for PHP to target only monolitic pre-compiled applications. At least until it has proven in the field that this is a better model.
In fact, if I am mistaken and this is so popular, why HipHop people don't introduce strict typing in their PHP and see how it goes? If it gets popular, great. If not, no harm done. Since they don't rely on Zend Engine anyway, AFAIK, keeping up with the patches in not really a question, so I don't see why it can't be done at any time.
And it will.
Objects are a big thing in the language - but their use is not mandated, many programs don't use them.
Right. But objects are done comprehensively, not by adding a little piece here and there. We had such thing in PHP 4, and it didn't really work until we redid it in PHP 5. Same with strict typing - you have to do the full thing to make it really useful. Serving just one use case is bad in the long run. I think adding full strict typing is not the way to go for PHP.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Which might be acceptable for some applications. For those where it is not
then this feature may not be useful. This is an optional declaration, not mandatory - so if it does not suit your purpose -- don't use it.
You are talking about pretty big change in language nature - introducing strict typing. "It's just option, don't use it if you don't want" doesn't really sound good in this case. If it's in the language, it should make sense and sit well with the rest of the language.
sorry, where would we intrudoc strict typing? as I mentioned before, the proposed implementation would "sit well with the rest of the language" in this aspect.
eg. would work the same as the argument type hinting does.
what Alain said, that there would be a possible to use the provided type information before actually calling the function. the fact that HipHop can use this information and Zend not (yet, or only for trivial cases) wouldn't change this fact, or the language itself.
Tyrael
FWIW, I don't see much point in this either, as far as error reporting is concerned. I think type checking for parameters can provide cheap and useful validation, as parameters passed in are outside the function's control. But return values? They are within the function's control, and in most functions are not hard to ensure are correct without needing runtime checking.
If PHP were compiled, not interpreted, i.e. less dynamic, there could be some benefits, as compile-time optimisations could perhaps be built in, and validations skipped if they were provably unnecessary. But that's not going to happen any time soon, as far as I'm aware.
So the only real benefit is that they could make code slightly self-documenting. But since they would still be optional, they don't help much: the coder might as well take up the option to write some more useful documentation.
Also, PHP has so many conventions such as returning false on error that don't fit cleanly into the model, that it's awkward. I'd love to see those problems solved neatly for function arguments before extending anything to function return types.
I don't think it would do any harm though, so don't see any argument against it. I just don't think there's a good argument for it.
Ben.
Quoted text · 4 lines
Hi!
http://wiki.php.net/rfc/returntypehint
for the upcoming 5.4 release, I think it would be extremely useful, even without the scalar stuff.Personally, I see even less point in having strict return typing (please stop using "hint" terminology, it confuses the whole matter, it's not "hint" if it describes mandatory restriction on the data) than strict argument typing. The RFC doesn't explain it either beyond "here how you can have some nice errors". But why would I want to see these errors? How they would make anything easier or better?
If PHP were compiled, not interpreted, i.e. less dynamic, there could be some benefits, as compile-time optimisations could perhaps be built in, and validations skipped if they were provably unnecessary. But that's not going to happen any time soon, as far as I'm aware.
It has already happened:
https://github.com/facebook/hiphop-php/wiki/
It might also be useful for reflection.
--
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
Quoted text · 4 lines
If PHP were compiled, not interpreted, i.e. less dynamic, there could be some benefits, as compile-time optimisations could perhaps be built in, and validations skipped if they were provably unnecessary. But that's not going to happen any time soon, as far as I'm aware.
It has already happened:
https://github.com/facebook/hiphop-php/wiki/
It might also be useful for reflection.
Hastily retracts comment.
That is very cool. I should give it a try.
I also see the value of such type checking for such a use case, obviously.
I guess there's a reason for it after all.
I'd still like to see something neat to handle more than just named classes, though, e.g. scalars, and things like "false on error" for return values.
Ben.
I also see the value of such type checking for such a use case, obviously.
I guess there's a reason for it after all.
I'd still like to see something neat to handle more than just named classes, though, e.g. scalars, and things like "false on error" for return values.
That is why I suggested, in addition to the above, a syntax like:
function (Foo | NULL) foo($something) {
to be able to specify where a function can return more than one type. The 'extra' types/values are nearly always either NULL or FALSE.
I agree that being able to specify as types: array, scalar, int, string, ... would be useful for a compiler but prob have little value for Zend - other than documentation/reflection.
--
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
Martin Scotta
On Thu, Apr 28, 2011 at 12:58 PM, Ben Schmidt <mail_ben_schmidt@yahoo.com.au
wrote:
FWIW, I don't see much point in this either, as far as error reporting is concerned. I think type checking for parameters can provide cheap and useful validation, as parameters passed in are outside the function's control. But return values? They are within the function's control, and in most functions are not hard to ensure are correct without needing runtime checking.
If PHP were compiled, not interpreted, i.e. less dynamic, there could be some benefits, as compile-time optimisations could perhaps be built in, and validations skipped if they were provably unnecessary. But that's not going to happen any time soon, as far as I'm aware.
Yes, PHP is interpreted, but remember that PHP has its own "compile-time". An option could be to use the "hints" only for compile purposes, but then at runtime the core can behave as it's today.
So the only real benefit is that they could make code slightly self-documenting. But since they would still be optional, they don't help much: the coder might as well take up the option to write some more useful documentation.
Also, PHP has so many conventions such as returning false on error that don't fit cleanly into the model, that it's awkward. I'd love to see those problems solved neatly for function arguments before extending anything to function return types.
I don't think it would do any harm though, so don't see any argument against it. I just don't think there's a good argument for it.
Quoted text · 9 lines
Ben.
Hi!
http://wiki.php.net/rfc/returntypehint
for the upcoming 5.4 release, I think it would be extremely useful, even without the scalar stuff.
Personally, I see even less point in having strict return typing (please stop
using "hint" terminology, it confuses the whole matter, it's not "hint" if it
describes mandatory restriction on the data) than strict argument typing. The RFC
doesn't explain it either beyond "here how you can have some nice errors". But why would I want to see these errors? How they would make anything easier or
better?