Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Type Annotation For Lists

Tags:

f#

c#-to-f#

In F# what is the type annotation for a typed list (e..g list of int)? With a simple function I can do annotations as follows:

let square(x:int) = ...

I've annotated x as an int type. But what if I want to do a type annotation for an int list? For example, let's say I have a max function that expects a list - how would I do a type annotation for it?

let max(numbers:??) = ...

like image 517
t3rse Avatar asked Jan 12 '11 04:01

t3rse


People also ask

Facebook itu apa sih?

Facebook adalah media sosial dan layanan jejaring sosial online Amerika yang dimiliki oleh Meta Platforms.


1 Answers

There are two options:

let max (numbers:int list) = ... 
let max (numbers:list<int>) = ... 

The first version uses syntax that is inherited from OCaml (and is frequently used for primitive F# types such as lists). The second version uses .NET syntax (and is more frequently used for .NET types or when writing object-oriented code in F#). However, both of them mean exactly the same thing.

In any case, the form of type annotation is always (<something> : <type>) where <something> is either a pattern (as in parameter list) or an expression. This means that int list and list<int> are just names of types. F# Interactive prints the type if you enter some value, so you can use this to learn more about how type names are written:

> [1;2;3]
val it : int list = [ 1; 2; 3 ]
like image 184
Tomas Petricek Avatar answered Oct 20 '22 09:10

Tomas Petricek