Search code examples
f#type-providersf#-data

F# Data Type Provider - Create with string variable


In F# I have the following bit of code

[<Literal>]
let fname=fstFile.FullName.ToString()
type genomeFile = CsvProvider<fname>

Where fstFile is an instance of the FileInfo class. However, this fails with:

This is not a valid constant expression or custom attribute value

And I can't seem to create the type without an actual hard coded string. Does anyone know how to fix this?


Solution

  • The parameter to the CSV type provider needs to be a constant, so that the types can be generated at compile-time (without actually evaluating the program. However, you can load a different file with the same schema at runtime.

    So, the best way to handle this is to copy one of your actual inputs to some well-known location (e.g. sample.csv in the same directory as your script) and then use the actual path at runtime:

    // Generate type based on a statically known sample file
    type GenomeFile = CsvProvider<"sample.csv">
    
    // Load the data from the actual input at runtime
    let actualData = GenomeFile.Load(fstFile.FullName.ToString())