RE: Type hints with null default values

php.internals

Cristiano Duarte

21 years ago
Marcus Boerger wrote:
> Hello Robert, > > Tuesday, October 19, 2004, 10:20:59 PM, you wrote: > >> The issues surrounding this seemed to have been muddied up a little, I'll >> try to clear them up. > >> I see two different sets of functionality that people are asking for. > >> #1. The ability to pass null on a type hinted param (but still a >> #mandatory >> param) > >> #2. The ability to define an optional type hinted param. > >> I see it as a very important distinction to make. Using the #2 to solve >> #1 is not the solution PHP should implement. Using some method which >> operates on the class hint itself is the right solution. > >> For example, using the [] method: > >> public function Compare([BaseClass] $objA, $cmpFunc); > > As far as we disallow this and only allow optional types at the end i'd > agre to this. Example: > > public function Compare($cmpFunc, [BaseClass] $objA); >
I don't see any danger if we allow null with this syntax:  public function Compare([BaseClass] $objA, $cmpFunc); if we don't allow null with this syntax:  public function Compare(BaseClass $objA, $cmpFunc); and if we make an argument optional with this syntax:  public function Compare($cmpFunc, BaseClass $objA=null); If someone (like me) has thousands lines of code that uses 'null' meaning 'null reference' just need to use syntax #1. If someone is very strict and don't even think about using null in typehinted arguments, because null is not a value it's a type and must be used as so, just need to use syntax #2. If someone wants to make typehinted parameters optional, just use syntax #3. I really don't see any problem allowing this three syntaxes(but I'm not very clever since I'm for syntax #1) :-P Regards, Cristiano Duarte

Cristiano Duarte

21 years ago
So reading all posts on this subject, the proposal would be: 1) Allow null typehints public function Compare([BaseClass] $objA, $cmpFunc); Ex: Compare(null, 'func1'); 2) Don't allow null typehints public function Compare(BaseClass $objA, $cmpFunc); Ex: Compare(new BaseClass(), 'func1'); 3) Optional argument (with typehint) public function Compare($cmpFunc, BaseClass $objA=null); Ex: Compare('func1'); Am I right ? Is it possible to implement all three syntaxes ? IMH it would satisfy everybody's needs. Regards, Cristiano Duarte

Marcus Börger

21 years ago
Hello Cristiano, Wednesday, October 27, 2004, 2:21:03 AM, you wrote:
> So reading all posts on this subject, the proposal would be:
> 1) Allow null typehints > public function Compare([BaseClass] $objA, $cmpFunc);
see below.
> 2) Don't allow null typehints > public function Compare(BaseClass $objA, $cmpFunc);
> Ex: Compare(new BaseClass(), 'func1');
> 3) Optional argument (with typehint) > public function Compare($cmpFunc, BaseClass $objA=null);
This is no option because BaseClass typehint doesn't allow null and if we have a dedicated syntax for default values we don't need this. If at all this would be: 4) public function Compare($cmpFunc, [BaseClass] $objA=null); But first we need to know if we really want to allow passing null with default typehinted values. Since there is no concept of null references in PHP. Having a value of null to mark not passed is a nice differentiation. So for now the only addition we may probably consider for 5.1 is adding 4: optional typehinted values that default to null and only null. marcus

Christian Schneider

21 years ago
Marcus Boerger wrote:
> So for now the only addition we may probably consider for 5.1 is > adding 4: optional typehinted values that default to null and only > null.
Please don't add another parameter syntax (especially with line-noi... err special characters) like [BaseClass]. This would make PHP more unreadable than it needs to be for a minor feature. To allow foo(BaseClass $objA = null) to also accept foo(null); handles the few cases where you want to pass null nicely IMHO. Advocating to only change the PHP syntax to simplify it, - Chris

Curt Zirzow

21 years ago
* Thus wrote Christian Schneider:
> Please don't add another parameter syntax (especially with line-noi... > err special characters) like [BaseClass]. This would make PHP more > unreadable than it needs to be for a minor feature. To allow > foo(BaseClass $objA = null) to also accept foo(null); handles the few > cases where you want to pass null nicely IMHO.
I'd have to agree, that changing the syntax on how to pass optional class type hints, is probably not a good thing to do. It has been traditional to set a parameter to default by assigning a value to the paramater, and the rule always has been what kind of value can be assigned. Curt
-- Quoth the Raven, "Nevermore."

Sebastian Bergmann

21 years ago
Marcus Boerger wrote:
> So for now the only addition we may probably consider for 5.1 is > adding 4: optional typehinted values that default to null and only > null.
That sounds like the most sane way to do it.
-- Sebastian Bergmann http://www.sebastian-bergmann.de/ GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69

Andi Gutmans

21 years ago
At 10:53 AM 10/27/2004 +0200, Christian Schneider wrote:
>Marcus Boerger wrote: >>So for now the only addition we may probably consider for 5.1 is >>adding 4: optional typehinted values that default to null and only >>null. > >Please don't add another parameter syntax (especially with line-noi... err >special characters) like [BaseClass]. This would make PHP more unreadable >than it needs to be for a minor feature. To allow foo(BaseClass $objA = >null) to also accept foo(null); handles the few cases where you want to >pass null nicely IMHO. > >Advocating to only change the PHP syntax to simplify it,
Yeah I tend to agree. Adding [] for a not that popular case seems not to be worth it. It just adds more complexity to the syntax. I suggest to either leave as is or to support default values of NULL (only) and if that is given then you may also pass NULL (give it a double meaning). Andi

Cristiano Duarte

21 years ago
Andi Gutmans wrote:
> At 10:53 AM 10/27/2004 +0200, Christian Schneider wrote: >>Advocating to only change the PHP syntax to simplify it, > > Yeah I tend to agree. Adding [] for a not that popular case seems not to > be worth it. It just adds more complexity to the syntax. > I suggest to either leave as is or to support default values of NULL > (only) and if that is given then you may also pass NULL (give it a double > meaning).
Will be possible to implement this "null default value" with "double meaning" for 5.1? Just as information, since it would help me a lot. Cristiano Duarte

Unnamed Person

21 years ago
What was the result of this discussion? Thanks On 27 Oct 2004, at 12:50, Andi Gutmans wrote:

Marcus Börger

21 years ago
Hello Cristiano, Friday, October 22, 2004, 7:41:24 PM, you wrote:
> If someone (like me) has thousands lines of code that uses 'null' meaning > 'null reference' just need to use syntax #1.
Then you are simply using your PHP tool wrong. PHP is not Java where you need (and have) null references. Best regards, Marcus mailto:helly@php.net