1

Any specific reason why Hack doesn't automatically infer nullable types? Instead you have to write ? explicitly. If it used inference instead, wouldn't it be able to check for null for regular PHP programs as well?

For example, this function would have type bool -> ?string:

function foo($condition) {
  if ($condition) {
    return null;
  else {
    return 'a string';
  }
}
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
  • I am looking into your question, but something that would help me is: can you phrase the question in a way that is not "why does a feature that I want not exist?" Features don't exist because they were never implemented; that lack of existence doesn't need to be justified. – Eric Lippert Mar 16 '16 at 17:40
  • I'm also not sure what you're asking -- Hack *does* infer nullable types *inside* a function, but doesn't infer *any* types across function boundaries. So the input `$condition` needs to be explicitly annotated as `bool` just like the return type needs to be annotated as `?string`. Are you asking something I don't understand about nullable types in particular, or are you asking more generally why Hack only infers types inside a function but not across function boundaries? – Josh Watzman Mar 16 '16 at 18:55
  • @JoshWatzman, yes, you're right - this question is meaningless in the context that Hack doesn't do full-program inference. Also, didn't know that Hack infers nullable types inside functions, but that makes sense. Thanks for your answer. – Olle Härstedt Mar 16 '16 at 23:23

0 Answers0