> -----Original Message-----
> From: Theodore Brown [mailto:theodor...@outlook.com]
> Sent: Wednesday, March 11, 2015 11:01 PM
> To: Zeev Suraski
> Cc: internals@lists.php.net
> Subject: RE: [VOTE][RFC] Coercive Scalar Type Hints
>
> On Wednesday, March 11 2015 at 1:14 pm Zeev Suraski wrote:
>
> > 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.
>
> The benefits of strict typing for static analysis are hardly relegated
to edge
> cases. Here's a simple example:
>
> function getUserPermissions(int $userId): array function getUsername(int
> $userId): string
>
> $userId = 10;
> $user = getUsername($userId);
> $permissions = getUserPermissions($user);
>
> In strict mode static analysis can tell you that this will fail, since a
string is
> being passed to a function expecting an int. With coercive STH, all you
know
> is that it may or may not work.

That's correct, but that's not the kind of edge case I was talking about.
When seeing a string being passed to an integer type hint, with coercive
STH, your static analysis tool can employ an identical rule but instead of
saying it WILL fail, it can tell you it may fail.  You can then decide
what you want to do about that knowledge, and cast explicitly if you want
to - which you would probably do in the strict STH case.

Yes, with strict STH you'd know that more things fail during static
analysis time, because, well, it fails a lot more cases.  What's the
remedy?  Typically it would be forcing the type using an explicit cast,
which is worse across the board than a dynamic cast.  It's also not any
faster in terms of JIT - the casting code is pretty much identical to the
coercion code.  But the bigger question is, do you WANT that to fail?  Or
is it noise that the language should be dealing with automatically?  For
20 years, the answer has been the latter, and even though it became a bit
of a sport to look down at PHP's dynamic typing, it seems to have worked
out pretty well for it in terms of popularity and ecosystem.


> Of course you can do the same type inference. The difference is that
with
> coercive STH knowing the type isn't enough - you also have to know the
> value. A string passed to a function with an int type declaration MAY
succeed
> or MAY fail. You won't know until the value is checked at runtime.
> However, in strict mode a user would be required to pass a value of the
> correct type. There are many cases where PHP can check the types at
> compile time, but not the values. Therefore, with strict STH it is
possible to
> eliminate many runtime value checks and type conversions. For example:

You can't eliminate anything unless you force the developer to change the
code.  These changes would typically be some sort of explicit type
enforcement, smart (which we don't yet have) or dumb - but those
conversions contain code that is pretty much identical to the checks
weak/coercive types do.

So again, it boils down to this:
1. If you have the types in the right form, static analysis yields
identical results between coercive & strict (it will give you thumbs up in
both cases).
2. If you don't have the right types, strict analysis will outright reject
it - sensible or not.  Assuming that semantically you do want that to work
- you'd have to add a type conversion - which would result in code that
performs pretty much identically to a weak type hint.

-> no gain.

This too was in the thread from a few weeks ago in a lot more detail.

> function getFooByVal(string $val): string function getBarByFoo(int $foo)
> getBarByFoo(getFooByVal("value"));
>
> With coercive STH a runtime value check is necessary. With strict types,
a
> failure could be detected at compile time, the user would fix their
code, and
> at runtime no value check is necessary. This is a huge advantage for
AOT/JIT.

It's a third example of the same thing.  Yes, strict type hints fail a lot
more cases, and yes, it can be detected in compile time in some cases -
much like coercive STH could detect these very same cases and tell you
they *may* fail.  There's nothing new in your examples that wasn't brought
up back in that thread.

Thanks,

Zeev

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to