3

I have an F# library where I defined a module. Inside the module there are 2 types defined. I'm accessing the library from C# project (currently it's only a test project though that probably doesn't matter).

I can access one of the types from C# but the other one is not available.

Below my F# code:

module Stock

open FSharp.Data
open InstrumentQueryBuilder

type Stock = CsvProvider<"..\prototype_historical_data.csv">

type StockData(symbol) = 
    member this.Symbol = symbol
    member private this.QueryBuilder = new InstrumentQueryBuilder(this.Symbol)
    member this.LoadData() =
        this.QueryBuilder.HistoricalData() |> Stock.Load
    // more member definitions here...

From C# I can see StockData but not Stock. Stock is CsvProvider from FSharp.Data. In my C# code I'm referencing both FSharp.Data and FSharp.Core.

Using StockData in C# is as simple as this:

var stockData = new Stock.StockData("SPY");

But if I do the same for Stock it shows a compile time error. Any suggestion how to access the Stock type or an explanation why that's not possible?

CoderDennis
  • 13,642
  • 9
  • 69
  • 105
PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
  • 1
    The `Stock` type is a provided type: it comes from an F# type provider. Type providers are run by the F# compiler, and construct a type and hand it back to the compiler. Therefore, if you aren't running the F# compiler (because you're doing C# work), it seems to me that the type provider wouldn't run so the `Stock` type wouldn't be available. OTOH, I would think the DLLs would contain the `Stock` type, so that explanation may be wrong. But my best guess is that the answer boils down to "type providers are special". – rmunn Oct 01 '16 at 03:36
  • 2
    Possible duplicate of [How do I create an F# Type Provider that can be used from C#?](http://stackoverflow.com/questions/12118866/how-do-i-create-an-f-type-provider-that-can-be-used-from-c) – Dax Fohl Oct 01 '16 at 03:50
  • @rmunn Your comment makes sense to me. I agree the problem must be with the type provider. If you make that an answer I'll gladly accept that. – PiotrWolkowski Oct 01 '16 at 04:28

1 Answers1

3

In F#, type has lot of meaning to it even though they all look very similar.

In you case,

Stock - Type abbreviation (alias). Not a first class object. Thats why it is not visible to c#.

StockData - It is class. Has function-style parameter list after name for use as constructor.

Cinchoo
  • 6,088
  • 2
  • 19
  • 34