【问题标题】:Why do I receive an error when adding a namespace over a module?为什么在模块上添加命名空间时会收到错误消息?
【发布时间】:2016-04-30 17:51:33
【问题描述】:

当我在现有模块上声明命名空间时收到错误消息。

错误:

定义中结构化构造的意外开始。预期'=' 或其他令牌。

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

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)

【问题讨论】:

  • 很高兴您自己解决了这个问题。在您的回答中,您没有说明为什么 = 有效。直到今天,命名空间/模块的事情有时仍然让我感到震惊。花几个小时来更好地理解原因,否则这个问题在未来会比现在更详细地了解它的几个小时消耗更多的时间。换句话说,您可以现在支付一点时间,也可以一次又一次地继续支付时间,这将是更多的时间。
  • 是的。这就是我在发现解决方案时引用的内容。谢谢。
  • 我只是添加更多链接供我使用。当您提出问题时,他们会更加切题,并且通常会提供所有需要的信息,这使得它们非常适合作为参考问题。由于我计划回馈您的questions,因此我还将面包屑留给其他可能通过链接返回的人的其他详细信息。你做得很好!。

标签: types module f# namespaces


【解决方案1】:

有两种类型的模块声明:顶级模块声明和本地模块声明。

顶级模块声明使用以下语法声明:

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

本地模块声明用

声明
module [accessibility-modifier] module-name =

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

请注意,虽然顶级声明还允许您选择添加 . 以提供合格的命名空间,但您不能在本地模块声明中使用 .s,尽管您可以嵌套本地模块。

在你的情况下,由于你的模块声明没有出现在文件的开头,你必须写:

namespace ManageModules
module CreateModule =
    ...

更多详情请见https://msdn.microsoft.com/en-us/library/dd233221.aspx

【讨论】:

  • 是的...我刚刚想通了。不过,我仍然会给予你信任。谢谢!
【解决方案2】:

看起来当我在模块上提供命名空间时,我需要在我的模块声明中附加一个相等运算符。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-20
    • 2017-01-25
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2017-11-15
    相关资源
    最近更新 更多