Jeff D
Jeff D

Reputation: 2175

F# - Calling a c# method with overloads and params

No idea how the 30 other articles have managed to not help me here, but I'm working with a c# dll with these overloads:

function TqlForBidAskTrade(string, int?, params string[])
function TqlForBidAskTrade(string[], int?, params string[])

I can call this method with the params I want in c# like this:

TqlForBidAskTrade("string", null)

What is the equivalent in F#? I can't seem to get anything to compile at all. I've tried:

TqlForBidAskTrade("string", null)
TqlForBidAskTrade("string", Nullable<int>())
TqlForBidAskTrade("string", Nullable<int>(), null)
TqlForBidAskTrade("string", Nullable<int>(), [])
TqlForBidAskTrade("string", Nullable<int>(), ["doodah"])
TqlForBidAskTrade("string", 4, ["doodah"])

It sure seems like w/ all of the similar requests I should have stumbled across this, but I've been looking for an hour.

Upvotes: 3

Views: 252

Answers (2)

Daniel
Daniel

Reputation: 47914

You found the solution, but to add some explanation:

The C# compiler treats Nullable<T> specially. One example is null can be substituted for new Nullable<T>(). Here is another example.

In F#, Nullable<'T> is just another type: no sugar, no magic. option<'T> is the closest thing to a Nullable counterpart, and is used for optional parameters. Your function could look like this, if defined in F#:

type T =
  static member TqlForBidAskTrade(s:string, ?n:int, ?args:string[]) = ()

T.TqlForBidAskTrade("foo")

Upvotes: 5

Jeff D
Jeff D

Reputation: 2175

and... face-palm.
TqlForBidAskTrade("string", Nullable()) did work, In my random code tinkering, I'd messed up the syntax.

Upvotes: 5

Related Questions