开发者

Empty list of generic arguments

开发者 https://www.devze.com 2023-04-07 09:47 出处:网络
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.

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 allo开发者_如何转开发wed? Since generic types can be instantiated with type args omitted, is this a type checking optimization of sorts?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号