Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

renaming current module

Tags:

module

haskell

Is it possible to rename current module ? Let's say I want to introduce data constructors Left and Right, but these would clash with Prelude.Left and Right. I don't want to write everywhere MyModule.Left and MyModule.Right, I would prefer to rename it somehow to MyModule as M and write M.Left. Is it possible ? What are the alternatives ?

like image 276
hNewb Avatar asked Sep 24 '26 03:09

hNewb


2 Answers

Dario's answer gives the easiest way to do this, but since you ask for alternatives as well, you might want to look at the description of import declarations in the Haskell report, or at the "Loading modules" section of Learn You a Haskell.

One common alternative to local aliases (i.e. import qualified SomeModule as X) is just to hide the Prelude functions (or constructors) if you know you'll not need them:

import Prelude hiding (Left, Right)
import MyModule

You can also import different parts of a module in different ways: one common trick is to import a module's types directly but alias its functions:

import Data.Map (Map)
import qualified Data.Map as M

import Data.Set (Set)
import qualified Data.Set as S

simpleMap :: Map String Int
simpleMap = M.singleton "one" 1

simpleSet :: Set String
simpleSet = S.singleton "one"

This allows us to use both singleton functions, but we also don't have to type M.Map and S.Set in every type signature.

like image 169
Travis Brown Avatar answered Sep 27 '26 02:09

Travis Brown


import qualified YourModule as M
like image 42
Dario Avatar answered Sep 27 '26 00:09

Dario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!