F#: let mutable vs. ref

I can only support what gradbot said – when I need mutation, I prefer let mutable. Regarding the implementation and differences between the two – ref cells are essentially implemented by a very simple record that contains a mutable record field. You could write them easily yourself: type ref<‘T> = // ‘ { mutable value … Read more

Global operator overloading in F#

Here is the (non idiomatic) solution: type Mult = Mult with static member inline ($) (Mult, v1: ‘a list) = fun (v2: ‘b list) -> v1 |> List.collect (fun x -> v2 |> List.map (fun y -> (x, y))) : list<‘a * ‘b> static member inline ($) (Mult, v1:’a ) = fun (v2:’a) -> v1 … Read more

Using keywords as identifiers in F#

Given section 3.4 of the F# 2.0 spec: Identifiers follow the specification below. Any sequence of characters that is enclosed in double-backtick marks (“ “), excluding newlines, tabs, and double-backtick pairs themselves, is treated as an identifier. I suspect you can put it in backticks: “private“ I haven’t tried it though.

Why isn’t there a protected access modifier in F#?

The protected modifier can be quite problematic in F#, because you often need to call members from a lambda expression. However, when you do that, you no longer access the method from within the class. This also causes confusion when using protected members declared in C# (see for example this SO question). If you could … Read more

How can i convert between F# List and F# Tuple?

Besides listToTuple then pblasucci has the right answer. But you wont be happy with the result unless you know something about type types involved, or if you wan’t to do a lot of boxing and unboxing. let tupleToList t = if Microsoft.FSharp.Reflection.FSharpType.IsTuple(t.GetType()) then Some (Microsoft.FSharp.Reflection.FSharpValue.GetTupleFields t |> Array.toList) else None let listToTuple l = let … Read more

Use of `inline` in F#

The inline keyword indicates that a function definition should be inserted inline into any code which uses it. Most of the time, this will not have any effect on the type of the function. However, in rare cases, it can lead to a function which has a more general type, since there are constraints which … Read more

Shadowing and Nested function

Your code is equivalent to the following, where I’ve simply numbered instances of your names to help you visualize how shadowing is occurring. let func y0 = let dup0 y1 = y1 + y1 let z0 = dup0 y0 let dup1 y2 = let dup2 z1 = let y3 = y2 * z1 y3 let … Read more