7

I decided to start a new project to get into hacklang, and after fixing some if the problems I initially ran into transitioning from php habits, I ran into the following errors:

Unbound name: str_replace
Unbound name: empty

Doing some research I found that this is due to using 'legacy' php which isn't typechecked, and will error with //strict.

That's fine and all, empty() was easy enough to replace, however str_replace() is a bit more difficult.

Is there an equivalent function that will work with //strict? Or at least something similar.

I'm aware that I could use //decl but I feel like that defeats the purpose in my case.

Is there at least any way to tell which functions are implemented in hack and which are not in the documentation as I couldn't find one?

For reference (though it isn't too relevant to the question itself), here is the code:

<?hh //strict
class HackMarkdown {
    public function parse(string $content) : string {
        if($content===null){ 
            throw new RuntimeException('Empty Content');
        }
        $prepared = $this->prepare($content);

    }
    private function prepare(string $contentpre) : Vector<string>{
        $contentpre = str_replace(array("\r\n","\r"),"\n",$contentpre);

        //probably need more in here
        $prepared = Vector::fromArray(explode($contentpre,"\n"));
        //and here
        return $prepared;
    }
}
Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
Tiksi
  • 73
  • 1
  • 4

1 Answers1

5

You don't need to change your code at all. You just need to tell the Hack tools about all the inbuilt PHP functions.

The easiest way to do this is to download this folder and put it somewhere in your project. I put it in a hhi folder in the base of my project. The files in there tell Hack about all the inbuilt PHP functions.

Most of them don't have type hints, which can lead to Hack thinking the return type of everything is mixed instead of the actual return, that is actually correct in most cases as, for example, str_replace can return either a string or a bool. However, it does stop the "unbound name" errors, which is the main reason for adding them.

  • This is exactly what you need to do. The relevant documentation lives here: http://docs.hhvm.com/manual/en/install.hack.bootstrapping.php#install.hack.bootstrapping.stdlib – Gabe Levi Mar 30 '14 at 16:12
  • 1
    Important notes from the docs, as well: 1) that folder is installed in `/usr/share/hhvm/hack` if you are using our Debian or Ubuntu binary packages, so it may already be on your system. 2) You need to actually copy, not symlink, the folder, [due to limitations of inotify in incremental mode](https://github.com/facebook/hhvm/issues/2160). – Josh Watzman Mar 30 '14 at 17:12
  • Thank you! This is exactly what I was looking for. I'd already written my own versions of `explode` and `str_replace` and was not looking forward to doing the rest. Not sure how I missed that in the docs. And thank you for the tip about the symlink, I was about to do just that. – Tiksi Mar 30 '14 at 20:13
  • well, the files say don't include us!. [Reference](https://github.com/facebook/hhvm/blob/master/hphp/hack/hhi/ArrayIterator.hhi#L15) – Steel Brain Oct 11 '14 at 05:38