问题描述
我已经编写了这段代码,可以在VS.NET 2010中编译并完美运行
I have written this code which compiles and works perfectly in VS.NET 2010
module ConfigHandler
open System
open System.Xml
open System.Configuration
let GetConnectionString (key : string) =
ConfigurationManager.ConnectionStrings.Item(key).ConnectionString
但是,当我进行控制+ A和Alt + Enter并将其发送给FSI时,我会收到错误消息
however when I do a control + A and Alt + Enter to send this to FSI I get an error
ConfigHandler.fs(2,1):错误FS0010:定义中的结构化构造意外启动.预期为"="或其他令牌.
ConfigHandler.fs(2,1): error FS0010: Unexpected start of structured construct in definition. Expected '=' or other token.
好.
所以我将代码更改为
module ConfigHandler =
open System
open System.Xml
open System.Configuration
let GetConnectionString (key : string) =
ConfigurationManager.ConnectionStrings.Item(key).ConnectionString
现在Control + A,Alt + Enter成功,我的FSI很好地告诉了我
Now Control + A, Alt + Enter is successful and I FSI nicely tells me
模块ConfigHandler =开始 val GetConnectionString:字符串->字符串结束
module ConfigHandler = begin val GetConnectionString : string -> stringend
但是现在,如果我尝试在VS.NET 2010中编译代码,则会收到错误消息
However now If I try to compile my code in VS.NET 2010, I get an error message
库或多文件应用程序中的文件必须以名称空间或模块声明开头,例如'namespace SomeNamespace.SubNamespace'或'module SomeNamespace.SomeModule'
Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'
我怎么都可以?是否可以在VS.NET中进行编译以及是否可以将模块发送到FSI?
How can I have both? Ability to compile in VS.NET and the ability to send modules to FSI?
推荐答案
这两个代码段之间存在微小但很关键的区别,这应该归咎于这里.
There is a tiny -- but crucial -- difference between your two snippets of code which is to blame here.
F#有两种声明module
的方法.第一个是顶层模块",其声明如下:
F# has two ways to declare a module
. The first, a "top-level module", is declared like this:
module MyModule
// ... code goes here
声明模块的另一种方法是将其作为本地模块",例如:
The other way to declare a module is as a "local module", like so:
module MyModule =
// ... code goes here
顶级"和本地"声明之间的主要区别在于,本地声明后跟一个=
符号,并且必须缩进本地"模块中的代码必须
The main differences between the "top-level" and "local" declarations are that the local declaration is followed by an =
sign and the code within a "local" module must be indented.
第一个代码段收到ConfigHandler.fs(2,1): error FS0010: Unexpected start of structured construct in definition. Expected '=' or other token.
消息的原因是,您无法在fsi
中声明顶级模块.
The reason you get the ConfigHandler.fs(2,1): error FS0010: Unexpected start of structured construct in definition. Expected '=' or other token.
message for the first snippet is that you can't declare top-level modules in fsi
.
在模块定义中添加=
符号后,它从顶级模块更改为本地模块.从那里,出现错误Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'
,因为本地模块必须嵌套在顶级模块或名称空间中. fsi
不允许您定义名称空间(或顶级模块),因此,如果要将整个文件复制粘贴到fsi
中,唯一可行的方法是将编译指令用作@垫提到.否则,您可以简单地将本地模块定义(不包含名称空间)复制粘贴到fsi
中,它们将按预期工作.
When you added the =
sign to your module definition, it changed from a top-level module to a local module. From there, you got the error Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'
because local modules must be nested within a top-level module or a namespace. fsi
doesn't allow you to define namespaces (or top-level modules), so if you want to copy-paste the entire file into fsi
the only way it'll work is if you use the compilation directives as @pad mentioned. Otherwise, you can simply copy-paste the local module definitions (without the containing namespace) into fsi
and they should work as expected.
参考: MSDN上的模块(F#)
这篇关于定义模块VS.NET与F#Interactive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!