TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Comparing the PHP 7 and Hack Type Systems

46 pointsby jazzdanabout 10 years ago

6 comments

Vendanabout 10 years ago
<p><pre><code> Fatal error: Argument 1 passed to myLog() must be of the type string, integer given, called in &#x2F;home&#x2F;vagrant&#x2F;basic&#x2F;main.php on line 9 and defined in &#x2F;home&#x2F;vagrant&#x2F;basic&#x2F;main.php on line 4 </code></pre> &quot;HHVM says the error occurred on line 6 where PHP says it occurred on line 9. I prefer HHVM&#x27;s approach here as it shows us where we called the function with the bad data, not where the function is defined that we are calling with bad data.&quot;<p>Hrm, PHP is telling you both line 4 and line 9, as where the function is defined and where it&#x27;s being called with bad data, which is quite nice. In comparison, HHVM is saying line 6, which is a closing brace for the myLog function. In my books, PHP&#x27;s approach is way way better then HHVM&#x27;s
评论 #9443566 未加载
jwatzmanabout 10 years ago
It&#x27;s worth pulling out what I think is the most fundamental difference between the two type systems, from the end of the article:<p><i>After writing a couple small programs in Hack I&#x27;ve realized that it&#x27;s not types themselves that make writing Hack enjoyable: it&#x27;s the tight feedback loop Hack creates between the machine and myself. Integrating the Hack type checker into my editor means that my entire codebase is analyzed in a split second as soon as I save a file. This immediately surfaces any dumb, or subtle mistakes I made. I find myself writing code fearlessly: when I forget what a function returns, I just write code that calls it with what I think it returns. If I&#x27;m wrong, the type checker will tell me immediately. I can fix it quickly, and move on.</i><p>Hack&#x27;s type system is static -- it&#x27;s enforced by a static analysis tool that instantly reports errors in all of your code. PHP can only report errors that it actually encounters at runtime. Along with the &quot;fearlessness&quot; above, it also means that you might miss you forgot to check for a null in an error case... until it explodes at runtime in production. Hack&#x27;s static type system can mechanically check for all of these immediately.
评论 #9443998 未加载
评论 #9444071 未加载
aaronharnlyabout 10 years ago
I believe that 1 == true as much as the next nerd, but still, this line:<p><pre><code> declare(strict_types=1) </code></pre> is that not a bit ironic?
评论 #9443874 未加载
评论 #9443987 未加载
评论 #9443875 未加载
ejcxabout 10 years ago
I really like writing hack and haven&#x27;t tried PHP7. It makes a huge difference having types. If you have a function<p><pre><code> function dowork(@?string $w) </code></pre> You can allow execution to continue but still log the error. It&#x27;s made me aware of all sorts of edge case bugs that I never would have been able to find before.<p>XHP has allowed me to do the same, by forcing me to write well formed html and I love it
评论 #9443695 未加载
TazeTSchnitzelabout 10 years ago
&gt; HHVM calls this a &quot;catachable&quot; fatal error. This is interesting as in the RFC the error shown actually matches HHVM&#x27;s error.<p>It <i>was</i> a &quot;catchable&quot; fatal error (i.e. E_RECOVERABLE_ERROR) in PHP 5.x for typehints. But PHP 7 is changing it to be an Exception instead, and for some reason it currently produces an inaccurate error message in the master branch - it should say &quot;Uncaught exception&quot; (which it is), but that&#x27;s not been fixed yet. Trunk builds aren&#x27;t ready for production use, etc.
allan_sabout 10 years ago
For nullable types, there&#x27;s a plan&#x2F;rfc to have them too<p><a href="https:&#x2F;&#x2F;github.com&#x2F;php&#x2F;php-src&#x2F;pull&#x2F;1045" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;php&#x2F;php-src&#x2F;pull&#x2F;1045</a> <a href="https:&#x2F;&#x2F;wiki.php.net&#x2F;rfc&#x2F;nullable_types" rel="nofollow">https:&#x2F;&#x2F;wiki.php.net&#x2F;rfc&#x2F;nullable_types</a><p>not for 7.0 but hopefully for 7.1