首页 > 解决方案 > 解决 .net 命名空间冲突

问题描述

我正在使用 Giraffe 并尝试集成 Elmish.Bridge。我收到以下错误:

error FS0892: This declaration opens the module 'Elmish.Bridge.Giraffe', which is marked as 'RequireQualifiedAccess'. Adjust your code to use qualified references to the elements of the module instead, e.g. 'List.map' instead of 'map'. This change will ensure that your code is robust as new constructs are added to libraries.

如果我按以下顺序打开模块:

open Elmish.Bridge
open Giraffe

但是如果我交换订单,那么错误就会消失。

open Giraffe
open Elmish.Bridge

有人可以解释为什么会发生这种情况以及如何最好地解决它?

标签: .netf#elmish

解决方案


这是命名冲突。

当您进入时open Elmish.Bridge,这会将模块纳入范围Elmish.Bridge.Giraffe,并且可以通过名称对其进行寻址Giraffe。这正是您打开模块时应该发生的事情:它的所有内容都变为“范围内”。

问题是这与Giraffe来自不同库的另一个名为 的模块冲突。

当出现此类命名冲突时,F# 会优先使用最近打开的模块中的名称。因此,当您编写 时open Giraffe,编译器会将其视为open Elmish.Bridge.Giraffe。并且由于该模块需要合格的访问权限(即无法open编辑),编译器会发出相关错误。

当您放open Giraffe before open Elmish.Bridge时,编译器将其表示为 module Giraffe,而不是Elmish.Bridge.Giraffe,因为此时后者尚未在范围内。全局模块Giraffe可以打开,所以不会出错。

除了更改行的顺序外,您还可以通过使用 prefixopen明确指定您的意思是“全局”模块Giraffe而不是来解决冲突,如下所示:Elmish.Bridge.Giraffeglobal.

open Elmish.Bridge
open global.Giraffe

推荐阅读