问题
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