codesnippets:avoidconflictprelude

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
codesnippets:avoidconflictprelude [2021/03/17 19:30] f2b216codesnippets:avoidconflictprelude [2025/10/08 00:48] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Avoiding name conflict with prelude ======+====== Avoiding name conflict with Prelude ======
  
 +The avoidance of prelude may make sense for the following reasons:
 +  * reprogramming prelude functions to understand what it does
 +  * reprogramming prelude functions to create improved versions
 +  * avoiding conflicts with same name
 +
 +Methods:
 +  * Hide functions from prelude.
   * The directive ''{-# LANGUAGE NoImplicitPrelude #-}'' can be used to avoid implicit import of module ''Prelude''   * The directive ''{-# LANGUAGE NoImplicitPrelude #-}'' can be used to avoid implicit import of module ''Prelude''
   * If you import ''Prelude'' by ''import qualified Prelude as <Prefix>'' then the directive above is not neccessary, because Prelude will only be imported qualified. However, the directive above will not harm.   * If you import ''Prelude'' by ''import qualified Prelude as <Prefix>'' then the directive above is not neccessary, because Prelude will only be imported qualified. However, the directive above will not harm.
-  * Alternatively you can import the funtions that you need from module Prelude if they do not conflict with other functions.+  * Alternativelyyou can import the funtions that you need from module Prelude if they do not conflict with other functions.
  
 NOTE: Examples work with GHC 8.10.4. Has not been tested with other versions. NOTE: Examples work with GHC 8.10.4. Has not been tested with other versions.
  
 ===== Example #1 ===== ===== Example #1 =====
 +
 +Hide functions from prelude.
 +
 +<code Haskell>
 +module Main where
 +
 +import Prelude hiding( reverse )
 +
 +main :: IO ()
 +main = do
 +    putStrLn $ reverse "1234abcd"
 +
 +
 +reverse :: [a] -> [a]
 +reverse l = rev l []
 +    where
 +        rev [] acc = acc
 +        rev (e:r) acc = rev r (e : acc)
 +</code>
 +
 +
 +===== Example #2 =====
  
 Qualified import of preload overrides implicit import. Qualified import of preload overrides implicit import.
Line 34: Line 63:
 </code> </code>
  
-===== Example #=====+===== Example #=====
  
 The same with directive ''{-# LANGUAGE NoImplicitPrelude #-}''. The same with directive ''{-# LANGUAGE NoImplicitPrelude #-}''.
Line 62: Line 91:
 </code> </code>
  
-===== Example #=====+===== Example #=====
  
 With import of all functions that are needed only. With import of all functions that are needed only.
Line 88: Line 117:
 </code> </code>
  
-===== Example #=====+===== Example #=====
  
 Shows what happens without measures, like the above. Shows what happens without measures, like the above.
Line 120: Line 149:
 </code> </code>
  
-===== Example #=====+===== Example #=====
  
 Shows what happens with directive ''{-# LANGUAGE NoImplicitPrelude #-}'' only. Shows what happens with directive ''{-# LANGUAGE NoImplicitPrelude #-}'' only.
Line 151: Line 180:
  
  
 +===== ✎ =====
 +~~DISCUSSION~~
codesnippets/avoidconflictprelude.1616005841.txt.gz · Last modified: (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki