gpt4 book ai didi

f# - 覆盖 ToString 和 `%A` 说明符

转载 作者:行者123 更新时间:2023-12-01 10:28:37 26 4
gpt4 key购买 nike

如果覆盖 ToString在一个类型中

type TestMe ()=
override __.ToString() = null

然后我通过 "%A" 输出它说明符

printfn "*%A*" (TestMe())

为什么会抛出 System.NullReferenceException

我希望它表现得像

printfn "*%A*" null

它只是打印 <null>无一异常(exception)。

最佳答案

本期got fixed in F# 4.1 ,它现在明确处理 ToString() 返回 null 的情况。在此修复之前,null 会向上移动并最终被取消引用。

升级您的 FSharp.Core 版本将为您解决问题。

话虽如此,我想指出从 ToString 返回 null 通常是一个很大的禁忌。连MSDN docs有关于它的具体警告。

关于f# - 覆盖 ToString 和 `%A` 说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45260047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com