I am trying to use the CsvProvider from FSharp.Data following the example here: http://fsharp.github.io/FSharp.Data/library/CsvProvider.html
In the example they define a type and then use the type but it looks like they are doing it in interactive mode. I am trying to do the same thing in my main but I don't understand why it doesn't work.
In the eample they do the following:
type Stocks = CsvProvider<"../data/MSFT.csv">
This is my attempt:
open System
open System.IO
open FSharp.Data
// Process execution params
// args(0) = nesting level
// args(1) = csv file name
[<EntryPoint>]
let main (args: string[]) =
printfn "Arguments passes in: %A" args
// F# searches the following directory at runtime : C:\Users\userName\source\repos\crawlerF\crawlerF\bin\Debug\netcoreapp2.1\test.csv'.
// It is therefore necessary to provide the top level of the repo for the file read by prepending ../../../../ to the file name
let filePath = "../../../../" + args.[1]
type urls = CsvProvider<filePath>
0
I get a few syntax errors when I try to define the urls variable. Why is this the case? I am new to F# so a more novice explanation would be appreciated.
Thanks.
There are a few things going on here:
You can't define types within a function, so you need to move the definition of
urls
out of themain
function and into a module or namespace.Type providers give you an easy way to generate types rather than defining them yourself manually, but these types still need to be generated at compile-time. This means that any parameter you pass to a type provider must be static, i.e. must have a value at compile-time.
In your case, the
filePath
of your sample CSV file is the only parameter you're passing to theCsvProvider
, but because this value is derived from a command line argument it is only known at runtime. To pass a static file name to theCsvProvider
you can use a literal string, e.g.This may just be a formatting error in your code sample, but in order to return the
0
from yourmain
function, it must be indented so that it's recognised as the last expression of that code block.Putting this all together gives you something like the following: