Inside F#

Brian's thoughts on F# and .NET

Come work on F#!

Posted by Brian on January 26, 2011

In case you haven’t heard, the F# team is hiring.  Want to work in Redmond as an F# developer, or a program manager, or a different developer?  (Or perhaps just search with keyword “F#”, for other F#-related jobs at Microsoft.)  See also Don’s blogs for info on applying.  There’s also a contract position in the UK for applied F#.

Since I dislike code-free blogs, I’ll leave you with a quick code snippet.  Ask yourself, what does this code print?  (Then try it.)

    type MyVector(x:int, y:int, z:int) =

        // expose the values as an array
        member this.Values = [| x; y; z |]

    let v = new MyVector(1,2,3)
    v.Values.[0] <- 42
    printfn "%d" v.Values.[0]

A hint: it illustrates a “gotcha” people sometimes hit when first learning the syntax for writing F# property getters.

About these ads

4 Responses to “Come work on F#!”

  1. codekaizen said

    Are you just going to leave us hanging on this one? I guessed the answer correctly (given F#’s inclination to immutability), but don’t know how to get around it!

  2. LucasC said

    My first guess would be it would print 42

    But then the phrasing was such that it suggested something sneaky my be going on…

    So now my guess is it will print 0 (I didn’t actually try it yet). The reason is that v.Values returns a newly created array on every invocation, containing the three constructor arguments. The array that is modified to contain 42 in the first slot is thrown away; the print uses a different (new) array.

    Btw – good to see a blog post appear here again, I was wondering why this great blog went so silent recently …

  3. [...] Comments (RSS) « Come work on F#! [...]

  4. @Codekaizen
    You can get around it by doing:

    > type MyVector(x : int, y , z) =
    – let values = [| x; y; z |]
    – member this.Values = values;;

    > let v = MyVector(1,2,3);;

    val v : MyVector

    > v.Values.[0] printfn “%d” v.Values.[0];;
    42
    val it : unit = ()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 30 other followers

%d bloggers like this: