The F# equivalent of C#'s 'out'

前端 未结 4 1009
小蘑菇
小蘑菇 2020-12-09 18:30

I am rewriting a C# library to F# and I need to translate the following code

bool success;
instance.GetValue(0x10, out success);

what is th

相关标签:
4条回答
  • 2020-12-09 18:51

    Neither wasatz's answer nor Max Malook's is complete. There are three ways of calling methods with out parameters. The second and third ways also work with ref parameters.

    For the examples, assume the following type:

    open System.Runtime.InteropServices //for OutAttribute
    type SomeType() =
        member this.GetValue (key, [<Out>] success : bool byref) =
            if key = 10 then
                success <- true
                "Ten"
            else
                success <- false
                null
    

    Assume also that we have an instance of that type:

    let o = SomeType()
    

    Option 1

    You can let the F# compiler handle the out parameter by tupling it with the return value:

    let result1, success1 = o.GetValue 10
    let result2, success2 = o.GetValue 11
    

    Running the above lines in F# interactive yields

    val success1 : bool = true
    val result1 : string = "Ten"
    val success2 : bool = false
    val result2 : string = null
    

    Option 2

    You can use a mutable value, passing its address with the & operator:

    let mutable success3 = false
    let result3 = o.GetValue (10, &success3)
    let mutable success4 = false
    let result4 = o.GetValue (11, &success4)
    

    In F# interactive, the result is

    val mutable success3 : bool = true
    val result3 : string = "Ten"
    val mutable success4 : bool = false
    val result4 : string = null
    

    This option is best when you are delegating to another method, since you can pass the calling method's out parameter directly to the called method. For example, if you are implementing a wrapper around IDictionary<_,_>, you can code the TryGetValue method as

    //...
    interface IDictionary<'TKey, 'TValue> with
        member this.TryGetValue (key, value) = inner.TryGetValue (key, &value)
        //...
    

    Option 3

    You can use a reference cell:

    let success5 = ref false
    let result5 = o.GetValue (10, success5)
    let success6 = ref false
    let result6 = o.GetValue (11, success6)
    

    The output:

    val success5 : bool ref = {contents = true;}
    val result5 : string = "Ten"
    val success6 : bool ref = {contents = false;}
    val result6 : string = null
    

    Warning!

    Be careful not to use the ref keyword as you would in C# for an in/out parameter. For example, the following does not yield the desired result:

    let success7 = false
    let result7 = o.GetValue (10, ref success7)
    

    The output:

    val success7 : bool = false
    val result7 : string = "Ten"
    

    Why does success7 hold the value false? Because success7 is an immutable variable.

    In C#, ref calls attention to the fact that you are passing a reference to a variable as the argument for a ref parameter. It simply serves as insurance that the programmer of the caller is aware that the variable may be modified by the called method. In F# however, ref creates a new reference cell holding a copy of the value of the following expression.

    In this case, we are making a reference cell that holds the value copied from the success7 variable, but not assigning that new reference cell to any variable. We then pass that reference cell to the GetValue method, which modifies the content of the reference cell. Because the calling method has no variable pointing to the modified cell, it has no way of reading the new value of the reference cell.

    0 讨论(0)
  • 2020-12-09 18:55

    I think it's also worth mentioning here that the value of the out parameter doesn't have to be initialized.

    It is possible to do the following:

    let mutable success3 = Unchecked.defaultof<bool>
    let result3 = o.GetValue (10, &success3)
    

    This might be usefull in scenarios where you are calling a .NET library function with arrays as output parameters, i.e:

    let mutable currFeatures = Unchecked.defaultof<PointF[]>
    let mutable status = Unchecked.defaultof<byte[]>
    let mutable trackError = Unchecked.defaultof<float32[]>
    
    CvInvoke.CalcOpticalFlowPyrLK(
          previousFrame, 
          nextFrame, 
          previousPoints, 
          Size(15,15), 
          2, 
          MCvTermCriteria(10, 0.03), 
          //Out params 
          &currFeatures, 
          &status, 
          &trackError,
          //---------
          LKFlowFlag.UserInitialFlow)
    
    0 讨论(0)
  • 2020-12-09 19:00

    You have to use a reference cell.

    let success = ref false
    instance.GetValue(0x10, success)
    
    // access the value
    !success
    
    0 讨论(0)
  • 2020-12-09 19:13

    You should probably return an option or a tuple instead. Because F# has pattern matching you really don't need out parameters since there are better ways to return more than one value from a function.

    So, something like this would be more idiomatic

    let (value, success) = instance.GetValue(0x10)
    

    where instance.GetValue is a

    unit -> ('a, bool) 
    

    Or you could return an option and do something like

    match instance.GetValue(0x10) with
    | Some value -> doStuff value
    | None -> failwith "Oops!"
    
    0 讨论(0)
提交回复
热议问题