# [RFC] Scalar Type Hints v0.2

- Thread: https://externals.io/message/80442
- Original archive: https://news-web.php.net/php.internals/80442
- Started by Andrea Faulds on 2015-01-14T00:16:52+00:00
- Messages: 147
- RFC: Scalar Type Hints (Withdrawn) — https://externals.io/rfc/scalar_type_hints.md
- Page 1 of 6: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/80442.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #80442 — Andrea Faulds — 2015-01-14T00:16:52+00:00

URL: https://externals.io/message/80442#80442

Good evening,

I’ve made some quite significant changes to my Scalar Type Hints RFC, and bumped its version to 0.2.

Here: https://wiki.php.net/rfc/scalar_type_hints

This is a new thread because I’ve made a significant revision to the RFC, so it’d be sensible to separate discussion of the updated RFC from the v0.1 RFC.

Please tell me your thoughts.

Thanks!
--
Andrea Faulds
http://ajf.me/

---

## #80445 — Robert Stoll — 2015-01-14T08:22:42+00:00

URL: https://externals.io/message/80442#80445 · In reply to: #80442

Hi Andrea

> -----Ursprüngliche Nachricht-----
> Von: Andrea Faulds [mailto:ajf@ajf.me]
> Gesendet: Mittwoch, 14. Januar 2015 01:17
> An: PHP Internals List
> Betreff: [PHP-DEV] [RFC] Scalar Type Hints v0.2
> 
> Good evening,
> 
> I’ve made some quite significant changes to my Scalar Type Hints RFC, and bumped its version to 0.2.
> 
> Here: https://wiki.php.net/rfc/scalar_type_hints
> 
> This is a new thread because I’ve made a significant revision to the RFC, so it’d be sensible to separate discussion of the
> updated RFC from the v0.1 RFC.
> 
> Please tell me your thoughts.
> 
> Thanks!
> --
> Andrea Faulds
> http://ajf.me/
> 
> --
>  

I had a few thoughts on the new proposed declare(strict_typehints=TRUE); construct and I must say I do not really like that we would have different behaviour just based on a directive. This is quite ugly from a readability point of view since I as user cannot easily see whether somewhere (above) in the code the directive was set (I have changed my mind a little bit below - left it here for traceability purposes). Which brings me to the question what happens with the following code:

function foo(int $a){}  //is weak right?
declare(strict_typehinting=TRUE);


I guess it is weak since you wrote: "If this boolean value is TRUE, strict type-checking mode is used for function calls in the remainder of the file"
Maybe it is a good idea to add such an example to the examples as well.

Now, having rethought my own email, I think we should enforce some code style to avoid a huge mess. Why not declare that the directive needs to be the very first statement inside a namespace scope (unless the directive block syntax is used). This way it would be easily verifiable for the user if strict mode is in place or not. But then again, we could also argue that is up to code conventions/code guidelines and they should specify whether mixing both modes is ok and where the directive has to be placed. So re-rethinking what I just wrote I would say it is fine as it is now but we should at least consider if we want to enforce some code practice to avoid a mess.

Now another point: "Whether or not the function being called was declared in a file that uses strict or weak type checking is entirely irrelevant. The type checking mode depends on the file where the function is called."
Seems quite strange to me but fair enough, why not -- actually makes perfectly sense for this RFC -> leaving it to the user to decide what he/she likes :-)

---

## #80446 — Andrea Faulds — 2015-01-14T08:48:19+00:00

URL: https://externals.io/message/80442#80446 · In reply to: #80445

Hey Robert,

> 
> 
> I had a few thoughts on the new proposed declare(strict_typehints=TRUE); construct and I must say I do not really like that we would have different behaviour just based on a directive. This is quite ugly from a readability point of view since I as user cannot easily see whether somewhere (above) in the code the directive was set (I have changed my mind a little bit below - left it here for traceability purposes). Which brings me to the question what happens with the following code:
> 
> function foo(int $a){}  //is weak right?
> declare(strict_typehinting=TRUE);
> 
> 
> I guess it is weak since you wrote: "If this boolean value is TRUE, strict type-checking mode is used for function calls in the remainder of the file"
> Maybe it is a good idea to add such an example to the examples as well.

Yes, it only affects stuff which comes after it. But in the example you just gave it doesn’t do anything, because the type checking mode is about the caller (where the function is called), not the callee (where the function is defined).

A better example:

<?php
foo(1.0); // weak
declare(strict_typehints=TRUE);
foo(1); // strict

> Now, having rethought my own email, I think we should enforce some code style to avoid a huge mess. Why not declare that the directive needs to be the very first statement inside a namespace scope (unless the directive block syntax is used). This way it would be easily verifiable for the user if strict mode is in place or not. But then again, we could also argue that is up to code conventions/code guidelines and they should specify whether mixing both modes is ok and where the directive has to be placed. So re-rethinking what I just wrote I would say it is fine as it is now but we should at least consider if we want to enforce some code practice to avoid a mess.

We could possible require it to be at the top of the file, maybe. Though I expect people will probably do this anyway. I’m sure it’ll become part of PSR-whatever and there’ll be some sort of standard.

By the way, you can mark some blocks as having different behaviour, e.g.:

<?php
declare(strict_typehints=TRUE) {
    foo(1);
}
declare(strict_typehints=FALSE) {
    bar(2);
}

This way, it’s more obvious that they have different behaviour.

