Len*_*low 2 haskell scope module
我的问题出在最后一行:
module A where
data A = A { f :: Int }
defaultA = A { f = 0 }
Run Code Online (Sandbox Code Playgroud)
和
module B where
import A as A
data B = B { f :: Int }
bToA :: B -> A
bToA x = defaultA { A.f = f x }
Run Code Online (Sandbox Code Playgroud)
给
B.hs:8:26:
Ambiguous occurrence `f'
It could refer to either `B.f', defined at B.hs:5:13
or `A.f', imported from A at B.hs
Run Code Online (Sandbox Code Playgroud)
由于我不能在其自身中包含B限定,有什么替代方法可以解决命名空间冲突?我宁愿不重命名冲突函数.
编辑:更新了这些示例.
我这样做:
module B where
import A hiding (A(..))
import qualified A as A
bToA x = defaultA { A.f = f x }
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以从A中访问所有非冲突名称,而无需预先添加"A".所有冲突的名字都是以完全的资格进口的 - 如'A.something'.您保留简洁的代码并解决冲突.
当然,import qualified Some.Long.Name as S如果你不介意预先'S' ,那么简化也会有效.到处.