# [RFC] Scalar Type Hints

- Thread: https://externals.io/message/80028
- Original archive: https://news-web.php.net/php.internals/80028
- Started by Andrea Faulds on 2014-12-31T20:27:37+00:00
- Messages: 87
- RFC: Scalar Type Hints (Withdrawn) — https://externals.io/rfc/scalar_type_hints.md
- Page 1 of 4: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/80028.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

---

## #80028 — Andrea Faulds — 2014-12-31T20:27:37+00:00

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

Good evening,

Parameter type hints for PHP’s scalar types are a long-requested feature for PHP. Today I am proposing an RFC which is a new attempt to add them to the language. It is my hope that we can finally get this done for PHP 7.

I’d like to thank Dmitry, who emailed me and gave me some feedback on the draft RFC and some improvements to the patch. He encouraged me to put this to internals sooner rather than later, as it’s a feature many people are hoping will be in PHP 7.

The new RFC can be found here: https://wiki.php.net/rfc/scalar_type_hints

As well as the RFC, there is a working Zend Engine III patch with tests, and an incomplete specification patch.

Please read the RFC (and specification patch, if you wish) and tell me your thoughts.

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

---

## #80029 — Adam Harvey — 2014-12-31T20:51:52+00:00

URL: https://externals.io/message/80028#80029 · In reply to: #80028

> Parameter type hints for PHP’s scalar types are a long-requested feature for PHP. Today I am proposing an RFC which is a new attempt to add them to the language. It is my hope that we can finally get this done for PHP 7.
>
> I’d like to thank Dmitry, who emailed me and gave me some feedback on the draft RFC and some improvements to the patch. He encouraged me to put this to internals sooner rather than later, as it’s a feature many people are hoping will be in PHP 7.
>
> The new RFC can be found here: https://wiki.php.net/rfc/scalar_type_hints

At a first read through, this looks great, and much more in line with
what I'd like scalar type hints to look like. Nice job!

In terms of the open issues, here's what I think:

1. Aliases: I think we should support the same set of names as we
currently support for type casts[0] (excluding non-scalar types,
obviously) — this actually expands the list a little more, since there
are three (!) variants for floating point values, but I think it's
important to be consistent in all the places in the language where we
use "type" names.

2. Prohibiting use in class names: I think this makes sense, otherwise
we'll be violating the principle of least surprise when somebody does
call a class Int. (Yes, namespacing might help here, but I think I'd
rather restrict it altogether rather than trying to come up with rules
around when you can call a class "int" and how you'd refer to it.)

To bang another drum (and this shouldn't be responded to in this
thread, since we have another one going for this RFC): this sort of
change is why 5.7 is vital for migration — emitting deprecation
warnings for users calling classes by names that we'll be prohibiting
in PHP 7 is important, and will help our users migrate their code
bases.

I haven't looked at the patches yet, but assuming they're good, my
initial feeling is +1. Good work on taming the beast. :)

Adam

[0] http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

---

## #80030 — Andrea Faulds — 2014-12-31T21:11:08+00:00

URL: https://externals.io/message/80028#80030 · In reply to: #80029

Hey Adam,

> 
> 
> At a first read through, this looks great, and much more in line with
> what I'd like scalar type hints to look like. Nice job!

Glad to hear that!

> In terms of the open issues, here's what I think:
> 
> 1. Aliases: I think we should support the same set of names as we
> currently support for type casts[0] (excluding non-scalar types,
> obviously) — this actually expands the list a little more, since there
> are three (!) variants for floating point values, but I think it's
> important to be consistent in all the places in the language where we
> use "type" names.

I had thought about this. I don’t really want to support multiple names for each type. I’m alright with allowing short and long forms (int/integer, bool/boolean), because it’s fairly obvious to users that they mean the same thing, and we don’t use either form consistently. However, I don’t like completely different type names like ‘long’, ‘double’ and ‘real’. Luckily, in master, we no longer use ‘long’ or ‘double’ in error messages, and I don’t think we’ve ever used ‘real’ anywhere outside of the cast operator. So, I don’t think it’d cause problems to not allow these as type hints.

