16

Is there a way to have mutable function arguments in F#, that would allow something like

let mutable i = 9

let somefun n = n <- 12; ()

somefun i

(* *not* a real-world example *)

I do understand that this can be made to work by wrapping it into a record type

type SomeRec = { mutable i: int }

let ri = { i = 9 }

let someotherfun r = r.i <- 12; ()

and that this can be done in a similar fashion for class members. However, even after browsing through the whole F# Language Specification (yes, I did!), there seems to be no syntax to allow the first case, and the compiler appears to be quite unhappy about my trying this. I was hoping there would be some sort of type annotation, but mutable cannot be used in such.

I also know that I should not be doing this sort of thing in the first place, but the first case (int binding) and the second (record type) are semantically identical, and any such objection would hold for both cases equally.

So I think that I am missing something here.

1
  • 8
    If you need to mutate your params, you're still thinking imperatively. Can you describe exactly what you're trying to do, maybe someone can suggest a more idiomatic solution instead.
    – Juliet
    Sep 23, 2010 at 14:52

2 Answers 2

24

You can use ref as arguments

let v = ref 0
let mutate r = 
    r := 100
mutate v
printfn "%d" !v

Or byref keyword

let mutable v = 0
let mutate (r : byref<_>) = 
    r <- 100
mutate &v
printfn "%d" v
4
  • 5
    Note the byref is like C# ref. If you need C# out, then use byref but also add the [<System.Runtime.InteropServices.Out>] attribute to the parameter.
    – Brian
    Sep 23, 2010 at 14:46
  • 4
    This answer is technically correct, but I would cringe seeing it used in practice. OP should stick to F# idioms instead of trying to write C# in a little different syntax.
    – Juliet
    Sep 23, 2010 at 15:03
  • 7
    @Juliet This can be a lot faster than the functional equivalent in F#. I've used this in production code.
    – J D
    Jan 3, 2013 at 19:01
  • @JD @Julient - Seems that byref<..> is needed if you are going to have an [<Out>] mutable result type for calling from C# to F# as seen here: musingstudio.com/2017/02/17/… Please, it would be great to learn a more F#-ish way to call from C# v7.0.3 (.NET 2) to F#. Thanks.
    – rfreytag
    Mar 26, 2021 at 17:09
13

Use byref keyword which is equivalent to C# ref. See Passing by reference.

2
  • 1
    Oh dear, there are still quite basic bits I am missing. I guess it's back to reading the whole thing again. Sorry, I can only mark one 'accepted' answer, but will vote this one up. Sep 23, 2010 at 14:25
  • 2
    I just found Jon Skeet's article on parameters passing in another topic: yoda.arachsys.com/csharp/parameters.html It's on C#, but this is just what you need now. Sep 23, 2010 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.