通用参数的空列表

发布于 2024-12-06 23:50:43 字数 349 浏览 0 评论 0原文

规范第 15.3 节提到序列 < > 中间有空格是允许的形式。它表示通用参数的空列表,这允许出现以下奇怪的情况。

type A() = class end
let a = new A< >()

为什么这是允许的?由于泛型类型可以在省略类型参数的情况下实例化,这是否是一种类型检查优化?

Section 15.3 of the spec mentions the sequence < > with intervening whitespace is an allowed form. It indicates an empty list of generic arguments, which allows for the following oddity.

type A() = class end
let a = new A< >()

Why is this allowed? Since generic types can be instantiated with type args omitted, is this a type checking optimization of sorts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-12-13 23:50:43

我同意 Ramon 的观点,它使事情更加一致,因为您可以将非泛型类型视为泛型 arity 0 类型的退化情况。对于泛型 arity“重载”的类型,这允许您更明确地说明您所指的类型:

type T(o:obj) = class end
type T<'t>(t:'t) = class end

let t = T("test")
let t' = T< >("test")
let t'' = T<_>("test")

在不仔细考虑重载解析的情况下,t 的类型不一定很明显,但完全清楚 t't'' 有。

I agree with Ramon that it makes things more consistent, in that you can treat non-generic types as a degenerate case of types of generic arity 0. In the case of types which are "overloaded" by generic arity, this allows you to be more explicit about which type you're referring to:

type T(o:obj) = class end
type T<'t>(t:'t) = class end

let t = T("test")
let t' = T< >("test")
let t'' = T<_>("test")

Without thinking carefully about overload resolution, it's not necessarily obvious what type t has, but it's completely clear what types t' and t'' have.

半夏半凉 2024-12-13 23:50:43

我认为这只是为了一致性。您可以执行 ListDictionary,因此您也可以执行 int<; >

I think it is just for consistency. You can do List<int> and Dictionary<string, bool>, so you can also do int< >.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文