> Now another point: "Whether or not the function being called was declared in a file that uses strict or weak type checking is entirely irrelevant. The type checking mode depends on the file where the function is called."
> Seems quite strange to me but fair enough, why not -- actually makes perfectly sense for this RFC -> leaving it to the user to decide what he/she likes :-) 

That’s the idea. I didn’t like the idea of having an API force you to deal with a particular approach, especially since it’d mean some of your calls would be strict, others weak. This way it’s completely consistent: everything is strict, or everything is weak, and you get to choose. :)

Thanks for your comments.
--
Andrea Faulds
http://ajf.me/

---

## #80447 — Leigh — 2015-01-14T09:17:12+00:00

URL: https://externals.io/message/80442#80447 · In reply to: #80442

> Good evening,
>
> I’ve made some quite significant changes to my Scalar Type Hints RFC, and bumped its version to 0.2.
>
> Here: https://wiki.php.net/rfc/scalar_type_hints
>
> This is a new thread because I’ve made a significant revision to the RFC, so it’d be sensible to separate discussion of the updated RFC from the v0.1 RFC.
>
> Please tell me your thoughts.
>

I really don't like this behaviour being changed at the call site. If
I design a function that I _know_ should only take a string, then I
want it to be an error if the user supplies anything else, so that
they know they messed up.

Also, there's this, that you might be able to re-purpose.
https://github.com/php/php-src/blob/d5dd29/Zend/zend_compile.c#L7199

---

## #80449 — Andrea Faulds — 2015-01-14T09:32:42+00:00

URL: https://externals.io/message/80442#80449 · In reply to: #80447

Hi Leigh,

> 
> 
> I really don't like this behaviour being changed at the call site. If
> I design a function that I _know_ should only take a string, then I
> want it to be an error if the user supplies anything else, so that
> they know they messed up.

I don’t like the idea of being forced to use strict (or weak) type checking because the API author decided as much.

More importantly, having the function and not its caller decide makes the addition of type hints to existing code difficult, because it will (possibly unintentionally) rely on PHP’s weak typing and thus error when faced with a strict type check. On the other hand, having the callee decide means that you can relatively safely add type hints to existing code and its consumers will not error. You can then gradually migrate that consuming code over to strict type-checking, if you so wish.

> Also, there's this, that you might be able to re-purpose.
> https://github.com/php/php-src/blob/d5dd29/Zend/zend_compile.c#L7199

I don’t want to get rid of that easter egg, and I prefer the declare() syntax since it’s more obviously some sort of directive (use strict; looks like an innocuous use statement) and supports the block syntax.

--
Andrea Faulds
http://ajf.me/

---

## #80450 — Zeev Suraski — 2015-01-14T09:41:08+00:00

URL: https://externals.io/message/80442#80450 · In reply to: #80449

> -----Original Message-----
> From: Andrea Faulds [mailto:ajf@ajf.me]
> Sent: Wednesday, January 14, 2015 11:33 AM
> To: Leigh
> Cc: PHP Internals List
> Subject: Re: [PHP-DEV] [RFC] Scalar Type Hints v0.2
>
> Hi Leigh,
>
> > […]
>
> I don’t like the idea of being forced to use strict (or weak) type
> checking
> because the API author decided as much.

I don't either.  But I don't like the user to do it either, it's something
that is a part of the language definition.

I completely agree with both Robert and Leigh.  I liked the v0.1 one, but
v0.2 is DOA from my point of view.  Arguably, from my POV, it's the one
option that's even worse than having strict typing exclusively.

Zeev

---

## #80452 — Thomas Nunninger — 2015-01-14T10:08:43+00:00

URL: https://externals.io/message/80442#80452 · In reply to: #80449

Hi,


> Hi Leigh,
>
> > […]
>
> I don’t like the idea of being forced to use strict (or weak) type checking because the API author decided as much.

What happens with that code:

----------------
$i = 1;
$a = myFunc( $i );

declare(strict_typehints=TRUE);

function myFunc( float $f )
{
     return otherFunc( $f );
}

function otherFunc( float $f )
{
     ...
}
----------------

As author of strict code I need to replace

     return otherFunc( $f );

by

     return otherFunc( (float) $f );


I'm not sure

- if this is what a strict coder wants and

- if you find an acceptable way to test your strict code if it works 
with non-strict code.


Regards

Thomas

---

## #80453 — Julien Pauli — 2015-01-14T10:14:03+00:00

URL: https://externals.io/message/80442#80453 · In reply to: #80442

> Good evening,
>
> I’ve made some quite significant changes to my Scalar Type Hints RFC, and
> bumped its version to 0.2.
>
> Here: https://wiki.php.net/rfc/scalar_type_hints
>
> This is a new thread because I’ve made a significant revision to the RFC,
> so it’d be sensible to separate discussion of the updated RFC from the v0.1
> RFC.
>
> Please tell me your thoughts.
>
> Thanks!
> --
> Andrea Faulds
> http://ajf.me/


Hello,

Using declare() IMO, is a PITA.
Everything that can be done without the use of declare(), must be done
without declare().

I would have prefered different syntax, like the ones we disccussed many
years ago , having strict checks with the foo(string $a) syntax, and
non-strict check with the foo((string) $a) syntax.

Julien.P

---

## #80454 — Andrea Faulds — 2015-01-14T10:20:00+00:00

URL: https://externals.io/message/80442#80454 · In reply to: #80452