> 2. Prohibiting use in class names: I think this makes sense, otherwise
> we'll be violating the principle of least surprise when somebody does
> call a class Int. (Yes, namespacing might help here, but I think I'd
> rather restrict it altogether rather than trying to come up with rules
> around when you can call a class "int" and how you'd refer to it.)
> 
> To bang another drum (and this shouldn't be responded to in this
> thread, since we have another one going for this RFC): this sort of
> change is why 5.7 is vital for migration — emitting deprecation
> warnings for users calling classes by names that we'll be prohibiting
> in PHP 7 is important, and will help our users migrate their code
> bases.

I hadn’t thought of that, but yes, having a deprecation notice for this in 5.7 would be a good idea.

Thanks for your comments.

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

---

## #80031 — Stanislav Malyshev — 2014-12-31T21:12:43+00:00

URL: https://externals.io/message/80028#80031 · In reply to: #80028

Hi!

I like it, it's consistent and to the point. If we must have scalar
typed parameters (I'm not sure but if we do), IMO that's the way to do it.

The issue is the class names though. E.g. see:
https://github.com/ralphschindler/zf2-db/blob/master/research/ColumnType/Integer.php

and:

https://github.com/canaydogan/ObjectValidator/blob/master/src/ObjectValidator/Validator/Int.php

These are uppercase "I", but class names are not case-sensitive... So
we'll need to figure it out. Unfortunately, looking at github,
disallowing "class Int" looks like pretty bad idea. We can make it
lowercase-only, but this would be a bit weird since it's not sensitive
in all other places (unless we change that which is another huge can of
worms).

BTW, right now this code:

function foo(integer $a) { var_dump($a); }
foo(1);

produces this message:

Catchable fatal error: Argument 1 passed to foo() must be an instance of
integer, integer given

which is pretty confusing I'd say :) We'd probably want to rephrase that
message (yes, I know, patching all those tests yet again...)
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #80032 — Andrea Faulds — 2014-12-31T21:28:20+00:00

URL: https://externals.io/message/80028#80032 · In reply to: #80031

Hey Stas,

> 
> 
> I like it, it's consistent and to the point. If we must have scalar
> typed parameters (I'm not sure but if we do), IMO that's the way to do it.

Glad you like it.

> The issue is the class names though. E.g. see:
> https://github.com/ralphschindler/zf2-db/blob/master/research/ColumnType/Integer.php
> 
> and:
> 
> https://github.com/canaydogan/ObjectValidator/blob/master/src/ObjectValidator/Validator/Int.php
> 
> These are uppercase "I", but class names are not case-sensitive... So
> we'll need to figure it out. Unfortunately, looking at github,
> disallowing "class Int" looks like pretty bad idea. We can make it
> lowercase-only, but this would be a bit weird since it's not sensitive
> in all other places (unless we change that which is another huge can of
> worms).

Yeah, it’s a problem. I think some breakage here is inevitable, unfortunately. Some of the classes with these names are stand-ins for scalar type hints, so that code can “just” migrate to using actual hints. But this doesn’t apply to all of them.

We could choose to simply not prohibit them as class names, but that creates a weird inconsistency where you can make ‘class Integer’ yet ‘function foo(Integer $a)’ hints against the integer type, not your class. Type hints are very widely used, so I doubt this would help anyone, and we’d still be breaking existing code type hinting against such classes.

There’s not much we can do really. I suppose there is one positive outcome, in that hopefully when broken code is updated to work, it might have more descriptive class names. :)

> BTW, right now this code:
> 
> function foo(integer $a) { var_dump($a); }
> foo(1);
> 
> produces this message:
> 
> Catchable fatal error: Argument 1 passed to foo() must be an instance of
> integer, integer given
> 
> which is pretty confusing I'd say :) We'd probably want to rephrase that
> message (yes, I know, patching all those tests yet again…)

We could rephrase it, but I don’t think it’s that bad. Once scalar hints are in, you won’t get that error any more. I think it could be worse as errors go, at least it distinguishes semi-clearly between objects (“instance of integer”) and other types (“integer”).

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

---

## #80038 — Sebastian Bergmann — 2015-01-01T08:28:15+00:00

URL: https://externals.io/message/80028#80038 · In reply to: #80028

Am 31.12.2014 um 21:27 schrieb Andrea Faulds:
> Parameter type hints for PHP’s scalar types

 Please use the term "type declaration for arguments" (or "type
 declaration for parameters") instead of "type hints". If it's used
 then it's not a hint.

---

## #80039 — Markus Fischer — 2015-01-01T11:09:26+00:00

URL: https://externals.io/message/80028#80039 · In reply to: #80038

Hello Sebastian,


> Am 31.12.2014 um 21:27 schrieb Andrea Faulds:
> > […]
> 
>  Please use the term "type declaration for arguments" (or "type
>  declaration for parameters") instead of "type hints". If it's used
>  then it's not a hint.

Thanks for bringing this up.

Interestingly, from the RFC I had another issue with it: what it
technically does it neither "hinting" nor "declaration" but it tries
conversion and seem to have multiple action how it may continue:
- continue, everything as expected
- catchable error
- notices thrown

To Andrea:

As much as I'd like to see such a language feature like this, I think:

1. the naming of the RFC thus the intent is confusing

What it really does is it tries its best to convert, e.g. the RFC reads
as it tries to work like this:

function foo(int $bar) {
  $bar = (int)$bar;
}

From your description I understand that, technically, it doesn't do that
exactly; I was merely trying to make a point how it "looks it works";
see my next point.


2. Casting and Validation Rules

"While this RFC merely follows PHP's existing rules for scalar
parameters, used by extension functions, these rules may not be familiar
to all readers of this RFC."

Very good job in pointing this out! Which brings me to this question:
are these different rules than the general casting rules (as I exampled
above) ? If yes, wouldn't this increase the burden on PHP developers
even more to learn new rules (and, those rules are already to many to
sanely remember).


3. "Non-numeric strings not accepted. Numeric strings with trailing
characters are accepted, but produce a notice. "

That behavior, IMHO, is very bad for this kind of feature.

What's the point of continuing the code when developer asked for "int"
and code logic continues with "something not quite an int"?

Doesn't that defeat the whole purpose of the use of this RFC?

I know that this can be fixed using an error handler, throwing an
exception to abort code execution but that should really be an error
anyway, IMHO, on par with a gross mismatch of types.


I'm not in favor of these soft rules.

thanks,
- Markus

---

## #80041 — Zeev Suraski — 2015-01-01T12:41:06+00:00

URL: https://externals.io/message/80028#80041 · In reply to: #80028

> -----Original Message-----
> From: Andrea Faulds [mailto:ajf@ajf.me]
> Sent: Wednesday, December 31, 2014 10:28 PM
> To: PHP Internals
> Subject: [PHP-DEV] [RFC] Scalar Type Hints
>
> Good evening,
>
> Parameter type hints for PHP’s scalar types are a long-requested feature
> for
> PHP. Today I am proposing an RFC which is a new attempt to add them to
> the language. It is my hope that we can finally get this done for PHP 7.

Andrea,


I like this draft too, and that's a first after countless proposals over the
last decade - so kudos! :)

My main feedback here are the discrepancies between this RFC's casting rules
and PHP's current built-in casting rules.  Ideally, I'd like those to be
completely identical and not almost-identical.
Since we're talking about v7.0, we do have the option of making changes to
PHP's fundamental casting rules where appropriate (e.g. converting an array
to a string).
But that said, I think the way strings->numbers are handled - where they
accept only numeric strings as it would mean you can't use casting to an
int/float as an ultra-simple way to sanitize untrusted input.  I would
change the † section from:

†Non-numeric strings not accepted. Numeric strings with trailing characters
are accepted, but produce a notice.
to
† Numeric strings with trailing characters and non-numeric strings are
accepted, but produce a notice.

- and apply it to both this RFC and the infrastructure convert_to_*(), so
that it applies across the board in PHP.

Zeev

---

## #80044 — Maxime Veber — 2015-01-01T13:29:46+00:00

URL: https://externals.io/message/80028#80044 · In reply to: #80041

Hello,

I was expected an RFC like this in PHP for a while. I'm happy somebody made
one, thanks.

But something hit me in that. even if you can't give an objet, you can give
any scalar type that will be cast.
I'm not sure this behavior is very relevant.

Actually if I ask for a string, why the user could be specify an int
without getting error ?

Consider the following PHP 5.6 code:

function foo($a) {
    if (!is_string($a)) {
        throw new \Exception('You need to give a string');
    }
}

Using your type hinting does not fix the problem of string checking because
the value will just be cast if it's an integer. Ofc if the user give an
object it's ok.
But why not throw a message error in any cases ?

I hope i'm clear enough.
Thanks for reading and have an happy new year :-) .


2015-01-01 13:41 GMT+01:00 Zeev Suraski <zeev@zend.com>:

> > […]
>
> Andrea,
>
>
> I like this draft too, and that's a first after countless proposals over
> the
> last decade - so kudos! :)
>
> My main feedback here are the discrepancies between this RFC's casting
> rules
> and PHP's current built-in casting rules.  Ideally, I'd like those to be
> completely identical and not almost-identical.
> Since we're talking about v7.0, we do have the option of making changes to
> PHP's fundamental casting rules where appropriate (e.g. converting an array
> to a string).
> But that said, I think the way strings->numbers are handled - where they
> accept only numeric strings as it would mean you can't use casting to an
> int/float as an ultra-simple way to sanitize untrusted input.  I would
> change the † section from:
>
> †Non-numeric strings not accepted. Numeric strings with trailing characters
> are accepted, but produce a notice.
> to
> † Numeric strings with trailing characters and non-numeric strings are
> accepted, but produce a notice.
>
> - and apply it to both this RFC and the infrastructure convert_to_*(), so
> that it applies across the board in PHP.
>
> Zeev

