In one file I need to use the regular prelude (++)
operator and I also wish to implement my own behaviour for (++)
. I have used import Prelude hiding (++)
at the top of my file, defined my own (++)
operator and now further below I wish to refer to the regular Prelude's (++)
. How do I achieve this?
Haskell Prelude with hiding, how to undo?
Write
import qualified Prelude
in addition to
import Prelude hiding ((++))
at the beginning of the code, and write Prelude.++
where you need ++
in Prelude.
How do you import the whole Prelude as qualified please? –
Selfpossessed
@Selfpossessed
import qualified Prelude
imports everything as qualified, you just have to write Prelude.
with the type you want. –
Coeducation As Tsuyoshi Ito explained, you can qualify the operator by its module name. However, since by defining your own version of (++)
you most likely want to increase the readabilty of your program, qualifying an operator with its module name later on seems to be a weird measure.
Just look at this: "abc" Prelude.++ "def"
Now that's ugly.
Why not simply create a new operator, like <++>
or an infix function like `append`?
Yeah, actually my first reaction to the question was “Do not name your function
(++)
,” although I did not post it. But if the asker is writing a library with its own ++
which is meant to replace the ++
in Prelude, then the user of the library will ideally never have to use Prelude.++
. In such a (rare) case, it makes sense to define a function with the same name. Otherwise, it makes little sense to cause a name clash with something so prevalent. –
Stores It is beginning to irritate me when people read past the question and answer with "don't do that"s, especially when the question does not give enough information about its motivation. Let the explorers explore, let them attempt what they think is right, let them see firsthand how ugly it comes out when they do. Soon they will be in a position for me to write this comment on their answers. :-) –
Surfboat
@Surfboat Lack of information in question is not a good reason to bash the answers. This answer clearly states an alternative. –
Schlueter
© 2022 - 2024 — McMap. All rights reserved.