misaochan
misaochan

Reputation: 890

What do 'T and 'U mean in F#?

Such as in the http://msdn.microsoft.com/en-us/library/ee370378.aspx example List.map : ('T -> 'U) -> 'T list -> 'U list .

Sorry if this sounds simplistic, but Googling it turns up no explanations.

Thanks!

Upvotes: 2

Views: 301

Answers (2)

John Palmer
John Palmer

Reputation: 25516

In this case 'T and 'U are arbitrary types.

In other cases, restrictions might be placed on 't using constraints.

Occasionally the names may be descriptive like 'key or 'value. In F#, the key is that a ' before a type makes it generic.

Similarly, types may be prefixed with a # which allows for any type which can be upcast to the given type.

More complex type constraints using inline methods can be used with a ^ before the type name allowing for member constraints which are not possible using the .Net type system and will only work with F# inline functions.

Upvotes: 5

manojlds
manojlds

Reputation: 301327

From the specifications:

A type of the form 'ident is a variable type. For example, the following are all variable types:

'a
'T
'Key

And for your question on why 'T and not 'a:

Note: this specification generally uses uppercase identifiers such as 'T or 'Key for user-declared generic type parameters, and uses lowercase identifiers such as 'a or 'b for compiler-inferred generic parameters.

http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec.html

In this case, all it means is List.map : ('T -> 'U) -> 'T list -> 'U list takes in a function that converts 'T type value to 'U type, a list of 'T type values and returns a list of ''U' type values - which is, of course, what a map does.

Upvotes: 3

Related Questions