Add .net-Core F# File to F# project in vscode

841 Views Asked by At

I am currently trying to add an F# file to an F# project in VSCode using .NET core. I have ionide installed and tried using the "Add Current File to Project" command, after creating a new file with the .fs extension, but this did not work.

Is there some setup for Ionide that I am missing? Or is there another tool I should be using?

Thanks!

2

There are 2 best solutions below

0
On

To add the file to the project by hand you can:

1- create you file, myfile.fs

2- add the file to the myproject.fsproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <RootNamespace>client_api</RootNamespace>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Complie Include="myfile.fs"/> 
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>

Open the file namespace on the main project and run the project.

0
On

Suppose we have the following directory tree:

.
├── MyFSharpApp.fsproj
└── Program.fs

Add we want to add the file DB.fs to get the following tree:

.
├── DB.fs
├── MyFSharpApp.fsproj
└── Program.fs

To do that we need to perform the following steps:

  1. Create the file DB.fs
module DB

type Repository(databaseUrl: string) =
    let databaseUrl = databaseUrl

    member this.xxx() = 3
  1. Reference DB.fs in MyFSharpApp.fsproj. We need to reference DB.fs before Program.fs
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="DB.fs" />
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>
  1. Use our new module in Program.fs
open System

[<EntryPoint>]
let main argv =
    let repository = DB.Repository("someUrl")
    printfn "A string: %i" (repository.xxx())
    0 // return an integer exit code