Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# to F# Interop: How to specify a C# method's optional parameter in F# function type signature?

Tags:

c#

interop

f#

I have the following method in some C# code:

Qux MyFunc(Foo foo, Bar bar, int baz = 1);

I want to add it as a field on my new F# Record type. I will assign the C# function later, to my record field, in F# code.

If I ignore the default nature of the final parameter baz, then my type would look like this:

type MyType = {
    MyFunc: Foo * Bar * int -> Qux
}

However, because we have the optional C# parameter, I wish to reflect that. I have tried using the Optional Attribute mentioned here, but it doesn't work:

type MyType = {
    MyFunc: Foo * Bar * [<Optional;DefaultParameterValue(null)>] int -> Qux
}

I have also tried other combinations such as the question mark prefix e.g. ?baz.

What is the correct way to do this (if it is at all possible)?

Update & summary

Whilst it is possible to do this on a type member, it is not possible to do this on a value (Record). I'm using F# 3.1, so this might be possible in future.

To be fair in rewarding points, I will upvote @Daniel-Fabian who confirmed this is the case, and mark @Mark-Seemann's workaround as the answer, because although it wasn't quite the answer that I was looking for, it solved my problem.

like image 866
Scott Avatar asked Feb 13 '23 12:02

Scott


1 Answers

You write your signature with a ? for optional parameters.

member this.MyFunc (foo, bar, ?baz) = Qux

or for just the signature

abstract member MyFunc : Foo * Bar * [<OptionalArgument>] baz : int option -> Qux

as is also written in http://msdn.microsoft.com/en-us/library/dd233213.aspx. However, it won't work in your case, because optional parameters are only allowed to type members and not values.

like image 194
Daniel Fabian Avatar answered Feb 16 '23 03:02

Daniel Fabian