Hack introduces a new way to handle null values by introducing the concept of Nullable.
? Operator is used for representing Nullable type.
The Nullable type is used with primitive types ( like int, bool).
Function receiving Null argument:
<?hh
function checkValue(?int $val): int {
  if ($val === null) {
    return -1;
  } else {
    return $val;
  }
}
Function receiving Null argument:
<?hh
function checkValue(int $val):? int {
     if ($val > 0) {
           return val;
     } else {
           return null;
     }
}
Passing class Object as Null argument:
<?hh
class MyClass {}
class NewClass{
public function CheckNull(?MyClass $obj): string {
      if ($obj === null) {
           return "Null";
      } else {
           return "Object not null";
      }
   }
}

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.