Reputation: 712
I'm working on the '99 problems of F#', and saw the following definition of NestedList
:
type 'a NestedList = List of 'a NestedList list | Elem of 'a
I'm a little confused by the syntax here, as normally there is only a type name after the type
. Can someone clarify it? Thanks!
Upvotes: 2
Views: 1035
Reputation: 13577
F# has two ways of specifying a generic type:
SomeType<'a, 'b>
('a * 'b) SomeType
Those are really two ways of saying the same thing and can be used interchangeably. Which one to use is a matter of preference and code standards. Usually people use OCaml style for basic F# types like list, array or option, and C# style for user-defined and BCL ones.
Upvotes: 3
Reputation: 2871
This is a discriminated union in a condensed syntax. You can also write it as:
type 'a NestedList =
| List of 'a NestedList list
| Elem of 'a
or
type NestedList<'a> =
| List of NestedList<'a> list
| Elem of 'a
This is using generics, where the type itself is taking another type as an argument.
Upvotes: 4
Reputation: 25526
So this is a definition of a generic discriminated union with generic type 'a
.
The key idea is that each element has the type 'a
.
Upvotes: 2