Zeev,
The vote on the Coercive Scalar Type Hints is now open for voting.
The latest version of the RFC includes changes discussed on internals@ last
week:1. Accept string->bool and int->bool conversions (false->bool is not
supported)
2. Accept leading/trailing spaces in string->number conversions.wiki.php.net/rfc/coercive_sth
wiki.php.net/rfc/coercive_sth#voteThanks!
Zeev
The proposed coercion rules aren't clear. The first table in the RFC implies
that string is accepted for bool hints, and the second table says this is
unchanged. But the examples section states that "foo" -> bool will raise an E_DEPRECATED
error. I'm guessing this is a mistake, since you mentioned
above that string->bool is accepted.
You also stated above that false->bool is not supported (I'm guessing you
meant float->bool). So users would be able to pass "4.3" to a bool typehint,
but not 4.3? This behavior seems very arbitrary and confusing.
Not only would this RFC introduce backwards compatibility breaks in a future
PHP release, but it still doesn't bring the same opportunities for static
analysis which the optional strict mode in Anthony's proposal enables.
The coercive RFC misleadingly contradicts itself by stating that "we don't
believe that is the case" "that Strict STH can help static analysis in
certain cases" "although it's possible that Strict Typing may be able to
help static analysis in certain edge cases." Do you believe it will help in
certain cases or not?
Strict types can absolutely help - not only for static analysis, but also
for potential future AOT/JIT development. Why? Because only types need to
be checked, rather than values. This is why an optional strict type system
in JavaScript makes it possible for Google to make "early and aggressive
optimisations" in V8 (see https://developers.google.com/v8/experiments).
It also makes it possible to catch errors earlier in development (see the
example in my "A different user perspective on scalar type declarations"
email). As a developer of large enterprise applications, these benefits
are very important to me.
Static Analyzers need to be designed for Languages, rather than Languages
being designed for Static Analyzers.
You really have this backwards. Why do you think Facebook created Hack?
Why did Microsoft create TypeScript? Why are there serious discussions
to add optional strict typing to ECMAScript proper?
Theodore Brown
The proposed coercion rules aren't clear. The first table in the RFC
implies
that string is accepted for bool hints, and the second table says this
is
unchanged. But the examples section states that "foo" -> bool will raise
an
E_DEPRECATED
error. I'm guessing this is a mistake, since you mentioned
above that string->bool is accepted.
You're right - that was an oversight when I updated the string->bool and
int->bool. Removed. Thanks!
You also stated above that false->bool is not supported (I'm guessing
you
meant float->bool). So users would be able to pass "4.3" to a bool
typehint,
but not 4.3? This behavior seems very arbitrary and confusing.
It may be confusing, but only academically so. Again, this approach tries
to work well with real world usage - and reject conversions which are
likely to be erroneous. "4.3" may look like a floating point number but
in the context of conversion to boolean, that doesn't matter much. It's a
string.
Not only would this RFC introduce backwards compatibility breaks in a
future
PHP release, but it still doesn't bring the same opportunities for
static
analysis which the optional strict mode in Anthony's proposal enables.The coercive RFC misleadingly contradicts itself by stating that "we
don't
believe that is the case" "that Strict STH can help static analysis in
certain
cases" "although it's possible that Strict Typing may be able to help
static
analysis in certain edge cases." Do you believe it will help in certain
cases or
not?
I don't believe strict STH would bring any meaningful gains for static
analysis, no. Unlike the JIT/AOT advantages (on the same code) which I
believe I've proven cannot be gained, I can't prove this one - because you
can infer slightly different data from strict type hints. But again, in
edge cases. Such as:
function bar(float $x)
$foo = 1;
bar($foo); // will definitely fail in strict mode
Does it give you much? Not really.
Strict types can absolutely help - not only for static analysis, but
also for
potential future AOT/JIT development. Why? Because only types need to be
checked, rather than values.
I'm sorry, but that is simply wrong. Why? Because types ARE in fact a
part of the value in PHP. They're an element of PHP's value structure
(zval). You do NOT know the value with certainty until runtime, because
again, the type IS in fact a part of the value. The very same type
inference you can do with strict types you can do with coercive or even
with weak types. Again, this has been discussed in length in the threads
that took place several weeks ago. I believe even Anthony concluded that
for a given piece of code - assuming it's unchanged - there are no AOT/JIT
advantages. The theory is that there can be stronger static analysis,
which will provide more insightful suggestions to the developer to change
their code which will in turn result in code that can be JIT'd or AOT'd
better. Here too, we don't believe that's the case and believe you can
provide the same value with coercive type hints.
This is why an optional strict type system in
JavaScript makes it possible for Google to make "early and aggressive
optimisations" in V8 (see https://developers.google.com/v8/experiments).
It also makes it possible to catch errors earlier in development (see
the
example in my "A different user perspective on scalar type declarations"
email). As a developer of large enterprise applications, these benefits
are
very important to me.
I'm not saying that type hints don't make compile-time type inference
easier or more powerful. I am saying there's zero difference between
coercive type hints and strict type hints (and, also, that given we only
know the types with certainty right after the entry point to the function,
it gives you a lot less value than you might think - in both cases).
Please do see the lengthy threads we had on this subject a few weeks ago.
Static Analyzers need to be designed for Languages, rather than
Languages being designed for Static Analyzers.You really have this backwards. Why do you think Facebook created Hack?
Why did Microsoft create TypeScript? Why are there serious discussions
to
add optional strict typing to ECMAScript proper?
I hardly think that static analyzers were the key reason for that at all,
but either way, that's my opinion. Again, note that I'm not arguing
there's zero advantage gained from adding type information to code - from
both reliability and performance perspectives. I am saying there's zero
difference on whether we coerce or error out in case we get a certain
value.
Zeev
Zeev,
You also stated above that false->bool is not supported (I'm guessing
you
meant float->bool). So users would be able to pass "4.3" to a bool
typehint,
but not 4.3? This behavior seems very arbitrary and confusing.It may be confusing, but only academically so. Again, this approach tries
to work well with real world usage - and reject conversions which are
likely to be erroneous. "4.3" may look like a floating point number but
in the context of conversion to boolean, that doesn't matter much. It's a
string.
Doesn't that contradict what you've been saying the whole time about
how "13" and 13 and 13.0 are really "the same thing", and as such
should be coerced?
function bar(float $x)
$foo = 1;
bar($foo); // will definitely fail in strict mode
No, actually it won't fail in strict mode:
https://wiki.php.net/rfc/scalar_type_hints_v5#integers_should_be_accepted_for_strict_float_arguments
Anthony
-----Original Message-----
From: Anthony Ferrara [mailto:ircmaxell@gmail.com]
Sent: Wednesday, March 11, 2015 8:28 PM
To: Zeev Suraski
Cc: Theodore Brown; internals@lists.php.net
Subject: Re: [PHP-DEV] RE: [VOTE][RFC] Coercive Scalar Type HintsZeev,
You also stated above that false->bool is not supported (I'm guessing
you
meant float->bool). So users would be able to pass "4.3" to a bool
typehint,
but not 4.3? This behavior seems very arbitrary and confusing.It may be confusing, but only academically so. Again, this approach
tries to work well with real world usage - and reject conversions
which are likely to be erroneous. "4.3" may look like a floating
point number but in the context of conversion to boolean, that doesn't
matter much. It's a string.Doesn't that contradict what you've been saying the whole time about how
"13" and 13 and 13.0 are really "the same thing", and as such should be
coerced?
No, it does not. "13" and 13 and 13.0 are the same thing when you're
expecting an integer, and rejecting "13" as an integer is nothing but noise
in the vast majority of cases - noise that will push towards an explicit
cast. Strings, however, are treated in a totally different way that places
zero meaning on their numeric value across the entirety of PHP when in the
context of a boolean.
function bar(float $x)
$foo = 1;
bar($foo); // will definitely fail in strict modeNo, actually it won't fail in strict mode:
https://wiki.php.net/rfc/scalar_type_hints_v5#integers_should_be_accepted
_for_strict_float_arguments
You're right, sorry. Reverse it then:
function bar(int $x)
$foo = 1.0;
bar($foo); // will definitely fail in strict mode
Zeev
From: Anthony Ferrara [mailto:ircmaxell@gmail.com]
function bar(float $x)
$foo = 1;
bar($foo); // will definitely fail in strict modeNo, actually it won't fail in strict mode:
https://wiki.php.net/rfc/scalar_type_hints_v5#integers_should_be_accepted
_for_strict_float_argumentsYou're right, sorry. Reverse it then:
function bar(int $x)
$foo = 1.0;
bar($foo); // will definitely fail in strict mode
And it should fail, as logically it is the same as:
$foo = 1.901 - 0.709 - 0.192;
bar($foo);
which is only 1, if you look at it with a small enough precision.
cheers,
Derick
-----Original Message-----
From: Derick Rethans [mailto:derick@php.net]
Sent: Wednesday, March 11, 2015 10:37 PM
To: Zeev Suraski
Cc: Anthony Ferrara; internals@lists.php.net
Subject: RE: [PHP-DEV] RE: [VOTE][RFC] Coercive Scalar Type HintsYou're right, sorry. Reverse it then:
function bar(int $x)
$foo = 1.0;
bar($foo); // will definitely fail in strict modeAnd it should fail, as logically it is the same as:
$foo = 1.901 - 0.709 - 0.192;
bar($foo);which is only 1, if you look at it with a small enough precision.
I'm not sure what you're trying to say. The two pieces are indeed
identical from all points of view - static analysis, how it would behave
in strict type hinting, weak type hinting or coercive type hinting.
Zeev
Zeev,
-----Original Message-----
From: Derick Rethans [mailto:derick@php.net]
Sent: Wednesday, March 11, 2015 10:37 PM
To: Zeev Suraski
Cc: Anthony Ferrara; internals@lists.php.net
Subject: RE: [PHP-DEV] RE: [VOTE][RFC] Coercive Scalar Type HintsYou're right, sorry. Reverse it then:
function bar(int $x)
$foo = 1.0;
bar($foo); // will definitely fail in strict modeAnd it should fail, as logically it is the same as:
$foo = 1.901 - 0.709 - 0.192;
bar($foo);which is only 1, if you look at it with a small enough precision.
I'm not sure what you're trying to say. The two pieces are indeed
identical from all points of view - static analysis, how it would behave
in strict type hinting, weak type hinting or coercive type hinting.
I think Derick's point is more that: http://3v4l.org/b4S8d
var_dump($foo); gives float(1) while it is != 1 (even loose).
So debugging is weird, since it looks like it should work (hey, it
says float(1), why isn't it passing for an int?). In strict mode, it's
an error because it's a float.
So it's not quite identical. Since logically it should work under your
rules, but it doesn't work. And there's nothing to the user that
implies or confirms that it shouldn't work.
Anthony