0
votes

I'm not able to find how to declare a function in an interface so that I have a Tuple or byref result.
I want to simulate this syntax:

let executed, value = Int32.TryParse("123") 

Let's say that I have a function with 2 input parameters and I want to know if the result is successful and in case having the result (a Record type in this case). Kind of:

type Result = {reference:string; value:decimal}

type IExecutor =
    abstract member DoStuff: (aa:string) * (bb:string) * (result:byref<Result>) -> bool
    abstract member DoStuff: (aa:string, bb:string, result:byref<Result>) -> bool

type Executor () =
    member this.DoStuff (aa:string, bb:string, result:byref<Result>):bool =
        result <- {reference="ref"; value=0m}
        false


let executed, result = executor.DoStuff "aa" "bb"
or 
let executed, result = executor.DoStuff("aa", "bb")

I'm not able to declare DoStuff in the interface.
The second try is a copy of the Int32.TryParse signature I see from intellisense, why doesn't work?
What is the correct syntax for having DoStuff called as I want ?

1
I think you'll need to declare an outref parameter, see docs.microsoft.com/en-us/dotnet/fsharp/language-reference/… - Abel

1 Answers

1
votes

First of all to declare an interface you use abstract members and you only apply the signature

type IExecutor =

    abstract member DoStuff: string*string*outref<int>->bool 

If you want to implement the interface it goes as follows

type Executor() =

   interface IExecutor with

     member this.DoStuff  (a:string,b:string,result:outref<int>) : bool =
       result <- 3
       true

You can call it like this

let s = new Executor() :> IExecutor

let a,b = s.DoStuff ( "lorem" "ipsum" )

Having said that, if you're consuming this only from F# avoid outrefs use tuples:

type IExecutor =

    abstract member DoStuff: string->string->bool*int

type Executor() =

   interface IExecutor with

     member this.DoStuff  (a:string) (b:string)  : bool*int =
       true,3

let s = new Executor() :> IExecutor

let a,b = s.DoStuff "lorem" "ipsum"