Hi Thomas,

> 
> 
> ----------------
> $i = 1;
> $a = myFunc( $i );
> 
> declare(strict_typehints=TRUE);
> 
> function myFunc( float $f )
> {
>    return otherFunc( $f );
> }
> 
> function otherFunc( float $f )
> {
>    ...
> }
> ----------------
> 
> As author of strict code I need to replace
> 
>    return otherFunc( $f );
> 
> by
> 
>    return otherFunc( (float) $f );
> 
> 
> I'm not sure
> 
> - if this is what a strict coder wants and
> 
> - if you find an acceptable way to test your strict code if it works with non-strict code.

I don’t understand, I’m sorry.

If you are using declare(strict_typehints=TRUE); then all calls in a file are “strict”. If you are not, all calls in a file are “weak”.

--
Andrea Faulds
http://ajf.me/

---

## #80455 — Andrea Faulds — 2015-01-14T10:22:27+00:00

URL: https://externals.io/message/80442#80455 · In reply to: #80453

Hi Julien,

> 
> 
> Using declare() IMO, is a PITA.
> Everything that can be done without the use of declare(), must be done without declare().
> 
> I would have prefered different syntax, like the ones we disccussed many years ago , having strict checks with the foo(string $a) syntax, and non-strict check with the foo((string) $a) syntax.

Actually, this approach came about mostly to avoid that. I don’t really like the idea of mixing and matching weak and strict type checks. Having some functions using (int) and (string), others use int and string, and others still use both, would be confusing. Plus, it would mean that you’d be forced to follow the personal preference of the API authors.

With this RFC, yes, you have to put an extra line at the top of the file. But it has the advantage that everything follows the same behaviour, even extension/built-in functions. That’s what I like about it.

--
Andrea Faulds
http://ajf.me/

---

## #80456 — Andrey Andreev — 2015-01-14T11:10:50+00:00

URL: https://externals.io/message/80442#80456 · In reply to: #80455

Hello,


>
> Hi Julien,
>
> > […]
>
> Actually, this approach came about mostly to avoid that. I don’t really like the idea of mixing and matching weak and strict type checks. Having some functions using (int) and (string), others use int and string, and others still use both, would be confusing. Plus, it would mean that you’d be forced to follow the personal preference of the API authors.
>
> With this RFC, yes, you have to put an extra line at the top of the file. But it has the advantage that everything follows the same behaviour, even extension/built-in functions. That’s what I like about it.

I don't understand why it should be a bad thing that the API author
forces rules on the consumer. The opposite is IMO fundamental to the
concept of an API - the rules specified by the author are a contract
that the consumer agrees to, and which allows for the whole thing to
work properly.

In this form, I don't get any benefit from strict type checks, because
I only want them so that writing foo(int $bar) guarantees me that the
caller *passed* an integer. Instead, I'd have to NOT use a type hint,
and instead do an is_int($bar) check inside ... this beats the whole
purpose.

This caller-controlled switch very much reminds me of
mbstring.func_overload, which I didn't care much for, until one day I
saw how it breaks all crypto code that authors had otherwise written
very carefully.

I prefer Julien's (int) $weak vs. int $strong syntax better (which is
what I've always wanted), but I don't care much if it's a declare()
statement, just as long as the author controls the behavior.
And while I'm on that - thank you for this proposal (specifically the
0.2 version), Andrea. Finally an RFC that allows both weak and strict
scalar type checks instead of the "can't go in both directions"
nonsense that has blocked this feature for years.

Cheers,
Andrey.

---

## #80457 — Andrea Faulds — 2015-01-14T11:21:45+00:00

URL: https://externals.io/message/80442#80457 · In reply to: #80456

Hi Andrey,

> 
> 
> I don't understand why it should be a bad thing that the API author
> forces rules on the consumer. The opposite is IMO fundamental to the
> concept of an API - the rules specified by the author are a contract
> that the consumer agrees to, and which allows for the whole thing to
> work properly.
> 
> In this form, I don't get any benefit from strict type checks, because
> I only want them so that writing foo(int $bar) guarantees me that the
> caller *passed* an integer. Instead, I'd have to NOT use a type hint,
> and instead do an is_int($bar) check inside ... this beats the whole
> purpose.

Why? What is wrong with a caller choosing to use weak type hints? Either way you receive an integer.

> This caller-controlled switch very much reminds me of
> mbstring.func_overload, which I didn't care much for, until one day I
> saw how it breaks all crypto code that authors had otherwise written
> very carefully.
> 
> I prefer Julien's (int) $weak vs. int $strong syntax better (which is
> what I've always wanted), but I don't care much if it's a declare()
> statement, just as long as the author controls the behavior.
> And while I'm on that - thank you for this proposal (specifically the
> 0.2 version), Andrea. Finally an RFC that allows both weak and strict
> scalar type checks instead of the "can't go in both directions"
> nonsense that has blocked this feature for years.

I don’t like going in both directions if I’ll have to deal with both at the same time. I want consistency. Either everything weak or everything strict. That’s why I like this approach. At least within a single file, everything will follow the same approach, rather than it being mix-and-match.

--
Andrea Faulds
http://ajf.me/

---

## #80458 — Andrey Andreev — 2015-01-14T11:37:18+00:00

URL: https://externals.io/message/80442#80458 · In reply to: #80457

Hi again,


> Hi Andrey,
>
> > […]
>
> Why? What is wrong with a caller choosing to use weak type hints? Either way you receive an integer.

