Suppose I have
class Foo a where
(++=) :: a -> a -> a
cool :: (a -> b -> b) -> a -> b
and want to make
class Bar a where
(++=) :: (a -> b) -> a -> b
magic :: a -> b
which has an overlapping method name, (++=)
. Is there some way I can do this?
This question has a subtle "no but yes" kind of answer, which requires going into three concepts:
Point 1: every definition in Haskell has both a short, unqualified name like map
, and a long, qualified name like Data.List.map
.
Point 2: when you import a module into another, you can do either a qualified or an unqualified import. When you use unqualified import, the foreign module's names that you bring in will be aliased under their short names. When you do a qualified import, they will only be available under a modified name:
import qualified Data.Map as Map
Now in the module where this appeared, the Data.Map.map
function is visible under the alias Data.map
.
Third point: this means that every Haskell definition has a fully qualified name determined by its short name and the module where it's defined, but also unqualified or partially qualified aliases in each module where it is imported.
Now, your question has two answers:
Foo
and Bar
classes in the same module, that will fail.No, you cannot, at least within the same module. You can declare class Foo
and class Bar
in two different modules and import each of them into the same file, but you'll still have to qualify
at least one of those imports to avoid conflicts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With