---

## #80045 — Nikita Popov — 2015-01-01T14:05:13+00:00

URL: https://externals.io/message/80028#80045 · In reply to: #80028

> Good evening,
>
> Parameter type hints for PHP’s scalar types are a long-requested feature
> for PHP. Today I am proposing an RFC which is a new attempt to add them to
> the language. It is my hope that we can finally get this done for PHP 7.
>
> I’d like to thank Dmitry, who emailed me and gave me some feedback on the
> draft RFC and some improvements to the patch. He encouraged me to put this
> to internals sooner rather than later, as it’s a feature many people are
> hoping will be in PHP 7.
>
> The new RFC can be found here: https://wiki.php.net/rfc/scalar_type_hints
>
> As well as the RFC, there is a working Zend Engine III patch with tests,
> and an incomplete specification patch.
>
> Please read the RFC (and specification patch, if you wish) and tell me
> your thoughts.
>
> Thanks!
>

While in favor of introducing scalar type annotations, I'm against this
proposal in particular. I've held a different position in the past, but by
now I'm thoroughly convinced that if we introduce scalar type declarations,
they should be strict. No wiggling, no casting.

Apart from being consistent with the existing behavior of type declarations
and being what the majority of the vocal community wants, it is also
possible to reason about strict type declarations statically.

