4

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.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
Jeff D
  • 2,164
  • 2
  • 24
  • 39

2 Answers2

5

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

Jeff D
  • 2,164
  • 2
  • 24
  • 39
  • Note that `new Nullable()` would also work. That is, if you add type parameters, you also need to explicitly use `new`. – kvb Feb 22 '13 at 21:45
  • @kvb: `Nullable()`—without `new`—works too (or even `Nullable<_>()`). – Daniel Feb 22 '13 at 21:58
  • @kvb: On further thought, I think you got it switched around: if you use `new` you must supply type args. – Daniel Feb 22 '13 at 22:00
  • 2
    @Daniel - oops, you're absolutely right. I was basing my comment off of the assumption that the code in the original question didn't work (which seems not to be the case). – kvb Feb 22 '13 at 22:06
5

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")
Community
  • 1
  • 1
Daniel
  • 47,404
  • 11
  • 101
  • 179