Scott Nimrod
Scott Nimrod

Reputation: 11595

What are the various ways that I can declare a type that operates on a generic type?

What are the various ways that I can declare a type that operates on a generic type?

I saw the following syntax:

// There's a generic type called Aggregate that will be operated on by the Id type
type 'Aggregate Id = Created of 'Aggregate 

Is the syntax above an alternative way of making the following declaration?

// The Id type operates on a generic type called 'Aggregate
type Id<'Aggregate> = Created of 'Aggregate 

I attempted to reference the following documentation. However, I did not see an example of an alternative technique.

Upvotes: 2

Views: 53

Answers (1)

robkuz
robkuz

Reputation: 9904

As far as I know the first one is the old syntax taken from OCaml/ML.

Both are the same insofar as single parameter generics are concerned. Multi param generics like

type 'a 'b Id = ...

do not work. you will have to do

type ('a, 'b) Id = ...

I find that syntax very unfortunate as all .NET - interfacing code and documentation will always use the C# syntax

type Id<'a, 'b> = ...

And VSCode/Ionide (just in case you use them) also uses the C# notation. I personally also switched to C# Notation for single params generics as well simply to have the same notation everywhere

Upvotes: 6

Related Questions