If you’re reading this I’m assuming that you have a background in software development (.NET in particular) so I won’t do more than show the keyword to .NET type mappings and highlight a few notable items.
As a .NET language F# supports the same primitives as the traditional .NET languages. Also like other .NET languages, F# supports suffixes on most numeric types to remove ambiguity. Using suffixes in your code can help the type inference engine resolve types, reducing the need for explicit type annotations.
Mappings
F# Keyword | .NET Type | Suffix |
---|---|---|
bool | System.Boolean | |
unit | N/A | |
void | System.Void | |
char | System.Char | |
string | System.String | |
byte | System.Byte | uy |
sbyte | System.SByte | y |
int16 | System.Int16 | s |
uint16 | System.UInt16 | us |
int (or int32) | System.Int32 | l (optional) |
uint (or uint32) | System.UInt32 | u |
int64 | System.Int64 | L |
uint64 | System.UInt64 | UL |
nativeint | System.IntPtr | n |
unativeint | System.UIntPtr | un |
decimal | System.Decimal | m (or M) |
float (or double) | System.Double | |
float32 (or single) | System.Single | f (or F) |
Although it is not a primitive type, F# also exposes System.BigInteger via the bigint keyword for computations with integers larger than 64-bits.
Unit
You should already be familiar with most of the types listed above but there’s one type that’s specific to F#. The unit type, denoted by (), represents the absence of an actual value but should not be confused with null or void. The unit value is generally used where a value would be required by the language syntax but not by the program logic such as the return value for functions that are invoked for their side-effect(s) rather than their result.
Type Conversions
One important way that F# differs from other .NET languages is that it does not allow implicit type conversions because of bugs that can arise due to type conversion errors. Instead we can explicitly convert values using the built-in conversion functions.
(float 1) + 2.0 |> printfn "%A"
In the example we convert the int value to float by passing it to the float function and add it to the existing float value.
One comment
Comments are closed.