gpt4 book ai didi

f# - 用 null 解决重载歧义

转载 作者:行者123 更新时间:2023-12-04 18:07:53 25 4
gpt4 key购买 nike

我正在使用 F# 中的 API(我无法控制),它声明了两个构造函数,如下所示:

public TheType(string bar, bool foo)

public TheType(IDisposable baz, bool foo)

我需要使用第一个构造函数创建对象的实例,但传递 null为酒吧。 F# 编译器当然对我要解析的构造函数感到困惑:
//Private field
static let blah : TheType = new TheType(null, false)

我可以通过转换 null 来解决这个问题字符串:
static let blah : TheType = new TheType(null :> string, false)

哪个有效,但这给了我一个编译警告:

The type 'string' does not have any proper subtypes and need not be used as the target of a static coercion



这也有道理。有没有办法在不诉诸编译警告的情况下解决重载歧义?

最佳答案

您可以指定 null 的类型:

static let blah : TheType = new TheType((null : string), false)

关于f# - 用 null 解决重载歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22285324/

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