Mono 编译器中的错误:库或多文件应用程序中的文件必须以命名空间或模块声明开头

leo*_*on 3 mono f# compiler-errors

我正在尝试在 ubuntu 上以单声道编译此示例

但是我得到了错误

wingsit@wingsit-laptop:~/MyFS/kitty$ fsc.exe -o kitty.exe  kittyAst.fs kittyParser.fs kittyLexer.fs main.fs 
Microsoft (R) F# 2.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

/home/wingsit/MyFS/kitty/kittyAst.fs(1,1): error FS0222: Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'

/home/wingsit/MyFS/kitty/kittyParser.fs(2,1): error FS0222: Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'

/home/wingsit/MyFS/kitty/kittyLexer.fsl(2,1): error FS0222: Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'
wingsit@wingsit-laptop:~/MyFS/kitty$ 
Run Code Online (Sandbox Code Playgroud)

我是 F# 的新手。有什么明显的我想念吗?

Tom*_*cek 5

正如 Brian 和 Scott 指出的,您需要将文件包含在命名空间或模块声明中。namespace SomeNamespace如果let文件中有顶级绑定(因为这些必须在某个模块中),那么仅添加可能不起作用。以下内容无效:

namespace SomeNamespace
let foo a b = a + b   // Top-level functions not allowed in a namespace
Run Code Online (Sandbox Code Playgroud)

也就是说,我更喜欢使用namespace而不是module在顶层,然后module显式地将所有函数包装起来(因为我相信这会使代码更具可读性):

namespace SomeNamespace
module FooFunctions =     
  let foo a b = a + b
Run Code Online (Sandbox Code Playgroud)

但是当然,您可以按照 Brian 的建议添加顶级模块(早期版本的 F# 自动使用PascalCase中的文件名作为文件中使用的顶级模块的名称):

// 'main.fs' would be compiled as:
module Main
let foo a b = a + b
Run Code Online (Sandbox Code Playgroud)