Because it's pointless. Why would I want strict checks if I only cared
for getting an integer? The whole point is that I care for what the
caller passed, that I want specifically a check instead of a cast.

Cheers,
Andrey.

---

## #80459 — Thomas Nunninger — 2015-01-14T13:04:05+00:00

URL: https://externals.io/message/80442#80459 · In reply to: #80454

Hi Andrea,


> Hi Thomas,
>
> > […]
>
> I don’t understand, I’m sorry.
>
> If you are using declare(strict_typehints=TRUE); then all calls in a file are “strict”. If you are not, all calls in a file are “weak”.


Sorry, if my mail was not clear. My point was: If I write a library in 
strict mode and someone else is using it from his non-strict mode, he 
can pass an integer to myFunc() without an error. If I use this integer 
in my library and hand it over to otherFunc() (in my library) this will 
fail as integer is not accepted for float.

Or did I misunderstood the RFC and there is a casting of the integer to 
a float when calling myFunc()?

Regards

Thomas

---

## #80460 — Andrea Faulds — 2015-01-14T13:15:07+00:00

URL: https://externals.io/message/80442#80460 · In reply to: #80459

Hi Thomas,

> 
> 
> Sorry, if my mail was not clear. My point was: If I write a library in strict mode and someone else is using it from his non-strict mode, he can pass an integer to myFunc() without an error. If I use this integer in my library and hand it over to otherFunc() (in my library) this will fail as integer is not accepted for float.
> 
> Or did I misunderstood the RFC and there is a casting of the integer to a float when calling myFunc()?

The value would be casted, yes. From the function author’s perspective, there’s no need to worry about strict vs. weak typing: in both cases, you get the type you asked for.

Thanks.

--
Andrea Faulds
http://ajf.me/

---

## #80461 — Richard Quadling — 2015-01-14T13:22:44+00:00

URL: https://externals.io/message/80442#80461 · In reply to: #80450

> > […]
>
> I don't either.  But I don't like the user to do it either, it's something
> that is a part of the language definition.
>
> I completely agree with both Robert and Leigh.  I liked the v0.1 one, but
> v0.2 is DOA from my point of view.  Arguably, from my POV, it's the one
> option that's even worse than having strict typing exclusively.
>
> Zeev


The issue of scalar type hinting has been around for a while, with various
proposals and RFCs, but no consensus. Each time a certain level of progress
is made, more issues arise.

It would seem that the major issues is that whatever a library/framework
developer decides, the user of that library/framework would have to develop
their application to fit. Is this such a bad thing? The comeback to that is
that PHP supports type juggling, so there is a different mechanism for
non-scalars to scalars. Fair enough.


From the lib developer's perspective, if a parameter to a function/method
is going to be treated as an integer and is expected to be an integer, then
they should be able to force/limit this just like they can when they say it
must be an array/callable/class/interface. But take into account the option
to enforce the type juggling.


I get 3 questions from this.

1 - Should the type hint restrict non matching types?
2 - When should the value be juggled to match the type hint if it is
different?
3 - Could we allow different signatures for the same method name? - A
different question entirely, but one that may be worth having in relation
to this proposal.


Whatever mechanism is used, it needs to be of zero effort for the user, but
fully understood of the effect.


I think to answer all of this, and to provide enough flexibility that
everyone can be made happy and understand the effect, I think the issues
could be resolved with the following.


1 - A type hint of 'scalar' is required.

This excludes other types (array/callable/class/interface) and would
specifically mean that no type juggling will take place as part of the
parameter passing.

If no type juggling is going to take place, why does it matter what the
type hint is? The advantage of having 'scalar' as a type hint is, as I
said, excludes non-scalars.

Now, if the method does something to the value and the value is passed by
reference, then that would need to be documented clearly.



2 - A scalar type type hint will automatically juggle. PHP is a dynamic
language and, as such, type juggling is used throughout. It shouldn't be
any different when a parameter is type hinted for scalars.



The frustration seems to be that without a way to say that this scalar will
NOT be type juggled (loose) or that it will be (strong) then we cannot move
forward.



Now, some of the comments related to this proposal (and others) is what to
do when the scalar type is of a different type to the parameter.

I think I've covered this by the use of the type hint 'scalar'.

Scalars get type juggled. Period. For good or bad. Until PHP becomes a
strongly type language, type juggling exists.

With the hinting, we can let users know that type juggling will now also
apply to parameters.

So.

<?php
// Any type.
function anyType($a){}

// Current types.
function anArray(array $b){}
function aCallable(callable $c){}
function aClass(stdclass $d){}
function anInterface(Countable $e){}

// Any scalar - not juggled.
function anyScalar(scalar $f){}

// Any scalar - juggled
function willBeJuggledToBool(bool $g){}
function willBeJuggledToString(string $h){}
function willBeJuggledToFloat(float $i){}
function willBeJuggledToInteger(integer $j){}
?>

Considering that there are no errors generated when type juggling of
scalars take place, there should be no errors when type juggling scalar
type hints for parameters.

In essence, if you are coding with scalars (ignore type hinting) and
relying on PHP to do the right thing when type juggling takes place, then
the same level of acceptance should be used when type juggling of scalar
parameters.


TL;DR; Rejecting calls to a scalar type typehinted function because the
value is of the wrong type doesn't fit with PHP's type juggling.

-- 
Richard Quadling

