English 中文(简体)
现有模块是否有名称空间?
原标题:Is there a namespace for the current module?

我的问题是最后一点:

module A where

data A = A { f :: Int }

defaultA = A { f = 0 }

以及

module B where

import A as A

data B = B { f :: Int }

bToA :: B -> A
bToA x = defaultA { A.f = f x }

gives

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

因为不能包括 B自称是,解决“空间冲突”的替代办法是什么? 我不会改称冲突的职能。

Edit:增订了这方面的实例。

最佳回答
import qualified A as A
问题回答

我这样说:

module B where

import A hiding (A(..))
import qualified A as A

bToA x = defaultA { A.f = f x }

这样,你就可以在不预先排出A的情况下从A中获取所有非冲突名字,所有相互冲突的名字都是以完全资格进口的,如A.something。 你保留了法典的精华,并围绕冲突开展工作。

当然,Simplier import有某些资格。 姓名:S 如果你不把注意力排在各地,也将发挥作用。

正义 B.f

你们不需要

import A as A

正义

import A




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签