Ivica M.
Ivica M.

Reputation: 4823

Clarification of List<'T> definition syntax

While reading the source code of F#, I found the definition of the List<'T> type, which looks like this:

type List<'T> = 
       | ([])  :                  'T list
       | (::)  : Head: 'T * Tail: 'T list -> 'T list

Could someone, please, explain the above syntax? The compiler warns that 'This construct is deprecated: it is only for use in the F# library'. Is the syntax just an old way to define discriminated unions? If so, why does Tail has the type of 'T list -> 'T list instead of just 'T list?

Upvotes: 1

Views: 115

Answers (1)

GregRos
GregRos

Reputation: 9143

I believe what this means is the following:

type List<'T> = 
       | ([])  :                  'T list
       | (::)  : (Head: 'T * Tail: 'T list) -> 'T list

That is to say, (::) is a function that, given an ordered pair 'T * 'T list generates a 'T list.

The deprecated construct is giving type annotations to the discriminated union constructors. You never needed to do that, in any version of F#, as far as I know. I don't know why they had to do that in the F# library.

Upvotes: 2

Related Questions