tags:

views:

116

answers:

3

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', imported 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.

+5  A: 
import qualified A as A
sclv
+2  A: 

Just B.f

And you don't need

import A as A

Just

import A
Yuras
How to refer to `defaultA` if the module doesn't `import A`?
KennyTM
Hm, that works, thanks. In my real-life example the module names are quiet long that's why was looking for a "import Foo as F" style solution.
Lenny222
2KennyTM: fixed, thanks
Yuras
+4  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.

ADEpt