【发布时间】:2015-01-23 22:40:43
【问题描述】:
在 F# 中稳健地测试 null 的正确方法是什么?
我有一个基于 Unity 游戏引擎(这是一个闭源单声道 c#/c++ 引擎)构建的混合 F#/C# 项目。
我有一个 F# 函数,它调用一个可能返回 null 的 Unity API 函数。 Unity 函数返回 null 但我的 F# 代码无法检测到这一点(我从测试数据的形状、附加调试器、插入日志语句等方面都知道这一点)。我编写的每个空测试似乎都在应该为真时返回假。第一次尝试:
let rec FindInParents<'t when 't : null> (go : GameObject) =
match go with
| null -> null
| _ ->
let comp = go.GetComponent<'t>() // GetComponent returns null
match comp with
| null -> FindInParents (go.transform.parent.gameObject) // This should be matched but isn't
| _ -> comp // Always this branch
我也尝试了以下方法但没有成功:
let rec FindInParents<'t when 't : null> (go : GameObject) =
if obj.ReferenceEquals (go, Unchecked.defaultof<'t>) then null
else
let comp = go.GetComponent<'t>() // Comp is null
if obj.ReferenceEquals (comp, Unchecked.defaultof<'t>) then FindInParents<'t> (go.transform.parent.gameObject)
else comp // Always this branch
我觉得我在这里遗漏了一些基本的东西,但到目前为止它一直让我望而却步。有什么指点吗?
编辑:我还应该指出,GetComponent 始终返回 UnityEngine.Component 的子类型,并且始终是引用类型。 UnityEngine.Component 是 UnityEngine.Object 的子类型,它定义了一个自定义 == 运算符(我认为这无关紧要,因为在第二个示例中不应调用 == (请参阅 Daniel 对 [Handling Null Values in F#)的回答
【问题讨论】:
-
Those should work. 正在发生其他事情。
-
"在 F# 中稳健地测试 null 的正确方法是什么?" 请参阅 this question and answers。您的第二种方法看起来不错。