What is the syntax for OCaml style generic type parameters in F#?

ぐ巨炮叔叔 提交于 2020-08-07 05:30:10

问题


According to this answer, F# supports OCaml style type parameters. The example in the question is:

type 'a NestedList = List of 'a NestedList list | Elem of 'a

However, I could not find this syntax documented anywhere in the F# documentation and moreover, I cannot get the F# compiler accept the syntax in the answer I gave the link to. This attempt to use multiple parameters are not accepted by the compiler:

type ('a * 'b) SomeType = ('a * 'b)

This however, works:

type ('a , 'b) SomeType = ('a * 'b)
let x:SomeType<int,int> = (4,5)

Based on the type annotation Rider displays above x, I'm assuming this is the accepted syntax, but I'd like to know where this is documented and if I did get it right.


回答1:


You are right. The answer you link to is wrong. type ('a * 'b) someType is not valid in OCaml either. Multiple type parameters should be separated by comma there as well: type ('a, 'b) someType.

The F# language, including its syntax, is specified in The F# Language Specification. See in particular chapter 5 (in the 4.1 specification) for the syntax of types and chapter 8 for type definitions.



来源:https://stackoverflow.com/questions/63198415/what-is-the-syntax-for-ocaml-style-generic-type-parameters-in-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!