My problem is in the last line:
module A where
data A = A { f :: Int }
defaultA = A { f = 0 }
and
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', impor开发者_运维百科ted from A at B.hs
Since i can not include B qualified within itself, what alternative is there to resolve the namespace clash? I would rather not rename the clashing function.
Edit: updated that examples.
import qualified A as A
I'd do it this way:
module B where
import A hiding (A(..))
import qualified A as A
bToA x = defaultA { A.f = f x }
This way you could access all non-clashing names from A without prepending 'A.' and all clashing names are imported with full qualification - as 'A.something'. You retain brevity of code and work around the conflicts.
Of course, simplier import qualified Some.Long.Name as S
would also work if you don't mind prepending 'S.' everywhere.
Just B.f
And you don't need
import A as A
Just
import A
精彩评论