Question

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;
    }
}
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top