how to get f# scripts to call module func? - f#

Just trying to structure an F# library. So far it "appears" that:
I can only reference the C# library from a .fs source file,
and .fs source files are not available to .fsx script files.
Are any of these statements are correct? The examples I read say just use an open, but that only works from a .fs file.
Is there a version of
#r "System.Drawing.dll"
for script files used via f# interactive (fsi) in the same project?
Also, I find the syntax difference between top level modules and lower modules a little strange. Top me it's weird that the top level doesn't have an equals.
module Utilities
module et =
Why not be consistent and have "module moduleName = " for everything?

It is important to note that "References" in Visual Studio in F# only apply to the current project. If you're working in a script file, it does not see the libraries referenced by the project.
To reference a library in a script file (e.g. Script.fsx), you can write (you can use both absolute paths and relative paths):
#r #"C:\<path_to_library>\library.dll"
If you want to load a file that is not a script (e.g. Library.fs) from a script file you can use:
#load "Library.fs"
The fs file itself cannot contain directives like #r, so if the file is using some library that needs to be referenced, you need to have an #r directive in the main script file (to reference the library before loading the source file).
As for the module inconsistency, the main reason is that when you use module Foo or namespace Foo at the top-level, you do not need to indent the rest of the file:
module Foo
let bar = 10
module Sub =
let bar = 12

Related

F#: Why does script file not recognize module?

I create a new F# console application project in Visual Studio 2015
I create a simple new module file called Calc.fs like so:
module Calc
let add a b = a + b
I add an F# script file to the project like so:
open Calc
let c = add 1 2
I get the following compiler errors in the script file:
The namespace or module 'Calc' is not defined
and
The value or constructor 'add' is not defined
Why is my module not recognized by my script file? How can I fix this?
Please note that my script file appears after the module in the order of files in the project:
.fsx files are not compiled together with the rest of the project; they don't "know" about any other code unless you explicitly make it known to them. This is true for any external DLLs (and in fact even many in the .NET Framework) as well as other F# code.
You need to add
#load "Calc.fs"
at the top of the script file to tell the compiler to load that code before evaluating the rest of the script.

Use environment variables in F# directives

In FSI, is it possible to run something like
#I #"%APPDATA%/npm/node_modules/blabla/bin/"
instead of
#I #"C:/Users/username/AppData/Roaming/npm/node_modules/blabla/bin/"
Any other options to make it not sensitive to username?
Not likely you can use environment variables in #I FSI directives.
However you may make your package installation agnostic of user name by using the trick with __SOURCE_DIRECTORY__ built-in identifier similar to one I've described in this SO answer:
create a file anchorfsi.fsx with the single line of code
#I __SOURCE_DIRECTORY__
and put it into the directory %APPDATA% points to
add to the command line starting your FSI the term
--load:%APPDATA%\anchorfsi.fsx
Now you can use relative paths in your #r directives.
Just for illustration I put into directory associated with my user profile a folder testlib containing FSharp.Data.dll. The snip below shows how it gets referenced from FSI using the outlined above technique:

(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
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'
The namespace or module 'MathNet' is not defined.
Unexpected identifier in implementation file
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.

Cannot open namespace in Fsharp script file

When in a separate Fsharp project in a SomeLib.fs file the following code is compiled:
namespace SomeNameSpace
type SomeType =
member this.SomeMember = "Some member"
and you want to reference and use this type in a script file like:
#I #"c:/pathToDll/"
#r "SomeLib.dll"
This is not possible, although the path to the dll is correct and I checked everything. Also when the SomeLib.fs file is in the same project and referenced by #load, you still cannot open the namespace.
I know you can put the type in a module, but I cannot do this as the type has te be used as a Wcf service type.
After a lot of experimental work and surprisingly little info on the internet or in F# books I found out the following:
// Cannot use a relative path
//#I #"bin\Debug"
// Have to use a absolute path
#I #"C:\Development\FSharpNameSpaceTest\SomeCSharpLib\bin\Debug"
// But I can reference a Csharp dll lib
#r "SomeCSharpLib.dll"
// I cannot add a reference to an external F# library dll
// #I #"C:\Development\FSharpNameSpaceTest\NameSpace\bin\Debug"
// #r "NameSpace.dll"
// If I directly load the external fs file, it works"
#load #"C:\Development\FSharpNameSpaceTest\NameSpace\SomeNameSpace.fs"
#load "Library1.fs"
// Namespaces in both the local and the external fs files can only be openend if every single file is loaded instead of referencing the dll.
// Referencing a C# dll is no problem
open FSharpNameSpaceTest
open SomeCSharpLib
open NameSpace
I do not know if this is the most optimal approach but it works. What I will do is, I will create a fsx file for every project that loads the individual fs files in that project and then I will load that fsx file in the fsx file that references the project.
I still find this all very confusing and counterintuitive. But that might be my limited knowledge of the innerworks of F#
Edit: And the right and complete answer is, I didn't implement a default constructor. Still, if you do not wan't to do this, the above approach is an alternative. Thanks to Marc Sigrit.
If on Windows, the slashes in the import directive should be replaced by backslashes.

conditional include in FSharp

I would like to convert a bunch of fs files to fsx files.
Each of those fs file reference class defined in, say, base.fs
So instead of being compiled in the project and relying on the compiler resolution, all would be file based.
That means if I have all those file to include base.fsx, and that one file references another, base.fsx would be included twice.
Does anyone know how to make a conditional include with fsx files ?
The preprocessor documentation states
There is no #define preprocessor directive in F#. You must use the
compiler option or project settings to define the symbols used by the
#if directive.
If you're loading all the files from a single fsx script, then you can load the individual files from the project in the right order and the individual library files do not need to load base.fs directly - the code will be defined, because it has been loaded before.
For example, if you have base.fs:
module Base
let test() = 10
and you have more.fs which does not load base.fs but uses the functions defined there:
module More
let more () =
Base.test() + 1
then you can load all files in F# interactive (in, say, script.fsx) and it will work fine:
#load "base.fs"
#load "more.fs"
More.more()
The only disadvantage is that you won't get IntelliSense when editting more.fs (because the editor does not know about base.fs). To workaround that, it is probably a good idea to keep the files in a project in Visual Studio. But you can still load them in F# interactive for experimentation & testing.

Resources