【问题标题】:How to pass null argument to function that accept :xhp as parameter?如何将空参数传递给接受 :xhp 作为参数的函数?
【发布时间】:2017-11-25 12:40:51
【问题描述】:

我有这样的功能:

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

我不想将任何东西作为 param2 传递。我怎样才能做到这一点 ? 当我尝试这样做时:

myfun("Hi",null), 

它显示错误。

【问题讨论】:

    标签: hacklang xhp


    【解决方案1】:

    为了能够传入null,你的类型提示必须允许它。在 Hack 中,这是通过使用 nullable type 来完成的。

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

    Hack 类型检查器还将确保您在使用之前检查 $param2 不为空。

    【讨论】:

      【解决方案2】:

      这是诀窍吗? private static function myfun( string $param1, :xhp $param2=null, ): :xhp { return //somethinf }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-27
        • 2021-04-13
        • 1970-01-01
        • 2018-01-31
        • 1970-01-01
        • 1970-01-01
        • 2020-12-18
        • 2010-09-17
        相关资源
        最近更新 更多