Search code examples
f#visual-studio-codeionide

(F#, mono for OS X) Errors is happened in visual studio code but send file to fsi successfully (fsi:send file)


I have got errors with the following codes in visual studio code. However, I am able to send the file with command (fsi:send file) and it is executed successfully. It seems I didn't setup the VSC with Ionide properly. Please feel free to comment.

#load "packages/MathNet.Numerics.FSharp.3.14.0-beta01/MathNet.Numerics.fsx"  

open MathNet.Numerics

SpecialFunctions.Gamma(0.5) // Unexpected identifier in implementation file

open MathNet.Numerics.LinearAlgebra
let m : Matrix<float> = DenseMatrix.randomStandard 50 50
(m * m.Transpose()).Determinant()

Syntax check for mistakes and errors in vscode, but the code can be executed in fsi

  1. This directive may only be used in F# script files (extensions .fsx or .fsscript). Either remove the directive, move this code to a script file or delimit the directive with '#if INTERACTIVE'/'#endif'
  2. The namespace or module 'MathNet' is not defined.
  3. Unexpected identifier in implementation file

Solution

  • The first error, "This directive may only be used in F# script files (extensions .fsx or .fsscript)", is telling you how to solve it. You haven't told us the filename of your F# file that you're getting errors in, but I bet it ends with .fs, right? The .fs extension is intended for files that are part of a larger project. A good rule of thumb is that if you have any .fs files, you need a project file (currently that's going to be in .fsproj format, which is an ugly XML file, but VS Code can help you create it).

    If you want to use the #load directive, it must be in an F# script file, which means a file with an .fsx extension. (The .fsscript extension is also allowed, but I never see anyone using it in practice. The .fsx extension is the de facto standard).

    Simply rename your .fs file to .fsx and that should solve error #1. Then errors #2 and #3 should go away on their own -- they are happening because the F# compiler isn't loading the MathNet namespace, which is because it's ignoring the #load directive in a .fs file. Once the #load directive is processed, then the MathNet.Numerics.fsx file should be loaded, and that file in turn loads all the namespaces needed.

    So this was simply because you saved the file as a .fs file when you needed .fsx.