This expression was expected to have type 'System.String' but here has type 'string' - f#

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.

Related

Plotly.NET configuration issue

Problem running Plotly.NET on F# Interactive. (VS2019, FSharp Core 7.0.0, Plotly.NET 3.0.1, TargetFramework: net472)
#r C:\....\.nuget\packages\plotly.net\3.0.1\lib\netstandard2.0\Plotly.NET.dll"
open Plotly.NET
let xData = [0. .. 10.]
let yData = [0. .. 10.]
let myFirstChart = Chart.Point(xData,yData)
Gives an error: " C:\...\AppData\Local\Temp\1\unknown(1,1): error FS3216: type 'Plotly.NET.GenericChart+GenericChart' not found in assembly 'Plotly.NET, Version=3.0.0.0, Culture=neutral, PublicKeyToken=.......'. A possible cause may be a version incompatibility. You may need to explicitly reference the correct version of this assembly to allow all referenced components to use the correct version."
What should be correct configuration for environment to get that running?
I'm not sure why you mention "TargetFramework: net472". Also I'm puzzled by your mention of "FSharp Core 7.0.0", since F# interactive is going to be tied to some specific version of F#, for VS 2019 that won't be 7.0.
It's better to use the new syntax for referencing a nuget package from a script:
#r "nuget: Plotly.NET"
I tried your code in VS 2019 (referencing the nuget package as above) and still got a weird error. I then went to Tools | Options | F# Tools | F# interactive and changed the option "Use .NET Core Scripting" from false to true. I then reset the F# interactive session to make the change take effect, and tried your code again, and it worked.
> let myFirstChart = Chart.Point(xData,yData);;
Binding session to 'C:/Users/jimfo/.nuget/packages/plotly.net/3.0.1/lib/netstandard2.0/Plotly.NET.dll'...
Binding session to 'C:/Users/jimfo/.nuget/packages/dynamicobj/2.0.0/lib/netstandard2.0/DynamicObj.dll'...
val myFirstChart: GenericChart.GenericChart =
Chart
(Plotly.NET.Trace2D, Plotly.NET.Layout, Plotly.NET.Config,
Plotly.NET.DisplayOptions)
I didn't bother testing 32 bit vs 64 bit or seeing what happens in VS 2022.
I don't know what the errors are about, I have seen some strange errors lately given the mix of F# compiler versions, FSharp.Core versions, VS versions (think of all the patched versions), and sometimes you have to just fool around a bit until you get the right combination that works.

Using and installing MathNet Package

I have installed the MathNet.Numerics package on Visual Studio 2017 using the Package Manager Console.
I have attempted to open a Source File on and execute an algorithm relating to the MersenneTwister type within the MathNet namespace.
However, when I try to generate numbers using this algorithm in F# Interactive, I am met with the error:
File1.fs(3,6): error FS0039: The namespace or module 'MathNet' is not defined. Maybe you want one of the following:
Math
Code is as per below:
module File1
open MathNet.Numerics.Random
let mersenneTwister = new MersenneTwister(42)
let a = mersenneTwister.NextDouble()
Apologies if this is unclear, I am relatively new to F# :)
Are you per change using the interactive window or running an fsx script? Cause these don't recognize your packages.
When reproducing your problem in a F# console app I got this output: Hello 0.374540.
I installed the MathNet.Numerics.fsharp nuget package (which also uses the package you mention) and used the following code in the Program.fs:
open MathNet.Numerics.Random
let hello () =
let mersenneTwister = new MersenneTwister(42)
let a = mersenneTwister.NextDouble()
printfn "Hello %f" a
[<EntryPoint>]
let main argv =
hello ()
0 // return an integer exit code
If you do want to use the nuget package from a script you can reference it in the top of your script like so (absolute or relative)
#r #"C:\Path\bin\Debug\netcoreapp3.0\MathNet.Numerics.dll"

error FS0039: The field, constructor or member 'X' is not defined

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

F# Interactive CsvProvider not defined

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.

FsLex changed with latest PowerPack?

I've been working on a compiler for a while but after changing to PowerPack 1.9.9.9 and the release version of VS2010 I'm no unable to compile the following line:
let lexbuf = Lexing.from_string text
I get the following two error:
"The value, constructor, namespace or type 'from_string' is not defined" pretty obviopus what it's trying to tell me but what's the resolution?
My quick guess is that this function has been renamed to fromString (because, in general, functions with underscores such as of_seq are now written in camelCase).
Lexing.LexBuffer<_>.FromString ?

Resources