Type Inference in F#


Type Inference

F# is a strongly typed language. Strongly Typed means that you can not use a function with a value that is inconsistent and also you can't call a function that has a string as a parameter with an integer argument, you must explicitly convert the two. F# is a strong type, but frequently code will not contain many type annotations. This is because F# uses Type Inference to automatically determine the type of expressions.

Type inference checks that the type matches in obvious ways, which means that a function definition is consistent and the definition of the function is sufficiently general to match all the ways it is used. The way the language treats the type of its value refers to as its type system. F# has a type system that does not get in the way of routine programming. Type Inference is a global process over an entire source file, or in the case of F# interactive, over the scope of a single entry delimited by (;;) semicolon also known as terminators. In F# all values have a type, and this includes values that are functions.

The idea of Type Inference is that you do not have to specify the types of F# constructs except when the compiler can not conclusively deduct the type or you can say that you don't need to explicitly declare the type; the compiler will determine the type of a value from the types of the literals in the function and the resulting types of other functions it calls. Omitting explicit type information does not mean that F# is a dynamically typed language. This process is generally referred to as Type Inference.

You have to follow following step for Display Type Inference on console.

Step 1: Click on Start In Task-bar -> then go in All Programs. Like the following image.

start

Step 2: When you Click this will show the All Programs list -> select Microsoft Visual Studio 2010.

all program

Step 3 : Then go to the third option Microsoft Visual Studio Tools -> click on Visual Studio Command Prompt(2010). 

visual studio Toolsvisual studio tools

The Command Prompt will open like below.

visual studio command prompt

Step 4: Then type fsi.exe and press Enter; the command prompt window will then look like below.

Fsharo console interactive

Now Creating Example for Type Inference.
Example:

The example shows two F# identifiers and than shows their inferred types display on console.

let oneString=" Type Inference Example";;

This will show you Result like below.

val oneString : string = " Type Inference Example"

Other you can try for Integer.

let oneInt=06;;
Result

val oneInt : int = 6

F# interactive console will look below:
Type Inference example result

The way Type Inference works in F# is very simple to understand; the compiler works through the program, assigning types to identifiers as they are defined.

The types of these two above identifiers are - string and Int. The syntax used by the compiler to describe them is also very straight; the keyword val means value and then the identifier, a colon and lastly the type.

Automatic Generalization

When F# determines the type of a value, function or Data Structure, it looks at context such as function parameter, the function body, how parameters are combined in operations etc. Type Inference automatically generalizes code to be as generic as possible. The compiler examines each parameter and determines whether the function has a dependency on a specific type of that parameter. If it does not, the type is inferred to be generic. The compiler performs automatic generalization only on a complete function definition that has an explicit argument and simple immutable values.

If you explicitly specify a type in your function parameter list or body of the function, you prevent automatic generalization on that value; however F# can still apply automatic generalization to the other parameters and values.
Summary
In this article I have covered Type Inference in F#.

Up Next
    Ebook Download
    View all
    Learn
    View all