Sco*_*rod 2 generics syntax f#
我可以通过各种方式声明一种在泛型类型上运行的类型?
我看到了以下语法:
// There's a generic type called Aggregate that will be operated on by the Id type
type 'Aggregate Id = Created of 'Aggregate
Run Code Online (Sandbox Code Playgroud)
上面的语法是另一种做出以下声明的方法吗?
// The Id type operates on a generic type called 'Aggregate
type Id<'Aggregate> = Created of 'Aggregate
Run Code Online (Sandbox Code Playgroud)
我试图参考以下文档.但是,我没有看到替代技术的例子.
据我所知,第一个是从OCaml/ML中获取的旧语法.
就单参数泛型而言,两者都是相同的.多参数泛型
type 'a 'b Id = ...
Run Code Online (Sandbox Code Playgroud)
不工作.你必须这样做
type ('a, 'b) Id = ...
Run Code Online (Sandbox Code Playgroud)
我发现这种语法非常不幸,因为所有.NET - 接口代码和文档都将始终使用C#语法
type Id<'a, 'b> = ...
Run Code Online (Sandbox Code Playgroud)
VSCode/Ionide(以防您使用它们)也使用C#表示法.我个人也转而使用C#Notation来处理单个params泛型,只是为了在任何地方使用相同的符号