Generic functions and types enable you to write code that works with a variety of types without repeating the code for each type.
You can make a function generic by using the single quotation ('
) mark in a type annotation to indicate that a parameter type is a generic type parameter, as shown below.
let func1 (x: 'a) (y: 'a) =
Console.WriteLine("{0}, {1}", x, y)
In the above example, func1
is generic because its parameters are declared using a single quotation ('
), as type parameters. You can call this function by passing different types of parameters as shown below.
func1 7 5
func1 22.2 3.1
func1 "hello" "world"
You can also make a function generic by explicitly declaring its type parameters in angle brackets (<type-parameter>
).
The following example shows the explicitly generic constructs.
let func2<'T> x y =
Console.WriteLine("{0}, {1}", x, y)
func2<int> 7 5