---

## #80462 — Zeev Suraski — 2015-01-14T13:35:55+00:00

URL: https://externals.io/message/80442#80462 · In reply to: #80461

I don’t think we’re ever going to get consensus.  But judging by the
feedback to the v0.1 version, I tend to disagree that the opposers would
have blocked it.  There were certainly opposers – but not that many of them
as far as I could tell.  I think it stood a good chance to pass at a 2/3.
Unlike strict typing – we didn’t even go to a vote on it, which I think is
unfortunate (and should be changed, before changing course completely as
this v0.2 suggests).



We’re definitely not going to have consensus on introducing both options as
per this RFC.  I for one think it’s the worst possible option.



Zeev





*From:* Richard Quadling [mailto:rquadling@gmail.com]
*Sent:* Wednesday, January 14, 2015 3:23 PM
*To:* Zeev Suraski
*Cc:* Andrea Faulds; Leigh; PHP Internals List
*Subject:* Re: [PHP-DEV] [RFC] Scalar Type Hints v0.2









> -----Original Message-----
> From: Andrea Faulds [mailto:ajf@ajf.me]
> Sent: Wednesday, January 14, 2015 11:33 AM
> To: Leigh
> Cc: PHP Internals List
> Subject: Re: [PHP-DEV] [RFC] Scalar Type Hints v0.2
>
> Hi Leigh,
>
> > […]
>
> I don’t like the idea of being forced to use strict (or weak) type
> checking
> because the API author decided as much.

I don't either.  But I don't like the user to do it either, it's something
that is a part of the language definition.

I completely agree with both Robert and Leigh.  I liked the v0.1 one, but
v0.2 is DOA from my point of view.  Arguably, from my POV, it's the one
option that's even worse than having strict typing exclusively.

Zeev



The issue of scalar type hinting has been around for a while, with various
proposals and RFCs, but no consensus. Each time a certain level of progress
is made, more issues arise.



It would seem that the major issues is that whatever a library/framework
developer decides, the user of that library/framework would have to develop
their application to fit. Is this such a bad thing? The comeback to that is
that PHP supports type juggling, so there is a different mechanism for
non-scalars to scalars. Fair enough.





From the lib developer's perspective, if a parameter to a function/method
is going to be treated as an integer and is expected to be an integer, then
they should be able to force/limit this just like they can when they say it
must be an array/callable/class/interface. But take into account the option
to enforce the type juggling.





I get 3 questions from this.



1 - Should the type hint restrict non matching types?

2 - When should the value be juggled to match the type hint if it is
different?

3 - Could we allow different signatures for the same method name? - A
different question entirely, but one that may be worth having in relation
to this proposal.





Whatever mechanism is used, it needs to be of zero effort for the user, but
fully understood of the effect.





I think to answer all of this, and to provide enough flexibility that
everyone can be made happy and understand the effect, I think the issues
could be resolved with the following.





1 - A type hint of 'scalar' is required.



This excludes other types (array/callable/class/interface) and would
specifically mean that no type juggling will take place as part of the
parameter passing.



If no type juggling is going to take place, why does it matter what the
type hint is? The advantage of having 'scalar' as a type hint is, as I
said, excludes non-scalars.



Now, if the method does something to the value and the value is passed by
reference, then that would need to be documented clearly.







2 - A scalar type type hint will automatically juggle. PHP is a dynamic
language and, as such, type juggling is used throughout. It shouldn't be
any different when a parameter is type hinted for scalars.







The frustration seems to be that without a way to say that this scalar will
NOT be type juggled (loose) or that it will be (strong) then we cannot move
forward.







Now, some of the comments related to this proposal (and others) is what to
do when the scalar type is of a different type to the parameter.



I think I've covered this by the use of the type hint 'scalar'.



Scalars get type juggled. Period. For good or bad. Until PHP becomes a
strongly type language, type juggling exists.



With the hinting, we can let users know that type juggling will now also
apply to parameters.



So.



<?php

// Any type.

function anyType($a){}



// Current types.

function anArray(array $b){}

function aCallable(callable $c){}

function aClass(stdclass $d){}

function anInterface(Countable $e){}



// Any scalar - not juggled.

function anyScalar(scalar $f){}



// Any scalar - juggled

function willBeJuggledToBool(bool $g){}

function willBeJuggledToString(string $h){}

function willBeJuggledToFloat(float $i){}

function willBeJuggledToInteger(integer $j){}

?>



Considering that there are no errors generated when type juggling of
scalars take place, there should be no errors when type juggling scalar
type hints for parameters.



In essence, if you are coding with scalars (ignore type hinting) and
relying on PHP to do the right thing when type juggling takes place, then
the same level of acceptance should be used when type juggling of scalar
parameters.





TL;DR; Rejecting calls to a scalar type typehinted function because the
value is of the wrong type doesn't fit with PHP's type juggling.



-- 

Richard Quadling

---

## #80464 — pajousek@gmail.com — 2015-01-14T14:00:11+00:00

URL: https://externals.io/message/80442#80464 · In reply to: #80462

Hello,

personally, as a language user, I really dislike the idea of both
options for scalar type hinting to be the part of the language.
Especially since you would have to declare the strict typing in each
file (if you are going by 1 class per file in a bigger project, that's
a LOT of declare directives to write in the long run) and if you'd
forgot once, it would make the calling of methods somehow
inconsistent.

I wish there was just one way to do it; I don't care if the weak or
strong variant is going to be accepted, IMHO most programmers will be
able to adapt to either of them. The weak version makes probably more
sense for PHP and how it handles scalar types (at least from userland
developer's standpoint), but either of them is better than no
typehints at all. :)

PS: Personally, I find the "scalar" typehint idea useless and cannot
find a real use case for it. Richard, would you mind giving an
example?

Regards
Pavel Kouril


> I don’t think we’re ever going to get consensus.  But judging by the
> feedback to the v0.1 version, I tend to disagree that the opposers would
> have blocked it.  There were certainly opposers – but not that many of them
> as far as I could tell.  I think it stood a good chance to pass at a 2/3.
> Unlike strict typing – we didn’t even go to a vote on it, which I think is
> unfortunate (and should be changed, before changing course completely as
> this v0.2 suggests).
>
>
>
> We’re definitely not going to have consensus on introducing both options as
> per this RFC.  I for one think it’s the worst possible option.
>
>
>
> Zeev
>
>
>
>
>
> *From:* Richard Quadling [mailto:rquadling@gmail.com]
> *Sent:* Wednesday, January 14, 2015 3:23 PM
> *To:* Zeev Suraski
> *Cc:* Andrea Faulds; Leigh; PHP Internals List
> *Subject:* Re: [PHP-DEV] [RFC] Scalar Type Hints v0.2
>
>
>
>
>
>
>
> 
>
> > […]
>
> I don't either.  But I don't like the user to do it either, it's something
> that is a part of the language definition.
>
> I completely agree with both Robert and Leigh.  I liked the v0.1 one, but
> v0.2 is DOA from my point of view.  Arguably, from my POV, it's the one
> option that's even worse than having strict typing exclusively.
>
> Zeev
>
>
>
> The issue of scalar type hinting has been around for a while, with various
> proposals and RFCs, but no consensus. Each time a certain level of progress
> is made, more issues arise.
>
>
>
> It would seem that the major issues is that whatever a library/framework
> developer decides, the user of that library/framework would have to develop
> their application to fit. Is this such a bad thing? The comeback to that is
> that PHP supports type juggling, so there is a different mechanism for
> non-scalars to scalars. Fair enough.
>
>
>
>
>
> From the lib developer's perspective, if a parameter to a function/method
> is going to be treated as an integer and is expected to be an integer, then
> they should be able to force/limit this just like they can when they say it
> must be an array/callable/class/interface. But take into account the option
> to enforce the type juggling.
>
>
>
>
>
> I get 3 questions from this.
>
>
>
> 1 - Should the type hint restrict non matching types?
>
> 2 - When should the value be juggled to match the type hint if it is
> different?
>
> 3 - Could we allow different signatures for the same method name? - A
> different question entirely, but one that may be worth having in relation
> to this proposal.
>
>
>
>
>
> Whatever mechanism is used, it needs to be of zero effort for the user, but
> fully understood of the effect.
>
>
>
>
>
> I think to answer all of this, and to provide enough flexibility that
> everyone can be made happy and understand the effect, I think the issues
> could be resolved with the following.
>
>
>
>
>
> 1 - A type hint of 'scalar' is required.
>
>
>
> This excludes other types (array/callable/class/interface) and would
> specifically mean that no type juggling will take place as part of the
> parameter passing.
>
>
>
> If no type juggling is going to take place, why does it matter what the
> type hint is? The advantage of having 'scalar' as a type hint is, as I
> said, excludes non-scalars.
>
>
>
> Now, if the method does something to the value and the value is passed by
> reference, then that would need to be documented clearly.
>
>
>
>
>
>
>
> 2 - A scalar type type hint will automatically juggle. PHP is a dynamic
> language and, as such, type juggling is used throughout. It shouldn't be
> any different when a parameter is type hinted for scalars.
>
>
>
>
>
>
>
> The frustration seems to be that without a way to say that this scalar will
> NOT be type juggled (loose) or that it will be (strong) then we cannot move
> forward.
>
>
>
>
>
>
>
> Now, some of the comments related to this proposal (and others) is what to
> do when the scalar type is of a different type to the parameter.
>
>
>
> I think I've covered this by the use of the type hint 'scalar'.
>
>
>
> Scalars get type juggled. Period. For good or bad. Until PHP becomes a
> strongly type language, type juggling exists.
>
>
>
> With the hinting, we can let users know that type juggling will now also
> apply to parameters.
>
>
>
> So.
>
>
>
> <?php
>
> // Any type.
>
> function anyType($a){}
>
>
>
> // Current types.
>
> function anArray(array $b){}
>
> function aCallable(callable $c){}
>
> function aClass(stdclass $d){}
>
> function anInterface(Countable $e){}
>
>
>
> // Any scalar - not juggled.
>
> function anyScalar(scalar $f){}
>
>
>
> // Any scalar - juggled
>
> function willBeJuggledToBool(bool $g){}
>
> function willBeJuggledToString(string $h){}
>
> function willBeJuggledToFloat(float $i){}
>
> function willBeJuggledToInteger(integer $j){}
>
> ?>
>
>
>
> Considering that there are no errors generated when type juggling of
> scalars take place, there should be no errors when type juggling scalar
> type hints for parameters.
>
>
>
> In essence, if you are coding with scalars (ignore type hinting) and
> relying on PHP to do the right thing when type juggling takes place, then
> the same level of acceptance should be used when type juggling of scalar
> parameters.
>
>
>
>
>
> TL;DR; Rejecting calls to a scalar type typehinted function because the
> value is of the wrong type doesn't fit with PHP's type juggling.
>
>
>
> --
>
> Richard Quadling

