F# scripting, is this a supported use case?

Hi,

I am very new to the .net world, as a development platform
But I used Perl5 and CPAN in the past, is this is what I am comparing to

At a high level (and honestly I only understand it at a high level)

For Perl, you can have multiple Perl interpreters installed on your computer
but usually only one active Perl interpreter, or you can usually execute just one depending on how you configured your Path

When you use CPAN, it installs a Package for that active Perl interpreter,
if you are using a brew like environment and want to use a package, you usually need to install it again for that interpreter

And if you distribute a script, usually you upload it to CPAN, and configure your script in package, and if anyone want to use your script, he can download it from CPAN, which will in turn make sure that all the dependencies are installed

If uploading to CPAN is not an option, you can create a simple custom script, that run some CPAN command that will install the dependencies and ask your script user to make sure to install all the dependencies before running your script

Also if your script user doesnt have Perl at all installed, you can use tools like Par Packer, to package a Perl interpreter and all the dependencies and and your script all in one executable

Sorry for the long Perl intro, but, again, this is what I am comparing to

Now, coming to .Net, and I am focused on .Net core, since its portable, and seems to be recommended even on windows for new stuff

Using the dotnet command it seems
I can install packages, but it seems that at this requires to be done within the context of a project
I am not really sure where the packages are installed, or if they are shared, and I am not even sure how can this work if you have multiple dotnet version on your computer

But, it doesnt seem that you just install a package to be used by scripts that are not in a project
It doesnt seem that scripting, is supported as a first class use case by the dotnet core framework

How can I install a package for usage within a script?
Where is the package installed?
How can reference it? i know about the #I and #r directives, but I think I need to know where a package is installed to use them (you dont have to know where exactly CPAN installed a Perl package to use it, you just need to make sure Perl have the packages path configured properly)
And how can I shared my script? How should my use install the dependencies

I can of course just create a project
And I can just switch to PowerShell for scripting
But I want to know how it can be done in F#

You can use fake plus VSCode (install ionide-fsharp) to make everything simpler for scripting. The official demo is for create a script for building staff but you can use it to do anything just add other nuget packages. Pls check below link:
https://fake.build/fake-gettingstarted.html

2 Likes

F# scripting is not supported with only .NET Core at this time. We’re working on bringing that up.

That said, you can still script against the .NET Framework or Mono. Packages are installed into a .nuget folder on a machine, but the location of that folder may differ depending on the OS. You currently reference the .dll directly with #r. In the future, you’ll be able to #r "package.name" and no longer need to use#r` to point to a location on disk.

3 Likes

This is kind of a follow up question …

As I understand, any .Net program is execute in the .Net runtime
Now we have multiple options for the runtime
.Net framework 4.7
.Net core
mono

when I execute an fsharp script, using fsi on windows or fsharpi on linux
in which runtime is it executed?

as per @cartermp reply, scripting is not supported for .Net Core

So does this mean, that fsi on windows use the .Net Framework and fsharpi on linux uses mono?

This is correct, yes. Once FSI works on .NET Core, then it will execute against .NET Core when you use the FSI that is bundled with .NET Core.

1 Like

Will the framework be a command line paramater passed to fsi or fsharpi or will we have multiple executables or will it be a compiler directive ?

fsi --target:net47
fsi --target:mono
fsi --target:core2.0.1

fsi
fsi-core
fsi-mono

#target “mono”

Just want to add that FAKE5 mentioned by @albertwoo is running on .NET Core and can be installed as dotnet global tool. So, only thing that is needed to be installed on target machine is dotnet CLI.