This means that an IDE or other tool will be able to perform meaningful
analysis based on typehinted functions. E.g. if you pass the result of a
string function to an int parameter, your code is definitely wrong and you
can be told so. Loose typehints as proposed here do not offer this
possibility, because a string can be or can not be a valid input to an int
parameter depending on the exact value.

For the same reason loose typehints are also more fragile. Code that worked
in casual testing during development will fail in production when
unexpected, improperly validated user input is encountered. With strict
types on the other hand it is very likely that code working with one input
will also work with all other possible inputs, because the type check is
not value-dependent. (Types are typically much less volatile than values.)

The ability to statically check type annotations is rather important to me.
I think much of the usefulness of this feature would be lost without the
ability to check correct usage with tooling. I'd also like to point out
that Hack uses a strict type scheme and it seems to work well there (though
I do acknowledge that the situation is not the same, as Hack has a
generally more powerful and fully statically checked type system).

Apart from these general thoughts, I also think that this proposal is a
regression from the previous one. In the name of "consistency" it uses the
rather weak zpp validation rules, which allow a lot of questionable input.
Just look at the conversion table in the RFC, practically all of it is
"Yes". I understand the motivation to reduce the number of different
conversion semantics, but I just can't get behind it if it means reusing
existing, bad conversion rules.

Nikita

---

## #80046 — Andrea Faulds — 2015-01-01T15:12:07+00:00

URL: https://externals.io/message/80028#80046 · In reply to: #80038

Hey Sebastian,

> 
> 
> Am 31.12.2014 um 21:27 schrieb Andrea Faulds:
> > […]
> 
> Please use the term "type declaration for arguments" (or "type
> declaration for parameters") instead of "type hints". If it's used
> then it's not a hint.

You’re not the only one who feels this way, Levi Morison is also not too fond of our existing terminology. However, it is the name we’ve always used for these. It’s not the best name, but it is the name we’ve given them. This could be changed, but I don’t think that’s the job of this RFC. If someone else wants to push to change the name, go ahead.

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

---

## #80047 — Andrea Faulds — 2015-01-01T15:19:04+00:00

URL: https://externals.io/message/80028#80047 · In reply to: #80039

Hi Markus,

> 
> 
> 1. the naming of the RFC thus the intent is confusing
> 
> What it really does is it tries its best to convert, e.g. the RFC reads
> as it tries to work like this:
> 
> function foo(int $bar) {
>  $bar = (int)$bar;
> }
> 
> From your description I understand that, technically, it doesn't do that
> exactly; I was merely trying to make a point how it "looks it works";
> see my next point.

I think it’d be weird to have different syntaxes for scalars and non-scalars. We already use the syntax the RFC proposes in the PHP manual, and I don’t think anyone’s confused by it. 

I realise that the behaviour of scalar type hints is slightly different from the hints for complex types, but I think this would just look ugly:

    function foo((int) $bar, Foo $baz, (float) $qux);

> 2. Casting and Validation Rules
> 
> "While this RFC merely follows PHP's existing rules for scalar
> parameters, used by extension functions, these rules may not be familiar
> to all readers of this RFC."
> 
> Very good job in pointing this out! Which brings me to this question:
> are these different rules than the general casting rules (as I exampled
> above) ? If yes, wouldn't this increase the burden on PHP developers
> even more to learn new rules (and, those rules are already to many to
> sanely remember).

Yes and no. With the exception of hexadecimal numbers in strings, explicit casts and internal functions follow the same rules for conversion. However, the validation rules don’t match. Explicit casts never fail, while internal functions will reject arguments that are non-scalar or don’t fit certain rules.

> 3. "Non-numeric strings not accepted. Numeric strings with trailing
> characters are accepted, but produce a notice. "
> 
> That behavior, IMHO, is very bad for this kind of feature.
> 
> What's the point of continuing the code when developer asked for "int"
> and code logic continues with "something not quite an int"?
> 
> Doesn't that defeat the whole purpose of the use of this RFC?
> 
> I know that this can be fixed using an error handler, throwing an
> exception to abort code execution but that should really be an error
> anyway, IMHO, on par with a gross mismatch of types.

I can’t say I love that behaviour either. It is, however, our existing behaviour. I’d rather we be consistent with internal functions.

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

---

## #80048 — Andrea Faulds — 2015-01-01T15:24:32+00:00

URL: https://externals.io/message/80028#80048 · In reply to: #80041

Hey Zeev,

> 
> 
> I like this draft too, and that's a first after countless proposals over the
> last decade - so kudos! :)

Glad to hear that.

