How is it possible that the F# compiler rejects the type string when it's expecting a System.String?
#I #"..\..\packages"
#r #"FSharp.Data\lib\net40\FSharp.Data.dll"
open FSharp.Data
let [<Literal>] csvFile = #"..\..\data\FootballResults.csv"
type Football = CsvProvider< csvFile >
let data = Football.GetSample().Rows |> Seq.toArray
After running this simple code, I get the error message:
Script.fsx(5,30): error FS0001: This expression was expected to have type 'System.String' but here has type 'string'
I thought string was simply an alias for System.String?
Edit
The csv file is quite big, just the first rows are important:
Date,Home Team,Away Team,Full Time Home Goals,Full Time Away Goals,Full Time Result,Half Time Home Goals,Half Time Away Goals,Half Time Result,Home Shots,Away Shots,Home Shots on Target,Away Shots on Target,Home Fouls,Away Fouls,Home Cards,Away Cards,Home Yellow Cards,Away Yellow Cards,Home Red Cards,Away Red Cards
08/18/2012,Arsenal,Sunderland,0,0,D,0,0,D,14,3,4,2,12,8,7,0,0,0,0,0
08/18/2012,Fulham,Norwich,5,0,H,2,0,H,11,4,9,2,12,11,6,3,0,0,0,0
08/18/2012,Newcastle,Tottenham,2,1,H,0,0,D,6,12,4,6,12,8,3,5,2,2,0,0
The full file can be downloaded here under the folder data
FSharp.Data version
FSharp.Data 2.3.2
I found the problem, it was in the paket script provided in the book Get Programming with F#.
The script was pointing to an old version of FSharp.Data (I thought paket was this amazing package manager that always get you the best version for your project while in fact, it does not, you always fight and struggle with wrong dependencies).
At the end I managed to fix the problem this way:
Solution 1
Simply start a new project using the nuget command Install-Package FSharp.Data -Version 4.2.7 and then the reference to the package #r "nuget: FSharp.Data"
Solution 2
Update the paket.lock provided by the book with the correct version FSharp.Data (4.2.7). The code posted in my question run after that.
Advice for F# learner
Whoever is trying to learn F# with this book, it's a good book but be very careful with their package reference. They seem outdated and can get you into unpleasant situation.
I'm using the Microsoft.Office.Interop.Excel library (version 14.0) from an F# application and I can't seem to be able to reference some of the properties defined in Interop's interfaces/classes.
For example, if I have a Worksheet object I can't do the following:
let sht = // Get the Worksheet
sht.PageSetup.CenterHeader <- // Set the header
It can't find CenterHeader as a property of the PageSetup interface, even though it's there if I view the Interop dll in the object browser.
Just for reference, the Interop dll that I'm using is from the VS directory: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll
Update:
I actually spoke too soon. Unfortunately the suggested solution with the cast didn't work either. VS thinks it's OK but it fails at runtime with the following error:
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.IPageSetup'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208B4-0001-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
As I said in the comments, F# doesn't support type equivalence for embedded interop types, so if a C# project has Embed Interop Types enabled then the F# compiler may be unable to determine which version of the interop type to use. Since the type embedded in the C# output assembly has been stripped down to only the members used, this can make it so that the F# compiler is unable to see members that are present in the version of the type from the primary interop assembly.
The workaround is to turn off Embed Interop Types on the C# project.
This works for me:
#if INTERACTIVE
#r "office"
#r "Microsoft.Office.Interop.Excel"
#endif
open Microsoft.Office.Interop.Excel
let setCenterHeader fileName worksheet header =
let file = System.IO.FileInfo(fileName)
let excel = ApplicationClass()
try
// Make sure to use full path since this will
// be interpreted as relative to Excel's process,
// not currently executing one
let workbook = excel.Workbooks.Open(file.FullName)
let sheet = workbook.Worksheets.[worksheet] :?> Worksheet
sheet.PageSetup.CenterHeader <- header
workbook.Save()
finally
excel.Application.Quit()
setCenterHeader "TestWorksheet.xlsx" "Sheet1" "My header 1"
setCenterHeader "TestWorksheet.xlsx" "Sheet2" "My header 2"
setCenterHeader "TestWorksheet.xlsx" "Sheet3" "My header 3"
You might want to make sure you have matching versions of PIA and Office installed/used.
I am trying to run my code interactively in an fsx file. I have loaded all the dlls required, I then try to load the required files with #load but when I load the "Utlities.fs" file which depends on a function in the top file "HttpGetExchangeRate.fs" i get the error "Utilities.fs(88,42): error FS0039: The field, constructor or member 'getExchangeRates' is not defined"
Dose the 'getExchangeRates' not get defined when i load "HttpGetExchangeRate.fs"as in the image below or an I missing something?
#load "HttpGetExchangeRate.fs"
#load "Utilities.fs"
open System
open FsCheck
open NUnit.Framework
open HttpClient
InvoiceApp.Http.getExchangeRates "EUR" "USD"
InvoiceApp.Math.convertInvoicingCurrencyToEuro 200.00M "EUR"
Here is an image of the error message
If I understand your scenario correctly, this is due to a bug in how namespaces are handled in FSI. The workaround is to open the namespace you need before #loading the second file
#load "HttpGetExchangeRate.fs"
open InvoiceApp
#load "Utilities.fs"
That should get you unblocked for now, the bug has since been fixed (F# 4.0/VS 2015 will have the fix).
It sounds like you are running into the issue described in this question with implicit modules in fsi.
How to load external F# code and use it in fsi
I'm loading FSharp.Data in the interactive console. The library is loaded without any problem:
> #r "FSharp.Data.dll";;
--> Referenced 'C:\Users\pw\AppData\Local\Temp\FSharp.Data.dll' (file may be locked by F# Interactive process)
> open FSharp.Data;;
However, when I'm trying to initialize CsvProvider (defined in FSharp.Data) I get the error message saying the type is not defined:
> type Stocks = CsvProvider<"C:\Users\pw\Downloads\msft.csv">;;
type Stocks = CsvProvider<"C:\Users\pw\Downloads\msft.csv">;;
--------------^^^^^^^^^^^
stdin(62,15): error FS0039: The type 'CsvProvider' is not defined
I thought the problem may be with file and assemblies paths but now I'm using absolute paths and the error remains. On the other hand, I am able to use the CsvProvider when I'm creating a standard, not interactive, project. Any help to make it work in interactive session highly appreciated.
The warning about file being locked looks worrisome. Can you copy FSharp.Data somewhere and reference it using absolute path:
\#r #"C:\Poligon\packages\FSharp.Data.2.1.0\lib\net40\FSharp.Data.dll";;
Downgrade your FSharp.Core to 4.7 and FSharp.Data to 3.3.3. It should work after that.
We're using Fake and I'd like to run DotCover after our Build target. It's alway telling me:
C:\Users\xxxxx\Dev>FAKE\tools\Fake build.fsx
F# Interactive for F# 3.1 (private)
Freely distributed under the Apache 2.0 Open Source License
For help type #help;;
> [Loading C:\Users\xxxxx\Dev\build.fsx]
build.fsx(8,1): error FS0039: The value or constructor 'DotCoverNUnit' is not defined
My short simple "test" script
#r #"FAKE/tools/FakeLib.dll"
open Fake
DotCoverNUnit dotCoverOptions nUnitOptions
What went wrong?
You need to open namespace containing DotCoverNUnit class:
open Fake.DotCover