F# : Values, Data Types and Type Inference

 

 

Hey Guys,

In this post i am going to talk about Values, Data Types and Types Inferences in F#.

 

Values in F# are what Variables are in some other languages. They are declared and assigned using the keyword “let”. By default simple values are immutable and thats the reason why they are not called variables. Their types are automatically inffered automatically by assignment. But this does not mean that F# is a dynamic language. It is still a statically typed language but its just that the type inference works so well that you would rarely find any explicitly assigned types. For litrels F# has a complete list of the suffixes available. Some of them are u for uint, y for byte, L for int64, l for bigint, m for decimal and so on.

If you would have worked with any of the programming languages you would know that a variable is used to hold a value. F# has variable as well but it also has values which are not similar to variables so they cannot be chaged at runtime.

You can declare a variable in F# by sinpmly writing

let  myVariable = 100

and now when you hover over the mouse on the variable you would see that F# has inferred it as int. But we cannot chage the value of myVariable and when we try to assign a value to the  myVariable then the compile would give a warning that it would expect the value comparision instead of a new assignment to value of  myVariable.

F# : Values, Data Types and Type Inference

Immuteable Variable

The compiler is catually comparing if myVariable has value 111 and its not reassigning it. Also if you try to reassign the value of myVariable saying

let myVariable = 111 then we will get an error that the definition of myVariable is being duplicated.

But if you try to do the same thing in the interactive enviorment (for details of how to use the interactive enviorment visit F# Interactive enviorment/) it would work fine as the interactive enviorment is designed to work for the evaluation and revaluation. But this will not work in the application context.

Now let type the different types of values for different variables and mouse over over them and see how they are inferred.

F# : Values, Data Types and Type Inference

Data Types in F#

You could take your mouse over each of the variables and could see the way it is inferred and type other types as well and see if they are correctly inferred or not.

So we can see that the standard value types in F# are not muteable..

Any Comments, Questions and Feedback are most welcome…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">