views:

304

answers:

2

I've always believed that in F# we needed to use the rec keyword for every recursive function, for example:

let rec factorial = function
| 0 -> 1
| k when k > 0 ->  k * (factorial (k - 1))
| failwith "oops!"

Today I was playing around with F# and I came up with a code similar to the following:

let MyRecordType =
    { Something     : float;
      SomethingElse : int }
    with
        static member factorial = function
            | 0 -> 1
            | k when k > 0 ->  k * (MyRecordType.factorial (k - 1))
            | failwith "oops!"

As you see, I've just defined a recursive function, but I made what at first seemed like a mistake: I forgot to declare the function as recursive by means of the rec keyword.

But to my surprise it compiles! And there's more to it: if you add the rec keyword, then it is a syntax error!

type MyRecordType =
    { (* ... *) }
    with
        // syntax error:
        static member rec factorial = function
        (* ... *)

I've googled around for an explanation but got nothing. In the MSDN documentation, I couldn't find any mention to the rec keyword outside the page about recursive functions, and as of 2010-01-03 it does not mention the case I'm asking about.

Exactly the same thing happens with non-static members.

So, why is it a syntax error to use the rec keyword on member functions of a record-type?

Thanks.

+12  A: 

'let rec' isn't about defining recursive functions, but defining a binding in an environment, that includes the binding for the current variable to be bound. You could use 'let rec' just as well to define e.g. an infinite list. Often, you don't want the binding to be included in the environment, as you might want to access an earlier variable by the same name.

When you are defining the static member function, factorial, you aren't looking for a binding for a variable 'factorial', but for a type 'MyRecordType' (which is in the environment as a type definition), and if it happens to have a static member function called 'factorial', which it has.

Sami
Interesting. Thanks. Just a question: how **often** do we want to access an earlier value by the same name (to me it seems something at least very rare)? Why would someone need that?
Bruno Reis
True, I think the 'often' in my answer is misleading. I should have said 'sometimes'. You could always rename your new variable to something that doesn't collide with an earlier variable name, if you need access to it. Sometimes reusing the variable name can just be more convenient. I don't have any good example handy, but e.g. something like 'let tot1 = a + b in let tot2 = tot1 / 10 in let tot3 = tot2 + 8 in let tot4 = tot3 % 15 in tot4' would usually be nicer to read (and to modify!) if it was 'let tot = a + b in let tot = tot / 10 in let tot = tot + 8 in let tot = tot % 15 in tot'.
Sami
+5  A: 

All "member" functions are implicitly "rec" within the type they're defined in.

Brian
Thanks. It would be nice to have this documented somewhere on MSDN for the RTM!
Bruno Reis
Exactly what I was looking for
Mo Flanagan