---

## #80466 — Ole Markus With — 2015-01-14T15:20:04+00:00

URL: https://externals.io/message/80442#80466 · In reply to: #80462

> I don’t think we’re ever going to get consensus.  But judging by the
> feedback to the v0.1 version, I tend to disagree that the opposers would
> have blocked it.  There were certainly opposers – but not that many of them
> as far as I could tell.  I think it stood a good chance to pass at a 2/3.
> Unlike strict typing – we didn’t even go to a vote on it, which I think is
> unfortunate (and should be changed, before changing course completely as
> this v0.2 suggests).
> 

Hi guys,

I completely agree. I would have gone through with voting on v0.1 first.
Then later (maybe) propose v0.2 as an improvement.

For me v0.1 is better than no hinting, and no hinting is better than v0.2.

-- 
Ole Markus

---

## #80467 — Arvids Godjuks — 2015-01-14T15:25:34+00:00

URL: https://externals.io/message/80442#80467 · In reply to: #80464

2015-01-14 16:00 GMT+02:00 Pavel Kouřil <pajousek@gmail.com>:

> Hello,
>
> personally, as a language user, I really dislike the idea of both
> options for scalar type hinting to be the part of the language.
> Especially since you would have to declare the strict typing in each
> file (if you are going by 1 class per file in a bigger project, that's
> a LOT of declare directives to write in the long run) and if you'd
> forgot once, it would make the calling of methods somehow
> inconsistent.
>
> I wish there was just one way to do it; I don't care if the weak or
> strong variant is going to be accepted, IMHO most programmers will be
> able to adapt to either of them. The weak version makes probably more
> sense for PHP and how it handles scalar types (at least from userland
> developer's standpoint), but either of them is better than no
> typehints at all. :)
>
> PS: Personally, I find the "scalar" typehint idea useless and cannot
> find a real use case for it. Richard, would you mind giving an
> example?
>
> Regards
> Pavel Kouril


As a userland developer, have to agree with this message fully.

-1 on the v0.2 version, the declare() approach is gonna make our job
misserable after a few years when this gets adopted and misused by a lot of
people...

---

## #80468 — Richard Quadling — 2015-01-14T15:34:40+00:00

URL: https://externals.io/message/80442#80468 · In reply to: #80464

>
>
> PS: Personally, I find the "scalar" typehint idea useless and cannot
> find a real use case for it. Richard, would you mind giving an
> example?
>

The point of the 'scalar' typehint comes about because we need to reject
non compatible types. Just like we do when we say array or callable.

Currently, if we want a method/function to accept a scalar, then we cannot
put any type hinting in and so we can supply an array, callable, class or
interface.

Being able to say 'scalar' would mean I can instantly inform the user that
any scalar can be used and that no type juggling will happen automatically.

For example, take ...

<?php
function sum(scalar $left, scalar $right) { return $left + $right;}
?>

Now in this example, sure the parameters will be juggled because of the +
operator.

But without the typehint, and the lack of operator overloading, you would
get an error if non-scalars are supplied in the addition, rather than in
the call. The + is fine, it is the wrong params to the function that should
be the problem. And a scalar typehint is the better place I think.

Now if we have polymorphic behaviour ...

<?php
function sum(scalar $left, scalar $right) { return $left + $right;} // Type
juggled addition
function sum(array $left, array $right) { return array_merge($left,
$right);} // Array merging
function sum(string $left, string $right) { return $left . $right;} //
String concatenation.
?>
(OK, 'sum' isn't a perfect example, but you see the point I hope).

Richard.





-- 
Richard Quadling
Twitter : @RQuadling
EE : http://e-e.com/M_248814.html
Zend : http://bit.ly/9O8vFY

---

## #80470 — Rowan Collins — 2015-01-14T15:45:14+00:00

URL: https://externals.io/message/80442#80470 · In reply to: #80460

Andrea Faulds wrote on 14/01/2015 13:15:
> Hi Thomas,
>
> > […]
> The value would be casted, yes. From the function author’s perspective, there’s no need to worry about strict vs. weak typing: in both cases, you get the type you asked for.
>

Perhaps it would be clearer if the RFC (and the documentation, if this 
is accepted) referred to the non-strict as something other than "weak". 
It makes it sound like only a weak check will be performed, and some 
values of the "wrong" type will be allowed through, when in reality you 
will always get exactly the type asked for.

Maybe "coercive type-checking" would more accurately reflect the 
functionality?

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #80475 — Marcio Almada — 2015-01-14T18:52:32+00:00

URL: https://externals.io/message/80442#80475 · In reply to: #80445

Andrea,

With all the respect this RFC is way worst than the v0.1:

We still have a BC break but now we also have code with **mutant** behavior
that might become buggy (do unexpected things) if a `declare` is used. As a
language user and a package maintainer it would be a huge problem. Imagine
how would be to maintain a package that can be used with both strict and
coercive type checking. We would have to write 2x more tests and yet
pollute code with manual type checks (is_string, is_integer)  for the non
strict runtime mode when type check is necessary.

This RFC is a huge regression, IMMO, -1 here

2015-01-14 5:22 GMT-03:00 Robert Stoll <php@tutteli.ch>:

> Hi Andrea
>
> > […]
> and bumped its version to 0.2.
> > […]
> RFC, so it’d be sensible to separate discussion of the
> > […]
> visit: http://www.php.net/unsub.php
>
> I had a few thoughts on the new proposed declare(strict_typehints=TRUE);
> construct and I must say I do not really like that we would have different
> behaviour just based on a directive. This is quite ugly from a readability
> point of view since I as user cannot easily see whether somewhere (above)
> in the code the directive was set (I have changed my mind a little bit
> below - left it here for traceability purposes). Which brings me to the
> question what happens with the following code:
>
> function foo(int $a){}  //is weak right?
> declare(strict_typehinting=TRUE);
>
>
> I guess it is weak since you wrote: "If this boolean value is TRUE, strict
> type-checking mode is used for function calls in the remainder of the file"
> Maybe it is a good idea to add such an example to the examples as well.
>
> Now, having rethought my own email, I think we should enforce some code
> style to avoid a huge mess. Why not declare that the directive needs to be
> the very first statement inside a namespace scope (unless the directive
> block syntax is used). This way it would be easily verifiable for the user
> if strict mode is in place or not. But then again, we could also argue that
> is up to code conventions/code guidelines and they should specify whether
> mixing both modes is ok and where the directive has to be placed. So
> re-rethinking what I just wrote I would say it is fine as it is now but we
> should at least consider if we want to enforce some code practice to avoid
> a mess.
>
> Now another point: "Whether or not the function being called was declared
> in a file that uses strict or weak type checking is entirely irrelevant.
> The type checking mode depends on the file where the function is called."
> Seems quite strange to me but fair enough, why not -- actually makes
> perfectly sense for this RFC -> leaving it to the user to decide what
> he/she likes :-)

