Search code examples
f#type-providers

How can I use a command line argument as the argument for a type provider?


What's the proper way to use a dynamic value as the argument for a type provider like CsvProvider? I'd expect this to work:

open System.IO
open FSharp.Data

[<EntryPoint>]
let main argv =
    type Stock = CsvProvider<argv.[0]>
    let stockData = Stock.Load(argv.[0])

    for row in stockData.Rows do
        printfn "(%A, %A, %A, %A)" row.High row.Low row.Open row.Close

    0 //Exit

What am I doing wrong?


Solution

  • You can't use a command-line argument as the static argument for the type provider. The line type Stock = CsvProvider<argv.[0]> requires the parameter to CsvProvider to be a compile-time constant, because the types generated by the type provider are created a compile-time, not at run-time.

    You can supply a different value to the Load function, and this can be a run-time value, as in your line Stock.Load(argv.[0]), but you will need to use a compile-time constant file name or sample data that matches the expected layout of the file being passed as a command-line argument, so that the types generated at compile-time will match the structure of the file being passed in at run-time (even though the data may be different).