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

StackOverflow https://stackoverflow.com/questions/22578151

  •  19-06-2023
  •  | 
  •  

Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top