---

## #80476 — Marc Bennewitz — 2015-01-14T19:01:00+00:00

URL: https://externals.io/message/80442#80476 · In reply to: #80442

Hi Andrea,

I have some notes about this RFC from a users POV with only little 
knowledge about internals.
I didn't read 100% of the theads of this RFC so I'm sorry if some notes 
of this email was already discussed.


1. Inconsistencies of ZPP and explicit casts

In my opinion it should be the same if you call a function in weak type 
mode and calling a function in strict type mode with explicit cast.

But that would require to remove inconsistencies of ZPP and explicit casts.


2. Only one choice for the language

In my opinion scalar types should be hinted strict and the caller of an 
API have to be sure to pass the right types. The caller have to know 
about the function he calls and he already should know what the function 
expects. This is the point were the caller have to know the type of an 
expected argument and he should know his own types. So he is the one how 
can pass the variable or make an explicit type cast.

To do so see [1]


3. Reserved words

I don't like it adding so much reserved words.
As I understand it correctly the reservation is because of naming 
collisions on type-hints with scalars and classes/interfaces.

Why not adding these types as classes (marked final and not allowed to 
be initialized for know)?

Than you can use the already existing standards of classes as type-hints.
This would allow you using your own class of a different namespace with 
the same name and would reduce BC break drastically.
The only consequence would be that you have to reference the root 
namespace or import (use) the classes of the root namespace to use the 
type-hints.
As sugar it should be possible to use inheritance and polymorphism to 
better hint what you really need.

E.g.: class Integer extends Scalar implements Numeric


4. Only one naming

I dislike the proposed aliases. The type names should be defined once 
without aliases and the manual and error massages should be changed to 
be consistent.

Because of PHP internally already allows different names this should 
persist to be BC safe but should be reviewed and documented what is the 
real type name and what are aliases. The aliases could be deprecated in 
another RFC.

Btw. The type float is used also as double and real. I don't see if 
these aliases are supported in your RFC or not.


Marc


Am 14.01.2015 um 01:16 schrieb Andrea Faulds:
> Good evening,
>
> I’ve made some quite significant changes to my Scalar Type Hints RFC, and bumped its version to 0.2.
>
> Here: https://wiki.php.net/rfc/scalar_type_hints
>
> This is a new thread because I’ve made a significant revision to the RFC, so it’d be sensible to separate discussion of the updated RFC from the v0.1 RFC.
>
> Please tell me your thoughts.
>
> Thanks!
> --
> Andrea Faulds
> http://ajf.me/

---

## #80493 — Dmitry Stogov — 2015-01-14T20:40:12+00:00

URL: https://externals.io/message/80442#80493 · In reply to: #80442

In my opinion, version 0.1 was consistent enough.

handling two different approaches just makes mess...

We have internal function strlen(string $s), and it may be called with
integer argument e.g. strlen(123) -> 3
I think user functions should follow the same rules.

If some rules are "bad", lets change that rules for both internal and user
functions.
But it's better to do it in a separate RFC (after scalar type hinting 0.1
accepted), or we will stick forever.

Thanks. Dmitry.



> Good evening,
>
> I’ve made some quite significant changes to my Scalar Type Hints RFC, and
> bumped its version to 0.2.
>
> Here: https://wiki.php.net/rfc/scalar_type_hints
>
> This is a new thread because I’ve made a significant revision to the RFC,
> so it’d be sensible to separate discussion of the updated RFC from the v0.1
> RFC.
>
> Please tell me your thoughts.
>
> Thanks!
> --
> Andrea Faulds
> http://ajf.me/

---

Next page: https://externals.io/message/80442.md?page=2
