Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use the special generic syntax for my own types?

Tags:

generics

f#

In F# some types have a special generic syntax (I'm not sure what it is called) so that you can do:

int list // instead of List<int>
int option // instead of Option<int>
  • What is this syntax called?
  • Can I enable it for my own types?
like image 715
sdgfsdh Avatar asked Jan 27 '23 21:01

sdgfsdh


1 Answers

It is covered in the MSDN on F# Types

Under "generic type":

generic type

type-parameter generic-type-name | 'a list

Or

generic-type-name<type-parameter-list> | list<'a>

And "constructed types":

constructed type (a generic type that has a specific type argument supplied)

type-argument generic-type-name

or

generic-type-name<type-argument-list>

type dave<'a> = {
    V : 'a
};;

let stringDave: dave<string> = { V = "string" };;
//val stringDave : dave<string> = {V = "string";}

let intDave : int dave = { V = 123 };;
//val intDave : dave<int> = {V = 123;}

like image 55
DaveShaw Avatar answered Jan 30 '23 13:01

DaveShaw