0

I have function like this :

private static function myfun(
    string $param1,
    :xhp $param2,
  ): :xhp {
    return
      //somethinf
  }

I don't want to pass any thing as param2. How can I do that ? When I try doing like :

myfun("Hi",null), 

It shows me error.

danish sodhi
  • 1,793
  • 4
  • 21
  • 31

2 Answers2

2

To be able to pass null in, your type hint has to allow it. In Hack, this is done by using a nullable type.

private static function myfun(
    string $param1,
    ?:xhp $param2,
): :xhp {
    return
      //somethinf
}

The Hack type checker will then also make sure that you check $param2 is not null before using it.

0

Does this the trick? private static function myfun( string $param1, :xhp $param2=null, ): :xhp { return //somethinf }

Ivo P
  • 1,722
  • 1
  • 7
  • 18