surfmuggle
surfmuggle

Reputation: 5954

Import Module fails - module is not defined and value or constructor not defined

I want to use functions from a module inside the file BasicFunctions.fs. After completing f# hello world tutorial one next step is to work through F# Tour.

The question what is the equivalent of import in F# and printfn an integer helped a bit. The question value or constructor not defined seem to be not relevant for my case.

Attempt 1: change config to include second file

If i change my project configuration myFsharpApp.fsproj to this

  <ItemGroup>
    <Compile Include="BasicFunctions.fs" />
    <Compile Include="Program.fs" />
  </ItemGroup>

I get the error: FSC : error FS0225: Source file 'C:\dev\fsharp\myFSharpApp\BasicFunctions.fs' could not be found [C:\dev\fsharp\myFSharpApp\myFSharpApp.fsproj]

But the file BasicFunctions.fs can be found under the path 'C:\dev\fsharp\myFSharpApp\BasicFunctions.fs'. So i do not understand why it is not found.

Attempt 2: change the config to this:

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>

Using dotnet build then returns

  1. error FS0039: The namespace or module 'BasicFunctions' is not defined
  2. error FS0001: Type mismatch. Expecting a ''a -> 'b -> 'c' but given a ''a -> unit ' The type ''a -> 'b' does not match the type 'unit'
  3. error FS0039: The value or constructor 'sampleFunction1' is not defined.[C:\dev\fsharp\myFSharpApp\myFSharpApp.fsproj]

Most of the code is copied from the F# Tour. The difference is that i want to import functions from the file BasicFunctions.fs and want to print the result of sampleFunction1 x = x*x + 3

The code structure is the following

// BasicFunctions.fs
module BasicFunctions = 
    let sampleFunction1 x = x*x + 3

// Program.fs
module main =
    open BasicFunctions

// Define a new function to print a name.
let printGreeting name =
    printfn "Hello %s from F#!" name

[<EntryPoint>]
let main argv =
    printfn "%d" BasicFunctions.sampleFunction1 3 // 12 expected
    0 // return an integer exit code

Questions

Screen of current code state

screen of current code

Content of fsproj

This is the content of myFSharpApp.fsproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>

</Project>

Upvotes: 1

Views: 912

Answers (1)

user1981
user1981

Reputation: 586

  • A top level module is defined without = and indentation
  • Nothing
  • Space is function application so in order to call like the with the right precedence you need paranthesis or pipe

So it becomes like below.

// BasicFunctions.fs
module BasicFunctions

let sampleFunction1 x = x * x + 3

// Program.fs
open BasicFunctions

[<EntryPoint>]
let main argv =
    sampleFunction1 3 
    |> printfn "%d" 
    0

Note that you can skip the open and reference the module directly. Both files needs to be included, in order.

  <ItemGroup>
    <Compile Include="BasicFunctions.fs" />
    <Compile Include="Program.fs" />
  </ItemGroup>

Upvotes: 2

Related Questions