Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a member that has an out parameter in F#

Tags:

I know that in f# I can treat out parameters as members of a result tuple when I'm using them from F#, e.g.

(success, i) = System.Int32.TryParse(myStr)

What I'd like to know is how I define a member to have signature that appears to C# as having an out parameter.

Is it possible to do this? And can I just return a tuple and have the opposite process occur when I call the method from C#, e.g.

type Example() =
  member x.TryParse(s: string, success: bool byref)
    = (false, Unchecked.defaultof<Example>)
like image 426
Khanzor Avatar asked Nov 25 '12 00:11

Khanzor


People also ask

How do you use out parameters in a function?

The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition​ as well as in the calling method.

How do you call a function with out parameters in C#?

Calling a method with an out argument TryParse method, which attempts to convert a string to a number. Starting with C# 7.0, you can declare the out variable in the argument list of the method call, rather than in a separate variable declaration.

Do you need to declare an out variable before use it?

Whenever we call a method with an parameter, we must have to declare that variable first, before we use it.

Can out parameter be optional C#?

No. To make it "optional", in the sense that you don't need to assign a value in the method, you can use ref . A ref parameter is a very different use case.


2 Answers

No, you can't return the result as a tuple -- you need to assign the value to the byref value before returning the result from the function. Also note the [<Out>] attribute -- if you leave that out, the parameter acts like a C# ref parameter.

open System.Runtime.InteropServices

type Foo () =
    static member TryParse (str : string, [<Out>] success : byref<bool>) : Foo =
        // Manually assign the 'success' value before returning
        success <- false

        // Return some result value
        // TODO
        raise <| System.NotImplementedException "Foo.TryParse"

If you want your method to have a canonical C# Try signature (e.g., Int32.TryParse), you should return a bool from your method and pass the possibly-parsed Foo back through the byref<'T>, like so:

open System.Runtime.InteropServices

type Foo () =
    static member TryParse (str : string, [<Out>] result : byref<Foo>) : bool =
        // Try to parse the Foo from the string
        // If successful, assign the parsed Foo to 'result'
        // TODO

        // Return a bool indicating whether parsing was successful.
        // TODO
        raise <| System.NotImplementedException "Foo.TryParse"
like image 129
Jack P. Avatar answered Sep 17 '22 22:09

Jack P.


open System.Runtime.InteropServices

type Test() = 
    member this.TryParse(text : string, [<Out>] success : byref<bool>) : bool = 
       success <- false
       false
let ok, res = Test().TryParse("123")
like image 38
desco Avatar answered Sep 18 '22 22:09

desco