F# Mono编译器错误:库或多个文件应用程序中的文件必须以命名空间或模块声明开头
我试图在ubuntu上用mono编译 但是我得到了错误F# Mono编译器错误:库或多个文件应用程序中的文件必须以命名空间或模块声明开头,f#,mono,compiler-errors,F#,Mono,Compiler Errors,我试图在ubuntu上用mono编译 但是我得到了错误 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/wing
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$
我是F#的新手。有什么明显的遗漏吗?出现这个问题是因为这些示例文件是为一个没有该要求的F#版本编写的
正如错误消息所解释的,您所需要做的就是在每个文件的顶部添加
namespace SomeNamespace
,这样它就可以正常编译了。即使是ubuntu上的mono。您也可以通过添加
module theCurrentFileName
到每个.fs文件的顶部
事实上,这是一个较新的需求,而且示例很旧,需要更新。正如Brian和Scott指出的,您需要将文件封装在名称空间或模块声明中。如果文件中有顶级的
let
绑定(因为这些绑定必须在某些模块中),仅添加namespace SomeNamespace
可能不起作用。以下内容无效:
namespace SomeNamespace
let foo a b = a + b // Top-level functions not allowed in a namespace
也就是说,我更喜欢在顶层使用名称空间
而不是模块
,然后将所有函数显式地包装在模块
中(因为我认为这样可以使代码更具可读性):
但当然,您可以按照Brian的建议添加顶级模块(早期版本的F#自动使用PascalCase中的文件名作为文件中使用的顶级模块名):
这是一个老问题,但我是针对同一个问题提出的,我想为F#3中的模块添加提示 在多文件应用程序中,不需要使用名称空间,但应该使用模块 顶级模块声明,以这种方式声明(不带等号=) 同一文件中的本地模块,使用“=” 例如:
- 您不必在顶级模块中缩进声明李>
- 您必须缩进本地模块中的所有声明
namespace SomeNamespace
module FooFunctions =
let foo a b = a + b
// 'main.fs' would be compiled as:
module Main
let foo a b = a + b
module module_name
//Not
module module_name =
module toplevel // top level module without "="
module module1 = // local module with "="
let square x = x*x
module module2 =
let doubleit x = 2*x