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

Viewed 190

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. Rider screenshot

1 Answers

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.

Related