5

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?

Calpau
  • 921
  • 10
  • 21

2 Answers2

6

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.

Community
  • 1
  • 1
Gabe Levi
  • 1,917
  • 15
  • 15
  • The project I have in mind is an embedded ARM system. I know Facebook said they were moving toward running on ARM, although it's not clear to me if it's finished and working. The typechecker seems useful in and of itself. I wonder if it could be modified to also support the commented-type-annotations in normal PHP (provided you don't use the unsupported PHP features the HHVM website mentions). This could be a good "first step" to moving to HHVM, to begin annotating types in regular PHP, using the typechecker and removing unsupported PHP features before moving to ?hh – Calpau Mar 22 '14 at 18:06
  • And now that I think about it, another way would be to maintain your code in PHP-compatible Hack and have your build process strip out the type annotations, etc. and make legitimate PHP when deploying to production. Then you could use the typechecker as a daemon. – Calpau Mar 23 '14 at 16:44
  • 2
    I work on HHVM. Since you say "embedded" ARM, I assume you don't mean ARMv8, the 64-bit server-oriented ARM architecture that isn't widely available yet. ARMv8 is the only kind of ARM we're working on porting HHVM to; the chances that we'll ever port HHVM to any 32-bit architecture are pretty much zero. Sorry :/ – Owen Yamauchi Mar 25 '14 at 16:01
0

This is exactly what we did in-house in our development division.

We made a script to convert code between hacklang and php as we wanted to be able to do the type checking without converting our production servers to hhvm (we are planing to do so)

You can find the script on my github page
https://gist.github.com/Chipcius/d3dd4052b07a152870bd#file-hacklang-php-juggler-php

You can convert you files by passing in a directory and a flag to decide the conversion level (decl, partial, strict)

After conversion you can run hh_client just as you were coding hacklang

When you want to turn back you can run the same script on your code with the php flag and it comments out the annotations that need commenting.


workflow example

php hacklang-php-juggler.php <myDir> hack
hh_client
php hacklang-php-juggler.php <myDir> php
Chipcius
  • 1
  • 1
  • 1