Haskell : hide function in module (i.e. make function usable only by other functions in same module)

StackOverflow https://stackoverflow.com/questions/10296185

  •  03-06-2021
  •  | 
  •  

Question

suppose I have a file NecessaryModule.hs, which has the following internals :

module NecessaryModule where

addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b

addNumber2 :: Int -> Int -> Int
addNumber2 a b = a + b

When I do :

:load NecessaryModule

both addNumber1 and addNumber2 are available in the current scope. Is there a way to hide the function addNumber2 so that it is available to other functions in the same module but does not load up when I load the module in the manner above? Thanks

----------------------------------------------------------------------------------------

[Response to nanothief]

I tried your suggestion in the following way but it did not work for me. I had a file called test2.hs as follows :

--test2.hs
module Test2 (addNumber1) where

addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b

addNumber2 :: Int -> Int -> Int
addNumber2 a b = a + b

But then when I do

:load test2 

then I am able to invoke both addNumber1 and addNumber2. Have I done something wrong? Thanks

Was it helpful?

Solution

You just specify the methods you do want to export on the module line:

module NecessaryModule (addNumber1) where
....

If you don't specify that line, it includes everything by default.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top