> My main feedback here are the discrepancies between this RFC's casting rules
> and PHP's current built-in casting rules.  Ideally, I'd like those to be
> completely identical and not almost-identical.

The RFC’s behaviour exactly matches that of zend_parse_parameters, with the exception of `NULL` handling. In fact, the implementation is shared.

Whether zend_parse_parameters’s behaviour matches that of other implicit casts in PHP is another matter.

> Since we're talking about v7.0, we do have the option of making changes to
> PHP's fundamental casting rules where appropriate (e.g. converting an array
> to a string).
> But that said, I think the way strings->numbers are handled - where they
> accept only numeric strings as it would mean you can't use casting to an
> int/float as an ultra-simple way to sanitize untrusted input.  I would
> change the † section from:
> 
> †Non-numeric strings not accepted. Numeric strings with trailing characters
> are accepted, but produce a notice.
> to
> † Numeric strings with trailing characters and non-numeric strings are
> accepted, but produce a notice.
> 
> - and apply it to both this RFC and the infrastructure convert_to_*(), so
> that it applies across the board in PHP.

That could be changed, of course, but I don’t think it’s the job of this RFC to change our implicit casting/validation rules for functions. I would be willing to postpone this RFC’s vote until an RFC that makes some changes gets through, though.

That said, I don’t actually like the idea of changing this specific thing. “0”, “0.0” and possibly even “0 foobar” are reasonable candidates for an integer value, but I don’t think making “” or “foobar” be accepted as numbers makes much sense at all.

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

---

## #80049 — Andrea Faulds — 2015-01-01T15:28:40+00:00

URL: https://externals.io/message/80028#80049 · In reply to: #80044

Hi Maxime,

> 
> 
> I was expected an RFC like this in PHP for a while. I'm happy somebody made one, thanks.

Glad to hear that.

> But something hit me in that. even if you can't give an objet, you can give any scalar type that will be cast.
> I'm not sure this behavior is very relevant.
> 
> Actually if I ask for a string, why the user could be specify an int without getting error ?
> 
> Consider the following PHP 5.6 code:
> 
> function foo($a) {
>     if (!is_string($a)) {
>         throw new \Exception('You need to give a string');
>     }
> }
> 
> Using your type hinting does not fix the problem of string checking because the value will just be cast if it's an integer. Ofc if the user give an object it's ok.
> But why not throw a message error in any cases ?

For various reasons, PHP has always been a weakly-typed language, so we allow conversions between scalar types when they make sense. An integer isn’t a string, sure, but it can be simply converted to one and the result makes sense, so I think that’s why we allow it. It would still error in some cases, like an object without __toString for a string parameter, or an array for an integer parameter.

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

---

## #80050 — Andrea Faulds — 2015-01-01T15:33:41+00:00

URL: https://externals.io/message/80028#80050 · In reply to: #80045

Hey Nikita,

> 
> 
> While in favor of introducing scalar type annotations, I'm against this proposal in particular. I've held a different position in the past, but by now I'm thoroughly convinced that if we introduce scalar type declarations, they should be strict. No wiggling, no casting.
> 
> Apart from being consistent with the existing behavior of type declarations and being what the majority of the vocal community wants, it is also possible to reason about strict type declarations statically.
> 
> This means that an IDE or other tool will be able to perform meaningful analysis based on typehinted functions. E.g. if you pass the result of a string function to an int parameter, your code is definitely wrong and you can be told so. Loose typehints as proposed here do not offer this possibility, because a string can be or can not be a valid input to an int parameter depending on the exact value.
> 
> For the same reason loose typehints are also more fragile. Code that worked in casual testing during development will fail in production when unexpected, improperly validated user input is encountered. With strict types on the other hand it is very likely that code working with one input will also work with all other possible inputs, because the type check is not value-dependent. (Types are typically much less volatile than values.)
> 
> The ability to statically check type annotations is rather important to me. I think much of the usefulness of this feature would be lost without the ability to check correct usage with tooling. I'd also like to point out that Hack uses a strict type scheme and it seems to work well there (though I do acknowledge that the situation is not the same, as Hack has a generally more powerful and fully statically checked type system).
> 
> Apart from these general thoughts, I also think that this proposal is a regression from the previous one. In the name of "consistency" it uses the rather weak zpp validation rules, which allow a lot of questionable input. Just look at the conversion table in the RFC, practically all of it is "Yes". I understand the motivation to reduce the number of different conversion semantics, but I just can't get behind it if it means reusing existing, bad conversion rules.

I would respond with something novel, but I notice now that your email is exactly the same as your comment on reddit, and there’s little point in responding to the same thing twice. So, for that reason, I’ll just reproduce my response on reddit, with some minor edits:

> I'm likely -1 on this proposal. I've held a different position in the past, but by now I'm thoroughly convinced that if we introduce scalar typehints, they should be strict.

Why are you opposed to adding non-strict type hints? I realise that strict type hints are desirable for certain reasons, but they fit very poorly with the rest of the language and are unlikely to ever make it into PHP. So why, then, oppose the addition of non-strict hints? Surely these hints, which offer many (albeit not all) of the benefits of strict hints, are far better than nothing?

> Apart from being consistent with the existing behavior of typehints

This RFC is consistent with the existing behaviour of extension functions (which match all userland type hints in behaviour). Besides, strict hints would be inconsistent with the rest of the language. PHP has never been strict for scalar types, and I think they'd make a rather awkward fit for that reason.

> , it is also possible to reason about strict typehints statically.

> This means that an IDE or other tool will be able to perform meaningful analysis based on typehinted functions. E.g. if you pass the result of a string function to an int parameter, your code is definitely wrong and you can be told so. Loose (casting) typehints do not offer this possibility, because a string can be or can not be a valid input to an int parameter depending on the exact value.

You have a point there. Unfortunately, casting hints prevent certain types of validation. However, some type combos always work and some always don't, so you can still error for certain cases (NULL, array, resource, or object where scalar expected).

While they do prevent certain types of validation, they don't prevent optimisations, so HHVM (and perhaps Zend in future) can still benefit from the type information. 

> For the same reason loose typehints are also more fragile. Code that worked in casual testing during development will fail in production when unexpected, improperly validated user input is encountered.

This is also true to an extent, but it is alleviated partly if code is fully type hinted. If your entire codebase has type hints, such errors can be caught early before they're passed to other functions. Once values are converted to integers, they stay integers (unless you pass it to something taking a string).

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

---

## #80051 — Markus Fischer — 2015-01-01T15:58:43+00:00

URL: https://externals.io/message/80028#80051 · In reply to: #80047

Hello Andrea,


> I think it’d be weird to have different syntaxes for scalars and non-scalars. We already use the syntax the RFC proposes in the PHP manual, and I don’t think anyone’s confused by it. 

I didn't meant to stay there's something wrong with the syntax, sorry if
my text was confusing! I was rather trying to point out that it does not
hint at anything but proactively tries to convert types; see below for more:

> > […]
> 
> Yes and no. With the exception of hexadecimal numbers in strings, explicit casts and internal functions follow the same rules for conversion. However, the validation rules don’t match. Explicit casts never fail, while internal functions will reject arguments that are non-scalar or don’t fit certain rules.
[...]
> 
> > […]
[...]

> > […]
[...]

> 
> I can’t say I love that behaviour either. It is, however, our existing behaviour. I’d rather we be consistent with internal functions.

And as you also pointed out: those conversion rules are different from
what a plain "(int)$whatever" would do.


I completely understand the technical ratio here: internal function have
an existing argument/parsing system which is now exposed to the end
developer ("php user") here.

I argue this system is fit and works (and, err, talk BC; obviously!) for
the whole internal PHP function/method system but ..

.. does not reflect the needs and requirements of a php user/developer
because exactly of these implicit conversion rules with three different
output states (everything ok, hard type mismatch, notice on partial
conversion).

I'd also argue that scalar types in function signatures behaving
differently than object type hints is potentially a bad thing for future
of PHP and I already made my point which is the most important to me:

> > […]



Now, going on step back here (talking about me), I'm speaking up because
>my< needs are developer are different (mostly speaking about backend
code, interfaces, libraries, frameworks) but OTOH I'm not a big known
open source framework developer either ;)

I would honestly be interested what the big framework/library players
actually want/need; do they prefer this implicit scalar type conversion
system or rather have a rigid system like the current object types but
for scalars too? I think decision on this RFC should include also
"their" saying too.

It's complex because we can't force anyone to participate but I think
above all these are the most important audience here because they know
what they want and they know what their users want. I say this because
usage of object types in PHP is almost non-existent (or, there are just
too few cases) compared to the architecture of some of the
framework/library systems out there.

Hmm.

thanks,
- Markus

---

## #80052 — Marcio Almada — 2015-01-01T15:59:30+00:00

URL: https://externals.io/message/80028#80052 · In reply to: #80050

The battle between strict type declarations vs coercive has been here for a
while. My problem with coercion in detriment of strictness is
that sometimes you DON'T WANT TYPE CASTING AT ALL. This new feature would
create serious impediments. So I wonder if we couldn't have both (strict
and coercive types declarations) and leave the current proposed type
hinting syntax reserved for strict type declarations? Like in:

    function(int $a, (int) $b) {
         // $a will be strict
         // $b will be type casted
    }

This way we can actually choose when to cast and when to be strict and both
features could be voted independently without affect each other possible
future adoptions.

---

## #80053 — Alexander Moskalev — 2015-01-01T16:27:05+00:00

URL: https://externals.io/message/80028#80053 · In reply to: #80052

genius and simple syntax!
int $a === assertInt($a)
(int)$a === (int)$a

2015-01-01 18:59 GMT+03:00 Marcio Almada <marcio.web2@gmail.com>:

