Hello All,
I would like to submit https://wiki.php.net/rfc/returntypehint2 into discussion. A link to the patch for this is provided and can be ran against the current HEAD.
There is an older entry still in existence, but this patch is syntactically different. The older entry is located at https://wiki.php.net/rfc/typechecking and is bundled with parameter, scalars, etc.
If possible, can someone promote this to the "Under Discussion" category within https://wiki.php.net/rfc?
-- Will
Hello All,
I would like to submit https://wiki.php.net/rfc/returntypehint2 into discussion. A link to the patch for this is provided and can be ran against the current HEAD.
There is an older entry still in existence, but this patch is syntactically different. The older entry is located at https://wiki.php.net/rfc/typechecking and is bundled with parameter, scalars, etc.
If possible, can someone promote this to the "Under Discussion" category within https://wiki.php.net/rfc?
-- Will
I've added it to https://wiki.php.net/rfc/typechecking with the other related RFCs.
This is linked from the "Under Discussion" section.
Chris
--
Email: christopher.jones@oracle.com
Tel: +1 650 506 8630
Blog: http://blogs.oracle.com/opal/
Hi Will!
One random thought I had while reading the RFC is: What about the
newly introduced callable typehint? Is this missing by intention? I
could well imagine so (because it's hard to check what scope
callability should be checked on), but wanted to be sure on that.
Nikita
Hello All,
I would like to submit https://wiki.php.net/rfc/returntypehint2 into discussion. A link to the patch for this is provided and can be ran against the current HEAD.
There is an older entry still in existence, but this patch is syntactically different. The older entry is located at https://wiki.php.net/rfc/typechecking and is bundled with parameter, scalars, etc.
If possible, can someone promote this to the "Under Discussion" category within https://wiki.php.net/rfc?
-- Will
Hi Nikita,
I didn't add that as it's not yet in production. As soon as things are finalized and 5.4 is GA, I will gladly add the callable type hint. The change wouldn't be different from parameter type hinting, and can easily be added.
Hi Will!
One random thought I had while reading the RFC is: What about the
newly introduced callable typehint? Is this missing by intention? I
could well imagine so (because it's hard to check what scope
callability should be checked on), but wanted to be sure on that.Nikita
Hello All,
I would like to submit https://wiki.php.net/rfc/returntypehint2 into discussion. A link to the patch for this is provided and can be ran against the current HEAD.
There is an older entry still in existence, but this patch is syntactically different. The older entry is located at https://wiki.php.net/rfc/typechecking and is bundled with parameter, scalars, etc.
If possible, can someone promote this to the "Under Discussion" category within https://wiki.php.net/rfc?
-- Will
hi Will,
You should add it now, while 5.4 final is not released yet, this
feature exists already and should be part of the RFC, to be complete.
Cheers,
Hi Nikita,
I didn't add that as it's not yet in production. As soon as things are finalized and 5.4 is GA, I will gladly add the callable type hint. The change wouldn't be different from parameter type hinting, and can easily be added.
Hi Will!
One random thought I had while reading the RFC is: What about the
newly introduced callable typehint? Is this missing by intention? I
could well imagine so (because it's hard to check what scope
callability should be checked on), but wanted to be sure on that.Nikita
Hello All,
I would like to submit https://wiki.php.net/rfc/returntypehint2 into discussion. A link to the patch for this is provided and can be ran against the current HEAD.
There is an older entry still in existence, but this patch is syntactically different. The older entry is located at https://wiki.php.net/rfc/typechecking and is bundled with parameter, scalars, etc.
If possible, can someone promote this to the "Under Discussion" category within https://wiki.php.net/rfc?
-- Will
--
--
Pierre
@pierrejoye | http://blog.thepimp.net | http://www.libgd.org
Will add tonight and generate a new patch.
hi Will,
You should add it now, while 5.4 final is not released yet, this
feature exists already and should be part of the RFC, to be complete.Cheers,
Hi Nikita,
I didn't add that as it's not yet in production. As soon as things are finalized and 5.4 is GA, I will gladly add the callable type hint. The change wouldn't be different from parameter type hinting, and can easily be added.
Hi Will!
One random thought I had while reading the RFC is: What about the
newly introduced callable typehint? Is this missing by intention? I
could well imagine so (because it's hard to check what scope
callability should be checked on), but wanted to be sure on that.Nikita
Hello All,
I would like to submit https://wiki.php.net/rfc/returntypehint2 into discussion. A link to the patch for this is provided and can be ran against the current HEAD.
There is an older entry still in existence, but this patch is syntactically different. The older entry is located at https://wiki.php.net/rfc/typechecking and is bundled with parameter, scalars, etc.
If possible, can someone promote this to the "Under Discussion" category within https://wiki.php.net/rfc?
-- Will
--
--
Pierre@pierrejoye | http://blog.thepimp.net | http://www.libgd.org
Hi Pierre and Nikita,
I have added callable to the patch and updated the RFC entry to reflect the changes. Please verify and let me know if you have any issues.
hi Will,
You should add it now, while 5.4 final is not released yet, this
feature exists already and should be part of the RFC, to be complete.Cheers,
Hi Nikita,
I didn't add that as it's not yet in production. As soon as things are finalized and 5.4 is GA, I will gladly add the callable type hint. The change wouldn't be different from parameter type hinting, and can easily be added.
Hi Will!
One random thought I had while reading the RFC is: What about the
newly introduced callable typehint? Is this missing by intention? I
could well imagine so (because it's hard to check what scope
callability should be checked on), but wanted to be sure on that.Nikita
Hello All,
I would like to submit https://wiki.php.net/rfc/returntypehint2 into discussion. A link to the patch for this is provided and can be ran against the current HEAD.
There is an older entry still in existence, but this patch is syntactically different. The older entry is located at https://wiki.php.net/rfc/typechecking and is bundled with parameter, scalars, etc.
If possible, can someone promote this to the "Under Discussion" category within https://wiki.php.net/rfc?
-- Will
--
--
Pierre@pierrejoye | http://blog.thepimp.net | http://www.libgd.org
The return type hinting is probably the biggest thing happening in php in a
long time.
Is this too good to be true?
With return type hinting I can see a huge improvenets in php IDEs in
spotting errors. Also it will be much easier to auto generate wsdl files
when return types are known to a compiler.
These are just to name a couple of important benefits that can come out of
it.
What I don't see is any discussion of being able to declare the type of
variable, you now, like in Java.
ie:
private \ArrayObject $customers;
Also, is there an implicit "void" return type?
I hope that declaring types of variables will be added too because without
it, this feature is not all that useful.
Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com
"A Top 100 Logistics I.T. Provider in 2011"
-----Original Message-----
From: Will Fitch [mailto:will.fitch@gmail.com]
Sent: Wednesday, December 21, 2011 5:29 PM
To: Pierre Joye
Cc: Nikita Popov; PHP Developers Mailing List
Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC
Hi Pierre and Nikita,
I have added callable to the patch and updated the RFC entry to reflect the
changes. Please verify and let me know if you have any issues.
hi Will,
You should add it now, while 5.4 final is not released yet, this
feature exists already and should be part of the RFC, to be complete.Cheers,
Hi Nikita,
I didn't add that as it's not yet in production. As soon as things are
finalized and 5.4 is GA, I will gladly add the callable type hint. The
change wouldn't be different from parameter type hinting, and can easily be
added.Hi Will!
One random thought I had while reading the RFC is: What about the
newly introduced callable typehint? Is this missing by intention? I
could well imagine so (because it's hard to check what scope
callability should be checked on), but wanted to be sure on that.Nikita
On Wed, Dec 21, 2011 at 3:09 AM, Will Fitch will.fitch@gmail.com
wrote:Hello All,
I would like to submit https://wiki.php.net/rfc/returntypehint2 into
discussion. A link to the patch for this is provided and can be ran against
the current HEAD.There is an older entry still in existence, but this patch is
syntactically different. The older entry is located at
https://wiki.php.net/rfc/typechecking and is bundled with parameter,
scalars, etc.If possible, can someone promote this to the "Under Discussion"
category within https://wiki.php.net/rfc?-- Will
--
--
Pierre@pierrejoye | http://blog.thepimp.net | http://www.libgd.org
2011/12/22 Dmitri Snytkine dsnytkine@ultralogistics.com
The return type hinting is probably the biggest thing happening in php in a
long time.
Is this too good to be true?With return type hinting I can see a huge improvenets in php IDEs in
spotting errors. Also it will be much easier to auto generate wsdl files
when return types are known to a compiler.
These are just to name a couple of important benefits that can come out of
it.What I don't see is any discussion of being able to declare the type of
variable, you now, like in Java.
ie:
private \ArrayObject $customers;
Once more "Why is PHP not Java?"
Also, is there an implicit "void" return type?
I hope that declaring types of variables will be added too because without
it, this feature is not all that useful.Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com"A Top 100 Logistics I.T. Provider in 2011"
-----Original Message-----
From: Will Fitch [mailto:will.fitch@gmail.com]
Sent: Wednesday, December 21, 2011 5:29 PM
To: Pierre Joye
Cc: Nikita Popov; PHP Developers Mailing List
Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFCHi Pierre and Nikita,
I have added callable to the patch and updated the RFC entry to reflect the
changes. Please verify and let me know if you have any issues.hi Will,
You should add it now, while 5.4 final is not released yet, this
feature exists already and should be part of the RFC, to be complete.Cheers,
On Wed, Dec 21, 2011 at 6:22 PM, Will Fitch will.fitch@gmail.com
wrote:Hi Nikita,
I didn't add that as it's not yet in production. As soon as things are
finalized and 5.4 is GA, I will gladly add the callable type hint. The
change wouldn't be different from parameter type hinting, and can easily be
added.Hi Will!
One random thought I had while reading the RFC is: What about the
newly introduced callable typehint? Is this missing by intention? I
could well imagine so (because it's hard to check what scope
callability should be checked on), but wanted to be sure on that.Nikita
On Wed, Dec 21, 2011 at 3:09 AM, Will Fitch will.fitch@gmail.com
wrote:Hello All,
I would like to submit https://wiki.php.net/rfc/returntypehint2 into
discussion. A link to the patch for this is provided and can be ran
against
the current HEAD.There is an older entry still in existence, but this patch is
syntactically different. The older entry is located at
https://wiki.php.net/rfc/typechecking and is bundled with parameter,
scalars, etc.If possible, can someone promote this to the "Under Discussion"
category within https://wiki.php.net/rfc?-- Will
--
--
Pierre@pierrejoye | http://blog.thepimp.net | http://www.libgd.org
private \ArrayObject $customers;
Once more "Why is PHP not Java?"
That comment confuses matters. People use PHP for all sorts of reasons, the
desire to tighten up in some places, on some occasions, should not be treated
with derision.
Tighter declarations, or any declarations at all, would not be mandatory. It would
be something that some projects might want to do. It would be nice if this could be
done on a module but module basis ... eg the implementors of a class library might
want to be really strict while allowing the class users to not be strict.
Just because Java has a feature that you appear to not like, does not mean that
others might not want it in some circumstances.
--
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
The return type hinting is probably the biggest thing happening in php in a
long time.
Is this too good to be true?With return type hinting I can see a huge improvenets in php IDEs in
spotting errors. Also it will be much easier to auto generate wsdl files
when return types are known to a compiler.
These are just to name a couple of important benefits that can come out of
it.What I don't see is any discussion of being able to declare the type of
variable, you now, like in Java.
ie:
private \ArrayObject $customers;
That looks like a namespace - I don't like that syntax.
Also, is there an implicit "void" return type?
I hope that declaring types of variables will be added too because without
it, this feature is not all that useful.
If - we do allow variable declaration to specify a type, can we also have a
syntax that simply says ''this variable is declared but I am not saying what type it is''.
Ie the variable can take any type - just as now.
Why ? One thing that I would really like to see in PHP is the ability to force
variables in a module to be declared -- AKA perl's 'use strict'. Have the compiler
complain if it sees a variable that has not been declared.
In a large program the occasional typeo on variable names does happen, catching them
with 'use strict' is great!
I have proposed this before and people did not like it. Here is to hopeing for a
different sentiment.
--
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
The return type hinting is probably the biggest thing happening in php in a
long time.
Is this too good to be true?
Hopefully not. We will see when the voting commences. :)
With return type hinting I can see a huge improvenets in php IDEs in
spotting errors. Also it will be much easier to auto generate wsdl files
when return types are known to a compiler.
These are just to name a couple of important benefits that can come out of
it.What I don't see is any discussion of being able to declare the type of
variable, you now, like in Java.
ie:
private \ArrayObject $customers;
Type hinting is relatively new to PHP, so we need to take careful steps in moving forward. With the introduction of parameter type hinting 5.0, and return type hinting following its lead, we will set ourselves up for a good base for a type hinting API and structure. That said, PHP is not Java, and type hinting in PHP is very different. It is, in fact, type hinting. This is a runtime feature. Only interface validation is verified at compile time, so we have to consider performance vs. feature in these cases.
Also, is there an implicit "void" return type?
The following types are allowed for both parameters and method returns:
- Callable
- Object (class name)
- Array
By not declaring a type hint with a parameter, it is considered mixed. With method returns, simply using the keyword "function" as you have been will signify mixed as well.
I hope that declaring types of variables will be added too because without
it, this feature is not all that useful.Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com"A Top 100 Logistics I.T. Provider in 2011"
-----Original Message-----
From: Will Fitch [mailto:will.fitch@gmail.com]
Sent: Wednesday, December 21, 2011 5:29 PM
To: Pierre Joye
Cc: Nikita Popov; PHP Developers Mailing List
Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFCHi Pierre and Nikita,
I have added callable to the patch and updated the RFC entry to reflect the
changes. Please verify and let me know if you have any issues.hi Will,
You should add it now, while 5.4 final is not released yet, this
feature exists already and should be part of the RFC, to be complete.Cheers,
Hi Nikita,
I didn't add that as it's not yet in production. As soon as things are
finalized and 5.4 is GA, I will gladly add the callable type hint. The
change wouldn't be different from parameter type hinting, and can easily be
added.Hi Will!
One random thought I had while reading the RFC is: What about the
newly introduced callable typehint? Is this missing by intention? I
could well imagine so (because it's hard to check what scope
callability should be checked on), but wanted to be sure on that.Nikita
On Wed, Dec 21, 2011 at 3:09 AM, Will Fitch will.fitch@gmail.com
wrote:Hello All,
I would like to submit https://wiki.php.net/rfc/returntypehint2 into
discussion. A link to the patch for this is provided and can be ran against
the current HEAD.There is an older entry still in existence, but this patch is
syntactically different. The older entry is located at
https://wiki.php.net/rfc/typechecking and is bundled with parameter,
scalars, etc.If possible, can someone promote this to the "Under Discussion"
category within https://wiki.php.net/rfc?-- Will
--
--
Pierre@pierrejoye | http://blog.thepimp.net | http://www.libgd.org
Hi!
That said, PHP is not Java, and type hinting in PHP is very
different. It is, in fact, type hinting. This is a runtime
feature. Only interface validation is verified at compile time, so
we have to consider performance vs. feature in these cases.
Naming it "hinting" was probably the worst mistake in PHP
documentation since "safe mode". The word hinting implies it's just
something non-mandatory, that can be used if needed, but ignored if not.
While using typing is indeed not mandatory, once used, it is mandatory
check that can not be ignored and produces fatal error in case of
mismatch. The only difference is that in Java this error is detected in
compile time, while in PHP it is a runtime failure, usually taking down
the application in the middle of processing, without possibility of
proper recovery. I'm not sure we're having the better deal here.
Performance is irrelevant here, more or less, since typing checks won't
take much time unless you have insanely deep class hierarchies (you have
to compare object's class and probably his parents' to the prescribed
type). Stability and maintainability of such code is an issue.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Your examples only show class methods with visibility qualifyiers, and
looking at the changes to zend_language_parser.y
it seems as if would only be available for methods. Wouldn't return
hints be available for plain functions?
In functional programming, it is common to return nullable types:
returns an instance of class Foo or null, an array or false, etc.
This is supported in normal arguments with the "= null" hint, and I
think there should be something equivalent for return types.
Does the proposed implementation allow null everywhere? (there's an
example of returning null from a return hint of array)
That seems wrong to me. Unless there's a way to make it strict (eg.
array! for only arrays).
Sent from my iPad
Your examples only show class methods with visibility qualifyiers, and
looking at the changes to zend_language_parser.y
it seems as if would only be available for methods. Wouldn't return
hints be available for plain functions?
Right now, it's only available for methods. Personally, I'm not a fan
of return types for functions. Adding this for functions in its form
today will also introduce shift/reduce issues.
In functional programming, it is common to return nullable types:
returns an instance of class Foo or null, an array or false, etc.This is supported in normal arguments with the "= null" hint, and I
think there should be something equivalent for return types.
Does the proposed implementation allow null everywhere? (there's an
example of returning null from a return hint of array)
That seems wrong to me. Unless there's a way to make it strict (eg.
array! for only arrays).
Most modern languages allow returning null in any case. This is a hail
Mary in the event something happens, but throwing an exception is
inappropriate. I see no reason to diverge from that.
Most modern languages allow returning null in any case. This is a hail
Mary in the event something happens, but throwing an exception is
inappropriate. I see no reason to diverge from that.
Agreed, it is often convenient to return NULL
or FALSE
to indicate failure
or something like end of input (think: fgetc()
). I was mulling syntax like:
function (string|boolean:false) fgetc(resource $handle)
Then the compiler would know that after:
if( ($ch = fgetc($in)) === FALSE)
return;
that $ch is string ... but seems too complicated.
--
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!
Most modern languages allow returning null in any case. This is a hail
Mary in the event something happens, but throwing an exception is
inappropriate. I see no reason to diverge from that.
In PHP, returning object if everything is OK and false if not is a very
common pattern.
Also, you understand that always allowing null means that this construct:
$foo = $this->returnsFoo();
$foo->fooMethod();
is no longer safe as you wanted it to be when you made returnsFoo use
strict typing? You'd have to check $foo anyway.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
Most modern languages allow returning null in any case. This is a hail
Mary in the event something happens, but throwing an exception is
inappropriate. I see no reason to diverge from that.In PHP, returning object if everything is OK and false if not is a very common pattern.
Also, you understand that always allowing null means that this construct:$foo = $this->returnsFoo();
$foo->fooMethod();is no longer safe as you wanted it to be when you made returnsFoo use strict typing? You'd have to check $foo anyway.
Are you suggesting not allowing null to be returned, or provide an
indicator within the syntax that it may return null? PHP would be the
first language I'm aware of that would do so in either case.
The point isn't to restrict a type hint to always be returned. The
goal is to guarantee that if a value is returned, it will be of type
X. In the event it is inappropriate to return the value back, would
you rather receive null or a bogus class instance, empty array or a
random function name back? Because that's what will happen. It will
become the "workaround" for not being able to return null.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
Are you suggesting not allowing null to be returned, or provide an
indicator within the syntax that it may return null? PHP would be the
first language I'm aware of that would do so in either case.
No I am not suggesting that. I'm just pointing out the problems with the
concept of strict typing in PHP and this particular instance of it. One
of them is that many functions do not actually return one type but
multiple types depending on the result. It is a very common pattern.
BTW, which languages you are talking about? PHP peers - Python, Ruby,
Perl, Javascript (to some measure), etc. don't have typing as far as I
know. Comparing PHP to statically compiled strictly typed languages does
not seem very useful to me. So could you clarify what do you mean?
The point isn't to restrict a type hint to always be returned. The
goal is to guarantee that if a value is returned, it will be of type
X. In the event it is inappropriate to return the value back, would
All functions in PHP return values, so it is not possible that no value
would be returned (yes, if you don't return anything it actually will
return null). So ensuring something is an object of type X prevents you
from returning anything else, false/null included. If you make
exceptions for null, then why not for false? If for false, then why not
for true? Etc.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi:
BTW, which languages you are talking about? PHP peers - Python, Ruby, Perl, Javascript (to some measure), etc. don't have typing as far as I know. Comparing PHP to statically compiled strictly typed languages does not seem very useful to me. So could you clarify what do you mean?
Perhaps the newest kid on the block deserves some reference for making optional typing more mainstream and being more radical about being checkable documentation: http://www.dartlang.org/docs/technical-overview/index.html
Dart's type system is not sound, and the design goal is to never (NEVER) interfere with execution semantics. It is more radical than previous research systems with its not-sound approach of making generics understandable, but people seem to appreciate that in practice.
It might give the necessary example to bridge the worlds between the 'PHP should be more like Java' and the 'PHP should be more like PHP' people.
Best regards
Stefan
--
Stefan Marr
Software Languages Lab
Vrije Universiteit Brussel
Pleinlaan 2 / B-1050 Brussels / Belgium
http://soft.vub.ac.be/~smarr
Phone: +32 2 629 2974
Fax: +32 2 629 3525
Sent from my iPhone
Hi!
Are you suggesting not allowing null to be returned, or provide an
indicator within the syntax that it may return null? PHP would be the
first language I'm aware of that would do so in either case.No I am not suggesting that. I'm just pointing out the problems with the concept of strict typing in PHP and this particular instance of it. One of them is that many functions do not actually return one type but multiple types depending on the result. It is a very common pattern.
And in those cases, they would continue to use the keyword "function"
and be considered unknown as they are today.
BTW, which languages you are talking about? PHP peers - Python, Ruby, Perl, Javascript (to some measure), etc. don't have typing as far as I know. Comparing PHP to statically compiled strictly typed languages does not seem very useful to me. So could you clarify what do you mean?
Not seem useful? Then during the discussions of parameter type
hinting, which language did you compare it to? What about abstracts
and interfaces?
I don't compare PHP to ruby, python or JavaScript. Do I suggest
features that I find useful in other languages like C# or Java or even
in any of the ones you listed? Absolutely.
Limiting yourself to comparing features only found in interpreted
languages is a very narrowed scope. Yes, return type validation does
add overhead, just as parameters do (arguably more). But it is a
feature that is very valuable to many. Those who wish not to use it
can continue to do so.
Don't forget that while return checks are runtime, interface
definition and implementations are compile time.
The point isn't to restrict a type hint to always be returned. The
goal is to guarantee that if a value is returned, it will be of type
X. In the event it is inappropriate to return the value back, wouldAll functions in PHP return values, so it is not possible that no value would be returned (yes, if you don't return anything it actually will return null). So ensuring something is an object of type X prevents you from returning anything else, false/null included. If you make exceptions for null, then why not for false? If for false, then why not for true? Etc.
Because null is a standard already set by parameter type hints. I do
not want to sway away from that as it works well. It is common for
null values to be considered "nothing", which is exactly what will be
returned if the specified hint isn't. All or nothing.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
And in those cases, they would continue to use the keyword "function"
and be considered unknown as they are today.
Taking the most common case and ignoring it and saying "ok, then don't
use it" is not a good way to design a feature in a general-purpose
language that would be used by literally millions.
I don't compare PHP to ruby, python or JavaScript. Do I suggest
features that I find useful in other languages like C# or Java or even
in any of the ones you listed? Absolutely.
C# and Java, again, are fully statically typed compiled languages. PHP
is not. That means that the benefits of strict type system that are
available to programmers in C# or Java will not be available to PHP
programmers, while the downsides of it (and inflexibility that such
system leads to) will still be there and will actually be multiplied by
the fact that you wouldn't know about the problem with your typing
system until your application starts crashing.
add overhead, just as parameters do (arguably more). But it is a
feature that is very valuable to many. Those who wish not to use it
can continue to do so.
Sorry, again, "don't use it" is not a valid response for a core feature
in a widely used language. It would be OK for a PECL module - if you
don't like mongoDB, don't use it, nobody objects to having mongodb
extension because it is not fit for some scenarios. However, designing
core language features that change the nature of the language - making
it from non-typed to strictly typed - have bigger requirements. You can
not just "not use" core feature - because others will be using it and
you'd have to leave with it and interface with their code.
Don't forget that while return checks are runtime, interface
definition and implementations are compile time.
Nothing is compile time in PHP, PHP has no compile time (at least if we
don't consider projects like HipHop, which is totally irrelevant here)
like C# or Java does. That's not how PHP code works. In C# and Java, you
can take the code and know in advance the type of any piece of data in
any place (maybe not precisely but at least base type). In PHP, you can
not.
Because null is a standard already set by parameter type hints. I do
not want to sway away from that as it works well. It is common for
I'm not sure what you're talking about as a "standard". Yes, input
parameters strict typing allows nulls because it was another frequently
used scenario that strict typing does not support. Fortunately, it
could be fit into "default value" syntax, even though it doesn't really
means that (so we actually have a syntax that means two different things
now, not a great idea). But with return types and value strict types
(which would inevitably be asked for next) it wouldn't be that easy.
There's no syntax for "default return value" and the case of returning
false - one of the most common - can not be covered. And, on top of
that, I still do not see any benefit of it as you'd still have to check
the return value anyway!
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
And in those cases, they would continue to use the keyword "function"
and be considered unknown as they are today.Taking the most common case and ignoring it and saying "ok, then don't use it" is not a good way to design a feature in a general-purpose language that would be used by literally millions.
The feature isn't designed around "only those who use it." In fact, as per the RFC, the keyword "function" will be used to serve as a mixed return type. There are cases where I don't want to type hint my return value from a method and will continue to use this. All scalar values will be forced to use this way anyway. Namespaces, interfaces, abstracts, closures, traits..... the list goes on with features that were built to give developers additional tools to use. Are any of them required to use? No. It's general purpose - just like the feature offered here. How many features are built into PHP that are meant to force a user to use it, or even break BC? Why would you suggest this is doing any different? Can you provide an example where this feature is doing that?
I don't compare PHP to ruby, python or JavaScript. Do I suggest
features that I find useful in other languages like C# or Java or even
in any of the ones you listed? Absolutely.C# and Java, again, are fully statically typed compiled languages. PHP is not. That means that the benefits of strict type system that are available to programmers in C# or Java will not be available to PHP programmers, while the downsides of it (and inflexibility that such system leads to) will still be there and will actually be multiplied by the fact that you wouldn't know about the problem with your typing system until your application starts crashing.
Are you saying parameter type hinting isn't in PHP?
add overhead, just as parameters do (arguably more). But it is a
feature that is very valuable to many. Those who wish not to use it
can continue to do so.Sorry, again, "don't use it" is not a valid response for a core feature in a widely used language. It would be OK for a PECL module - if you don't like mongoDB, don't use it, nobody objects to having mongodb extension because it is not fit for some scenarios. However, designing core language features that change the nature of the language - making it from non-typed to strictly typed - have bigger requirements. You can not just "not use" core feature - because others will be using it and you'd have to leave with it and interface with their code.
Sorry, again, look at what I said above. Namespaces, interfaces, abstracts, closures, traits, parameter type hints, even classes are core features that users aren't forced to use. Please tell me what makes them different. It is an optional feature - just like all those I listed. If others are using it, that's the point. Can you walk into existing code that you must interface with today that has a type hinted parameter and decide not to use it? No. Why? Because the company/developer who developed that solution made the decision that it was a feature they found useful. What you're suggesting is that we design features around those who do not want to use it.
Don't forget that while return checks are runtime, interface
definition and implementations are compile time.Nothing is compile time in PHP, PHP has no compile time (at least if we don't consider projects like HipHop, which is totally irrelevant here) like C# or Java does. That's not how PHP code works. In C# and Java, you can take the code and know in advance the type of any piece of data in any place (maybe not precisely but at least base type). In PHP, you can not.
So Java's bytecode, which requires a VM (interpreter), isn't comparable to PHP's "compile"? Are you saying compile is only when code is converted into machine code? Moving PHP code through a lexical analyzer, syntax parser, intermediate code generator/opcode isn't a form of compilation? Considering the verification of interfaces is done during the syntax parser, in a file called zend_compile.c, before the code is actually executed, I'm pretty comfortable calling this transformation "compiled."
Because null is a standard already set by parameter type hints. I do
not want to sway away from that as it works well. It is common forI'm not sure what you're talking about as a "standard". Yes, input parameters strict typing allows nulls because it was another frequently used scenario that strict typing does not support. Fortunately, it could be fit into "default value" syntax, even though it doesn't really means that (so we actually have a syntax that means two different things now, not a great idea). But with return types and value strict types (which would inevitably be asked for next) it wouldn't be that easy. There's no syntax for "default return value" and the case of returning false - one of the most common - can not be covered. And, on top of that, I still do not see any benefit of it as you'd still have to check the return value anyway!
public function blah(SomeClass $class = null)
{
if ($class)
{
// code here
}
}
What I'm calling a "standard" is something already in the language. If I had written this to allow "false" to be returned instead of null, the complaints would be much greater. Why? Because we're used to the type hinting allowing that.
I want to make it clear that my goal with this RFC isn't to offer a compile time feature for the sake of having it. Because PHP is interpreted and we have our on minds, we can make decisions such as the frequently used scenario you mentioned above. If the general consensus is that you must return the type declared, then I'm game for it. In fact, code execution would actually be faster. But what will end up happening is this:
public array getArray()
{
// Uh oh, I'll have to return an empty array
return array();
}
$array_value = $obj->getArray();
if (!empty($array_value))
This is why I decided on the null option.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
I would like to take this opportunity to query on a consensus:
Would you prefer to allow methods with type hinted return values to return null at will, or add a marker noting that it may return null?
Example: Return null at will
public ArrayIterator getIterator()
{
// something happened, will return null
return null;
}
Example: Return only if identified as such
public ArrayIterator? getIterator()
{
return null;
}
Would you prefer to allow methods with type hinted return values to return null at will, or add a marker noting that it may return null?
My preference would be to have a marker, and when null is not allowed, if the function tries to return it (or fails to return anything at all), then an error is raised or exception thrown. This behavior would be great for those cases where you're trying to protect against situations that theoretically should be impossible, much like the role of assertions. The marker then would handle those situations where you want to explicitly allow null return values based on routine inputs.
A common problem with PHP is that you just don't know what a function might do if you don't look over its code or it's docs. The marker makes one part of its behavior explicit, thus abbreviating the guessing game. (Not to mention when the docs themselves errantly fail to mention that a function can return types other than the obvious....)
In fitting with the PHP way, perhaps it would make sense that the marker indicates not just that null may be returned, but that any type may be returned. This would allow, say, returning false or -1 instead of null. Or maybe it's better just to allow indication of multiple types and have the marker be for just for null.
public ArrayIterator getIterator()
I would really, really prefer to always have the 'function' keyword be present. It offers something to scan for when quickly reviewing code, it makes it easier to do search-and-replace operations on functions, and it allows text editors that don't have the full-blown lexical analyzer of an IDE to still be able to pick out all the functions and offer the user an easy navigation feature.
I do, however, quite like the idea of putting the return type at the end of the function declaration. I've always disliked the way C and its derivatives stick the return type at the beginning (along with an ever-increasing list of other keywords), since it makes it harder to quickly scan the code by forcing a more thorough mental parsing instead of just letting you snap your eye to a known position. As for an operator suggestion, one precedent I can think of is from Pascal, which uses a colon:
function GetName(): string
This puts the result type at the end where you always know right where to look for it without mental parsing, and it reads naturally in that the effect (the result) is listed after the cause (the function). And, at least for English writers, the colon's purpose is intuitive because of its use in English grammar. Finally, PHP doesn't already use the single colon for anything that I can think of off-hand.
I'd also like to comment on the use of type checking in PHP. I completely agree that having more broad checking available in the language would be a great thing. However, I also understand the criticisms against it. What if, instead of specifying strict scalar types, like int, one could specify a type class (not in the OOP sense)? The concept has already been alluded to, but I don't think anyone has run with the idea yet. I'm thinking here of things like PHP's filter functions or the character classes in regular expressions. So you might specify a type of 'digits', which would allow anything that consists only of the numbers 0-9, or which could losslessly (by which I mean reversible to the same starting value) be cast to such a beast, equivalent to this monstrosity that I frequently find myself using:
if (!\ctype_digit((string)$parameterValue) {
...
}
(I think it was Stas that mentioned using is_numeric()
for things like this, but I find that function virtually useless since it unconditionally allows oddities like hex values that you typically don't want to allow. The other alternative, is_int()
, forces the very type of strict checking--and thus, calling-side casting--that we all wish to avoid.)
Allowing specifications of types that are more flexible than the base scalars would enable type checking but retain the advantages that a dynamic language offers.
That said, I suspect that no one is talking about this option because it's been discussed a million times in the past and deemed a bad and/or unworkable solution for whatever reasons. :-)
--
Bob Williams
Notice: This communication, including attachments, may contain information that is confidential. It constitutes non-public information intended to be conveyed only to the designated recipient(s). If the reader or recipient of this communication is not the intended recipient, an employee or agent of the intended recipient who is responsible for delivering it to the intended recipient, or if you believe that you have received this communication in error, please notify the sender immediately by return e-mail and promptly delete this e-mail, including attachments without reading or saving them in any manner. The unauthorized use, dissemination, distribution, or reproduction of this e-mail, including attachments, is prohibited and may be unlawful. If you have received this email in error, please notify us immediately by e-mail or telephone and delete the e-mail and the attachments (if any).
From: Will Fitch [mailto:will.fitch@gmail.com]
I would like to take this opportunity to query on a consensus:
Would you prefer to allow methods with type hinted return values to return null at will, or add a marker noting that it may return null?
Example: Return null at will
public ArrayIterator getIterator()
{
// something happened, will return null
return null;
}Example: Return only if identified as such
public ArrayIterator? getIterator()
{
return null;
}
I hate the syntax in the second example (using ?).
IMO allowing null should be the default unless specifically disallowed.
I far prefer the addition/use of a keyword ("notnull" or "null") to disallow (or allow) as opposed to random symbol abuse (potential incompatibilities notwithstanding).
John Crenshaw
Priacta, Inc.
2011/12/23 John Crenshaw johncrenshaw@priacta.com
From: Will Fitch [mailto:will.fitch@gmail.com]
I would like to take this opportunity to query on a consensus:
Would you prefer to allow methods with type hinted return values to
return null at will, or add a marker noting that it may return null?Example: Return null at will
public ArrayIterator getIterator()
{
// something happened, will return null
return null;
}Example: Return only if identified as such
public ArrayIterator? getIterator()
{
return null;
}I hate the syntax in the second example (using ?).
It looks strange, but easy to get used to. Two examples from C#:
public decimal? Grade { get; set; }
public Nullable<System.DateTime> Time { get; set; }
IMO allowing null should be the default unless specifically disallowed.
I disagree for the reasons mentioned by for instance Robert.
Type hints should be strict/explicit or not done at all.
Sent from my iPhone
2011/12/23 John Crenshaw johncrenshaw@priacta.com
From: Will Fitch [mailto:will.fitch@gmail.com]
I would like to take this opportunity to query on a consensus:
Would you prefer to allow methods with type hinted return values to
return null at will, or add a marker noting that it may return null?Example: Return null at will
public ArrayIterator getIterator()
{
// something happened, will return null
return null;
}Example: Return only if identified as such
public ArrayIterator? getIterator()
{
return null;
}I hate the syntax in the second example (using ?).
It looks strange, but easy to get used to. Two examples from C#:
public decimal? Grade { get; set; }
public Nullable<System.DateTime> Time { get; set; }
If it is decided that we continue down the road of limiting nullable
returns, and want to add an indicator, what about something like this:
public nullable ArrayIterator getIterator()
If the nullable token isn't declared, it can't return null.
IMO allowing null should be the default unless specifically disallowed.
I disagree for the reasons mentioned by for instance Robert.
Type hints should be strict/explicit or not done at all.
In the interest of providing options for an ability to mark a method as returning null, I have added a new patch here: http://www.willfitch.com/php/nullable.patch
This includes a new token "T_NULLABLE". Here are a few examples:
// This is allowed
private nullable ArrayIterator getIterator()
{
return null;
}
// This throws an E_RECOVERABLE_ERROR
private ArrayIterator getIterator()
{
return null;
}
The token/identifier can certainly change, but I want to provide the most options for the best solution.
2011/12/23 John Crenshaw johncrenshaw@priacta.com
From: Will Fitch [mailto:will.fitch@gmail.com]
I would like to take this opportunity to query on a consensus:
Would you prefer to allow methods with type hinted return values to return null at will, or add a marker noting that it may return null?
Example: Return null at will
public ArrayIterator getIterator()
{
// something happened, will return null
return null;
}Example: Return only if identified as such
public ArrayIterator? getIterator()
{
return null;
}I hate the syntax in the second example (using ?).
It looks strange, but easy to get used to. Two examples from C#:
public decimal? Grade { get; set; }
public Nullable<System.DateTime> Time { get; set; }
IMO allowing null should be the default unless specifically disallowed.
I disagree for the reasons mentioned by for instance Robert.
Type hints should be strict/explicit or not done at all.
In the interest of providing options for an ability to mark a method as
returning null, I have added a new patch here:
http://www.willfitch.com/php/nullable.patchThis includes a new token "T_NULLABLE". Here are a few examples:
// This is allowed
private nullable ArrayIterator getIterator()
{
return null;
}// This throws an
E_RECOVERABLE_ERROR
private ArrayIterator getIterator()
{
return null;
}The token/identifier can certainly change, but I want to provide the most
options for the best solution.
This looks fine to me, looks more php like then the C# examples.
2011/12/23 John Crenshaw johncrenshaw@priacta.com
From: Will Fitch [mailto:will.fitch@gmail.com]
I would like to take this opportunity to query on a consensus:
Would you prefer to allow methods with type hinted return values to
return null at will, or add a marker noting that it may return null?Example: Return null at will
public ArrayIterator getIterator()
{
// something happened, will return null
return null;
}Example: Return only if identified as such
public ArrayIterator? getIterator()
{
return null;
}I hate the syntax in the second example (using ?).
It looks strange, but easy to get used to. Two examples from C#:
public decimal? Grade { get; set; }
public Nullable<System.DateTime> Time { get; set; }
IMO allowing null should be the default unless specifically disallowed.
I disagree for the reasons mentioned by for instance Robert.
Type hints should be strict/explicit or not done at all.
For the record; This was not ment as an argument against scalar, and
similar type hints ( object, callable.. ) .
It was an argument against hinting about something and getting
something completely different (null).
Happy xmas!
In PHP, returning object if everything is OK and false if not is a very common pattern.
Also, you understand that always allowing null means that this construct:$foo = $this->returnsFoo();
$foo->fooMethod();is no longer safe as you wanted it to be when you made returnsFoo use strict typing? You'd have to check $foo anyway.
Are you suggesting not allowing null to be returned, or provide an
indicator within the syntax that it may return null?
It should be possible to allow it. Either with a generic syntax for
allowing
many types of return types or through a specific one (eg. in C# a ?
marks an internal type as nullable: int? )
PHP would be the
first language I'm aware of that would do so in either case.
It already barfs at you if you pass null to a parameter expecting a
given class
(though you can allow it).
The point isn't to restrict a type hint to always be returned. The
goal is to guarantee that if a value is returned, it will be of type
X. In the event it is inappropriate to return the value back, would
you rather receive null or a bogus class instance, empty array or a
random function name back? Because that's what will happen. It will
become the "workaround" for not being able to return null.
As it would support both ways, it's up to the framework to choose if
they want to
return null or a fake-class interface.
In PHP, returning object if everything is OK and false if not is a very common pattern.
Also, you understand that always allowing null means that this construct:$foo = $this->returnsFoo();
$foo->fooMethod();is no longer safe as you wanted it to be when you made returnsFoo use strict typing? You'd have to check $foo anyway.
Are you suggesting not allowing null to be returned, or provide an
indicator within the syntax that it may return null?
It should be possible to allow it. Either with a generic syntax for
allowing
many types of return types or through a specific one (eg. in C# a ?
marks an internal type as nullable: int? )
You can return null from methods. Parameters and variables are nullable. I'm not against appending an indicator as to whether null may be returned, but that would need to be a general consensus, I think.
I would like other opinions on this...
PHP would be the
first language I'm aware of that would do so in either case.
It already barfs at you if you pass null to a parameter expecting a
given class
(though you can allow it).The point isn't to restrict a type hint to always be returned. The
goal is to guarantee that if a value is returned, it will be of type
X. In the event it is inappropriate to return the value back, would
you rather receive null or a bogus class instance, empty array or a
random function name back? Because that's what will happen. It will
become the "workaround" for not being able to return null.
As it would support both ways, it's up to the framework to choose if
they want to
return null or a fake-class interface.
Yes, it would. But providing the option of null would be a good incentive not to do that. At the end of the day, you're giving a set of tools to someone. If they choose to use it in a way that's less efficient, or wrong in other's eyes, that's their choice.
Sent from my iPad
Your examples only show class methods with visibility qualifyiers, and
looking at the changes to zend_language_parser.y
it seems as if would only be available for methods. Wouldn't return
hints be available for plain functions?Right now, it's only available for methods. Personally, I'm not a fan
of return types for functions. Adding this for functions in its form
today will also introduce shift/reduce issues.
IMO, it should work for normal functions too.
In functional programming, it is common to return nullable types:
returns an instance of class Foo or null, an array or false, etc.This is supported in normal arguments with the "= null" hint, and I
think there should be something equivalent for return types.
Does the proposed implementation allow null everywhere? (there's an
example of returning null from a return hint of array)
That seems wrong to me. Unless there's a way to make it strict (eg.
array! for only arrays).Most modern languages allow returning null in any case. This is a hail
Mary in the event something happens, but throwing an exception is
inappropriate. I see no reason to diverge from that.
I tend to agree with that one.
cheers,
Derick
--
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Sent from my iPad
Your examples only show class methods with visibility qualifyiers, and
looking at the changes to zend_language_parser.y
it seems as if would only be available for methods. Wouldn't return
hints be available for plain functions?
Right now, it's only available for methods. Personally, I'm not a fan
of return types for functions. Adding this for functions in its form
today will also introduce shift/reduce issues.
Yes, it's much easier for the parser for methods with explicit
visibility (note that public is optional).
I'm ok on requiring the public for methods with a return hint, but if
methods can get return hints,
normal functions must, too.
If it's hard with your proposed syntax, it should be discarded and a
better one developed.
Else we would end up with different syntaxes for return hints depending
on the kind of function.
For instance, the return hint could go at the end of the prototype:
function foo (Class1 $a, Class2 $b) => Class3 {
return new Class3($a, $b);
}
(I'm unsure about the T_DOUBLE_ARROW, although for parsing, I feel there
should be some token there
before the class name, though I'm unconvinced on which)
In functional programming, it is common to return nullable types:
returns an instance of class Foo or null, an array or false, etc.This is supported in normal arguments with the "= null" hint, and I
think there should be something equivalent for return types.
Does the proposed implementation allow null everywhere? (there's an
example of returning null from a return hint of array)
That seems wrong to me. Unless there's a way to make it strict (eg.
array! for only arrays).
Most modern languages allow returning null in any case. This is a hail
Mary in the event something happens, but throwing an exception is
inappropriate. I see no reason to diverge from that.
I disagree with you, but Stas already provided a compelling case.
(I'm unsure about the T_DOUBLE_ARROW, although for parsing, I feel there
should be some token there
before the class name, though I'm unconvinced on which)
What about this?
function foo (Class1 $a, Class2 $b) return Class3 {
/* Do something */
return new Class3($a, $b);
}
I will update to take functions into consideration. Will let you know when the RFC/patch reflect it.
(I'm unsure about the T_DOUBLE_ARROW, although for parsing, I feel there
should be some token there
before the class name, though I'm unconvinced on which)What about this?
function foo (Class1 $a, Class2 $b) return Class3 {
/* Do something */
return new Class3($a, $b);
}
Sent from my iPad
Your examples only show class methods with visibility qualifyiers, and
looking at the changes to zend_language_parser.y
it seems as if would only be available for methods. Wouldn't return
hints be available for plain functions?
Right now, it's only available for methods. Personally, I'm not a fan
of return types for functions. Adding this for functions in its form
today will also introduce shift/reduce issues.
Yes, it's much easier for the parser for methods with explicit
visibility (note that public is optional).
I'm ok on requiring the public for methods with a return hint, but if
methods can get return hints,
normal functions must, too.If it's hard with your proposed syntax, it should be discarded and a
better one developed.
Else we would end up with different syntaxes for return hints depending
on the kind of function.
It's not a matter of being difficult. I can see your argument, however.
For instance, the return hint could go at the end of the prototype:
function foo (Class1 $a, Class2 $b) => Class3 {
return new Class3($a, $b);
}(I'm unsure about the T_DOUBLE_ARROW, although for parsing, I feel there
should be some token there
before the class name, though I'm unconvinced on which)
Let me see what can be done without doing too much to the definitions. I like the idea of not making "function" required.
In functional programming, it is common to return nullable types:
returns an instance of class Foo or null, an array or false, etc.This is supported in normal arguments with the "= null" hint, and I
think there should be something equivalent for return types.
Does the proposed implementation allow null everywhere? (there's an
example of returning null from a return hint of array)
That seems wrong to me. Unless there's a way to make it strict (eg.
array! for only arrays).
Most modern languages allow returning null in any case. This is a hail
Mary in the event something happens, but throwing an exception is
inappropriate. I see no reason to diverge from that.
I disagree with you, but Stas already provided a compelling case.