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

前端 未结 4 1018
小蘑菇
小蘑菇 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: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
    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
    let mutable status = Unchecked.defaultof
    let mutable trackError = Unchecked.defaultof
    
    CvInvoke.CalcOpticalFlowPyrLK(
          previousFrame, 
          nextFrame, 
          previousPoints, 
          Size(15,15), 
          2, 
          MCvTermCriteria(10, 0.03), 
          //Out params 
          &currFeatures, 
          &status, 
          &trackError,
          //---------
          LKFlowFlag.UserInitialFlow)
    

提交回复
热议问题