> The battle between strict type declarations vs coercive has been here for a
> while. My problem with coercion in detriment of strictness is
> that sometimes you DON'T WANT TYPE CASTING AT ALL. This new feature would
> create serious impediments. So I wonder if we couldn't have both (strict
> and coercive types declarations) and leave the current proposed type
> hinting syntax reserved for strict type declarations? Like in:
>
>     function(int $a, (int) $b) {
>          // $a will be strict
>          // $b will be type casted
>     }
>
> This way we can actually choose when to cast and when to be strict and both
> features could be voted independently without affect each other possible
> future adoptions.
>



-- 
With regards, Alexander Moskalev
irker@irker.net
irker@php.net
a.moskalev@corp.badoo.com

---

## #80054 — Maxime Veber — 2015-01-01T16:29:58+00:00

URL: https://externals.io/message/80028#80054 · In reply to: #80052

Woow. The compromise proposed by Marcio looks awesome to me :) .

Andrea, I can understand that in the PHP logic, but IMO if the RFC does not
avoid people that use code like I present before i don't think it's
completely relevant.
I mean, if people still use old tricks because the type hinting doesn't
allow to make what they wanted, then things are not cool enough and should
be re-think. And actually i say that because I see easily me still using
old trick without type hinting... Becaue of this cast story.

Also notice that Marcio's point could be another way, like adding something
with @integer (random operator choice, do not keep that in mind) that make
the check strict... Then IMO this operator will be so much used that it
will not make sence that is not the default behavior.
That's why the proposal of Marcio is cool to me.

2015-01-01 16:59 GMT+01:00 Marcio Almada <marcio.web2@gmail.com>:

> The battle between strict type declarations vs coercive has been here for a
> while. My problem with coercion in detriment of strictness is
> that sometimes you DON'T WANT TYPE CASTING AT ALL. This new feature would
> create serious impediments. So I wonder if we couldn't have both (strict
> and coercive types declarations) and leave the current proposed type
> hinting syntax reserved for strict type declarations? Like in:
>
>     function(int $a, (int) $b) {
>          // $a will be strict
>          // $b will be type casted
>     }
>
> This way we can actually choose when to cast and when to be strict and both
> features could be voted independently without affect each other possible
> future adoptions.

---

## #80055 — Andrea Faulds — 2015-01-01T16:35:01+00:00

URL: https://externals.io/message/80028#80055 · In reply to: #80051

Hi Markus,

> 
> 
> 
> > […]
> 
> I didn't meant to stay there's something wrong with the syntax, sorry if
> my text was confusing! I was rather trying to point out that it does not
> hint at anything but proactively tries to convert types; see below for more:

Ah, I see. Well, at least I was able to cover that syntax suggestion before someone else brings it up. I see your point.

> And as you also pointed out: those conversion rules are different from
> what a plain "(int)$whatever" would do.

The conversion rules are the same, it’s just that scalar hints reject certain values. The values they accept are converted the same. I don’t think this is unreasonable: implicit and explicit casts shouldn’t behave identically. Implicit casts need to be much stricter.

> Now, going on step back here (talking about me), I'm speaking up because
> > […]
> code, interfaces, libraries, frameworks) but OTOH I'm not a big known
> open source framework developer either ;)
> 
> I would honestly be interested what the big framework/library players
> actually want/need; do they prefer this implicit scalar type conversion
> system or rather have a rigid system like the current object types but
> for scalars too? I think decision on this RFC should include also
> "their" saying too.
> 
> It's complex because we can't force anyone to participate but I think
> above all these are the most important audience here because they know
> what they want and they know what their users want. I say this because
> usage of object types in PHP is almost non-existent (or, there are just
> too few cases) compared to the architecture of some of the
> framework/library systems out there.

That’s a fair point. I’m not sure how they feel about it.

Their views aren’t necessarily the most important, though. Frameworks can do whatever they like, but what ultimately matters is what’s best for the end users, who don’t deal with the framework internals.

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

---

## #80056 — Ferenc Kovacs — 2015-01-01T16:36:23+00:00

URL: https://externals.io/message/80028#80056 · In reply to: #80052

> The battle between strict type declarations vs coercive has been here for a
> while. My problem with coercion in detriment of strictness is
> that sometimes you DON'T WANT TYPE CASTING AT ALL. This new feature would
> create serious impediments. So I wonder if we couldn't have both (strict
> and coercive types declarations) and leave the current proposed type
> hinting syntax reserved for strict type declarations? Like in:
>
>     function(int $a, (int) $b) {
>          // $a will be strict
>          // $b will be type casted
>     }
>
> This way we can actually choose when to cast and when to be strict and both
> features could be voted independently without affect each other possible
> future adoptions.
>

for the record this was proposed (as an idea) previously with the exact
same syntax(the earlies mention I could find in my mailbox was from Derick
from 2009: http://comments.gmane.org/gmane.comp.php.devel/57653 but I
remember seeing it every time when this discussion happens).
If somebody is interested in previous attempts/discussion on the topic,
here are some links for starters:
http://nikic.github.io/2012/03/06/Scalar-type-hinting-is-harder-than-you-think.html
http://blog.ircmaxell.com/2012/03/parameter-type-casting-in-php.html
https://wiki.php.net/rfc/parameter_type_casting_hints
https://wiki.php.net/rfc/typechecking

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #80057 — Thomas Bley — 2015-01-01T16:44:02+00:00

