2016-01-24 68 views
1

我在现有模块上声明名称空间时收到错误消息。为什么在通过模块添加名称空间时收到错误?

错误:

Unexpected start of structured construct in definition. Expected '=' or other token.

注意,我只是将一个命名空间:

namespace ManageModules 

没有命名空间,代码编译。

代码:

namespace ManageModules 
module CreateModule.UILogic 

open System.Windows.Input // Error is referenced here... 
open UILogic.State 
open UILogic.Interaction 
open ManageModule.Entities 
open System.Collections.ObjectModel 

type CreationViewModel() = 
    inherit ViewModelBase() 

    let mutable (_modules:Module ObservableCollection) = ObservableCollection() 

    member this.Modules 
     with get()  = _modules 
     and set(value) = _modules <- value 

    member this.Add moduleItem = 
     _modules.Add(moduleItem) 
+0

很高兴你自己想通了。在你的回答中,你没有说出为什么'='起作用。有时甚至直到今天,命名空间/模块的东西仍然会抛出我。花几个小时来更好地理解原因,否则这个问题将在未来花费更多的时间,比现在几个小时更多地了解它更详细。换句话说,你现在可以付出一点时间,或者一次又一次地付出时间,而后者会花更多的时间。 –

+0

请参阅:[组织功能](http://fsharpforfunandprofit.com/posts/organizing-functions/)和[组织项目中的模块](http://fsharpforfunandprofit.com/posts/recipe-part3/) –

+0

是的。这就是我发现解决方案时所引用的内容。谢谢。 –

回答

4

有两种类型的模块声明:顶层模块声明和局部模块声明。

顶级模块声明声明的语法:

module [accessibility-modifier] [qualified-namespace.]module-name 

本地模块声明宣布与

module [accessibility-modifier] module-name = 

顶级模块声明必须显示为文件和模块中的第一个语句然后包含该文件中的所有内容。相比之下,您可以有多个本地模块声明。

注意到,虽然顶层声明也让你选择添加.提供合格的命名空间,你不能在本地模块声明虽然可以嵌套本地模块使用.秒。

在你的情况,因为你的模块声明不会在文件的开始出现,你必须写:

namespace ManageModules 
module CreateModule = 
    ... 

详情请参阅https://msdn.microsoft.com/en-us/library/dd233221.aspx

+0

是的......我只是想通了。尽管如此,我仍然会给你信任。谢谢! –

0

看来,当我在一个模块提供了一个命名空间一样,我需要一个平等的运营商追加到我的模块声明。

namespace ManageModules 
module CreateModule = 

代码:

namespace ManageModules 
module CreateModule = 

    open System.Windows.Input 
    open UILogic.State 
    open UILogic.Interaction 
    open ManageModule.Entities 
    open System.Collections.ObjectModel 

    type CreationViewModel() = 
     inherit ViewModelBase() 

     let mutable (_modules:Module ObservableCollection) = ObservableCollection() 

     member this.Modules 
      with get()  = _modules 
      and set(value) = _modules <- value 

     member this.Add moduleItem = 
      _modules.Add(moduleItem)