Avoid using “mutable” variable to call a method with a byref (out in C#) parameter?
-
27-06-2021 - |
Вопрос
So I have this class in C#:
public class Foo : IFoo {
public bool TryConnect(out Status status) {
...
}
}
And I want to consume it with F#.
Unfortunately I had to use some hackery such as:
type FooService (foo : IFoo) =
member x.Execute() =
let mutable status = Unchecked.defaultof< ^Status>
foo.TryConnect(&status) |> ignore
(...do something with status...)
Is there a way to not use "mutable" here? In the end, I'm not actually using the first initialization value at all.
(I'm using F# 2.0, but if you have hints that only apply to F# 3.0, I'll be happy to hear them.)
Thanks
Решение
out
params are automatically tuplized. So, you can do:
let res, status = foo.TryConnect()
For more info, see Passing by Reference on MSDN.
Не связан с StackOverflow