URL: https://externals.io/message/80028#80057 · In reply to: #80045

I think it is no problem to add strict parameter type hints with another rfc (if this rfc gets accepted), e.g. function foobar(string! $str, int! $str){} or any other syntax.

Regards
Thomas

Nikita Popov wrote on 01.01.2015 15:05:

> 
> 
> > […]
> 
> While in favor of introducing scalar type annotations, I'm against this
> proposal in particular. I've held a different position in the past, but by
> now I'm thoroughly convinced that if we introduce scalar type declarations,
> they should be strict. No wiggling, no casting.
> 
> Apart from being consistent with the existing behavior of type declarations
> and being what the majority of the vocal community wants, it is also
> possible to reason about strict type declarations statically.
> 
> This means that an IDE or other tool will be able to perform meaningful
> analysis based on typehinted functions. E.g. if you pass the result of a
> string function to an int parameter, your code is definitely wrong and you
> can be told so. Loose typehints as proposed here do not offer this
> possibility, because a string can be or can not be a valid input to an int
> parameter depending on the exact value.
> 
> For the same reason loose typehints are also more fragile. Code that worked
> in casual testing during development will fail in production when
> unexpected, improperly validated user input is encountered. With strict
> types on the other hand it is very likely that code working with one input
> will also work with all other possible inputs, because the type check is
> not value-dependent. (Types are typically much less volatile than values.)
> 
> The ability to statically check type annotations is rather important to me.
> I think much of the usefulness of this feature would be lost without the
> ability to check correct usage with tooling. I'd also like to point out
> that Hack uses a strict type scheme and it seems to work well there (though
> I do acknowledge that the situation is not the same, as Hack has a
> generally more powerful and fully statically checked type system).
> 
> Apart from these general thoughts, I also think that this proposal is a
> regression from the previous one. In the name of "consistency" it uses the
> rather weak zpp validation rules, which allow a lot of questionable input.
> Just look at the conversion table in the RFC, practically all of it is
> "Yes". I understand the motivation to reduce the number of different
> conversion semantics, but I just can't get behind it if it means reusing
> existing, bad conversion rules.
> 
> Nikita

---

## #80058 — Thomas Bley — 2015-01-01T16:54:08+00:00

URL: https://externals.io/message/80028#80058 · In reply to: #80028

thanks for the rfc! Currently we allow:

function test(Array $o){}
test([]);
function test2(array $o){}
test2([]);

So I propose to allow also uppercase type names:
ublic function __construct(String $name, Int $age, Float $cuteness, Bool $evil) {

Regards
Thomas

Andrea Faulds wrote on 31.12.2014 21:27:

> Good evening,
> 
> Parameter type hints for PHP’s scalar types are a long-requested feature for
> PHP. Today I am proposing an RFC which is a new attempt to add them to the
> language. It is my hope that we can finally get this done for PHP 7.
> 
> I’d like to thank Dmitry, who emailed me and gave me some feedback on the
> draft RFC and some improvements to the patch. He encouraged me to put this to
> internals sooner rather than later, as it’s a feature many people are hoping
> will be in PHP 7.
> 
> The new RFC can be found here: https://wiki.php.net/rfc/scalar_type_hints
> 
> As well as the RFC, there is a working Zend Engine III patch with tests, and an
> incomplete specification patch.
> 
> Please read the RFC (and specification patch, if you wish) and tell me your
> thoughts.
> 
> Thanks!
> --
> Andrea Faulds
> http://ajf.me/

---

## #80060 — Andrea Faulds — 2015-01-01T16:56:11+00:00

URL: https://externals.io/message/80028#80060 · In reply to: #80058

Hi Thomas,

> 
> 
> thanks for the rfc! Currently we allow:
> 
> function test(Array $o){}
> test([]);
> function test2(array $o){}
> test2([]);
> 
> So I propose to allow also uppercase type names:
> ublic function __construct(String $name, Int $age, Float $cuteness, Bool $evil) {

The RFC doesn’t mention it, but this is allowed by the patch, since type names are case-insensitive.

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

---

## #80061 — Marco Pivetta — 2015-01-01T16:56:37+00:00

URL: https://externals.io/message/80028#80061 · In reply to: #80057

> I think it is no problem to add strict parameter type hints with another
> rfc (if this rfc gets accepted), e.g. function foobar(string! $str, int!
> $str){} or any other syntax.
>

The problem is that the current proposed hints/casts are deviating from the
type-hints that we are used to, therefore this particular feature should
(eventually) have an alternate syntax, whereas the strict hints would just
use `int`, `integer`, `string`, `float`, `bool`, `boolan` without
additional chars (assuming that we will get strict hints somewhen).

This will also reduce clashes with the current HHVM implementation.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

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