Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hybrid PHP/Hacklang: Use the typechecker on regular PHP with commented type annotations

Tags:

php

hacklang

I can't build hhvm at the moment for lack of access to a 64-bit VM, so I haven't been able to use the typechecker that they have. Their documentation doesn't seem to describe the operation of the typechecker (hh_server and hh_client?) in any detail.

What I'm wondering, for anyone who's used it, is if the typechecker could be used in this situation:

Let's say someone can't convert their PHP codebase to Hack, so they instead write their PHP with comments in the form of hacklang type annotations, and at build time use a tool to strip the comments out, make a hh file, run the typechecker and report errors.

E.g. original PHP:

<?php
function lar(/* int */ $x)/* : int */
{
    return $x;
}

Make a copy of the above, strip out comments, change ?php to ?hh :

<?hh
function lar(int $x): int
{
    return $x;
}

Run it through the typechecker and see if it produces errors.

That way you'd get access to legitimate type checking with normal PHP without the need for running it on HHVM. Does the typechecker run in a way amenable to this set up?

like image 985
Calpau Avatar asked Mar 22 '14 13:03

Calpau


1 Answers

I am an engineer at Facebook who works on Hack. You definitely could do this and I wouldn't say it's a bad thing to do, but you'd be missing out on a bunch of great features. The Hack typechecker can be run at build time (hh_server --check /path/to/www), but the best way to run the typechecker is as a daemon. Since the daemon incrementally checks your code in the background, it can report the errors very quickly whenever asked. This allows you to get feedback while you are writing your code rather than after you have finished. This quick feedback loop really helps speed up development.

Some other things that you would be missing out on:

  • Many language features, like Collections, lambda expressions, runtime enforcement of type annotations, and trailing commas (Paul Tarjan's personal favorite)
  • HHVM's massive performance boost.

So if you absolutely can't use HHVM then this might be worth considering, but if you can then I strongly recommend HHVM in order to reap the full benefits of Hack.

like image 173
Gabe Levi Avatar answered Oct 19 '22 00:10

Gabe Levi