Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Type inference in F#

2010-08-29

I have recently been to a short F# presentation at HiQ in Arboga. One of the things that got mentioned was type inference. Both Visual Basic and C# have type inference in their current version, but F# takes this one step further. This approach will produce two integers in VB when Option Infer is set to On:

Dim X = 10
Dim Y = 20

This is the F# version of the above code:

let mutable x=10
let mutable y=20

But when it comes to function declarations, VB is not support type inference anymore. This will not compile:

Private Function DoSomething(ByVal X, ByVal Y)
   Return X
End Function

You will need to declare the input types and the return type, like so:

Private Function DoSomething(ByVal X As Integer, ByVal Y As _
   Integer) As Integer
   Return X
End Function

In F#, type inference will work all the way. This is the F# version of the DoSomething function, with type inference:

let DoSomething x y = x

The DoSomething function can be called using any arguments. Type inference will take care of the argument and return typing. Once the function is called, the compiler knows what types the function will handle. Therefore, you can call the function using strings or integers but not a combination. Here, z1 will be a string and z2 will be an integer. This is the complete code listing:

let mutable x=10
let mutable y=20

let DoSomething x y = x
let z1 = DoSomething "winsoft" "rocks"
let z2 = DoSomething 10 20

Now, this is strange: If the types interact with each other, the first call will determine the types of the input and output. If I change my code, so that the function DoSomething returns x + y, only the first call will be accepted.

let mutable x=10
let mutable y=20

let DoSomething x y = x + y
let z1 = DoSomething "winsoft" "rocks"
//let z2 = DoSomething 10 20

The second call will fail, because the compiler in this case only provides a string version of the DoSomething function.

Categories: Microsoft .NET

Tags: F#

One response to “Type inference in F#”

  1. Carl-Johan Lundberg says:

    You can even go so far to have F# implement units of measurement, http://blogs.msdn.com/b/andrewkennedy/archive/2008/08/29/units-of-measure-in-f-part-one-introducing-units.aspx. This makes it possible to catch errors like

    speedOfImpact = sqrt(2.0 * gravityOnEarth + heightOfDrop) where the addition will trigger an error during compilation, as the resulting type is not of (m/s ^2)

Leave a Reply

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



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips