문제

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:??) = ...

도움이 되었습니까?

해결책

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 ]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top