Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use out attributes in an F# Interface?

Tags:

f#

I would like to define one of my parameters to be a C# out parameter in one of my interfaces. I realize that F# supports byref but how can I apply the System.Runtime.InteropServices.OutAttribute to one of my interface parameters?

C# Interface I am trying to replicate

public interface IStatisticalTests
{
    void JohansenWrapper(
        double[,] dat,
        double alpha,
        bool doAdfPreTests,
        out double cointStatus,
        out JohansenModelParameters[] johansenModelParameters);
}
like image 804
Dave Avatar asked Jul 12 '13 17:07

Dave


1 Answers

Here's an example:

open System
open System.Runtime.InteropServices

[<Interface>]
type IPrimitiveParser =
    //
    abstract TryParseInt32 : str:string * [<Out>] value:byref<int> -> bool

[<EntryPoint>]
let main argv =
    let parser =
        { new IPrimitiveParser with
            member __.TryParseInt32 (str, value) =
                let success, v = System.Int32.TryParse str
                if success then value <- v
                success
        }

    match parser.TryParseInt32 "123" with
    | true, value ->
        printfn "The parsed value is %i." value
    | false, _ ->
        printfn "The string could not be parsed."

    0   // Success

Here's your interface, translated:

[<Interface>]
type IStatisticalTests =
    //
    abstract JohansenWrapper :
        dat:float[,] *
        alpha:float *
        doAdfPreTests:bool *
        [<Out>] cointStatus:byref<float> *
        [<Out>] johansenModelParameters:byref<JohansenModelParameters[]>
            -> unit
like image 134
Jack P. Avatar answered Dec 15 '22 08:12

Jack P.