2

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

artella
  • 5,068
  • 4
  • 27
  • 35
  • 8
    You're not doing something wrong. If you load the file test2.hs in the interpreter, you are in fact able to access the internal functions of the contained module. This is actually quite nice, as it gives you a means to do white-box testing. If you, however, **import** the module Test2 from another module, you will not be able to access anything but `addNumber1` from there. – Stefan Holdermans Apr 24 '12 at 11:33

1 Answers1

10

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.

David Miani
  • 14,518
  • 2
  • 47
  • 66
  • dblhelix's comment is spot on - it won't have any effect if you load that module in ghci. However if you use that module with any other module, `addNumber2` wont be available. I'm guessing that is what you want to improve encapsulation. I can't think of any reason why you would want to hide `addNumber2` from ghci when loading the module explicitly, since it is really useful to have all module functions available for testing. – David Miani Apr 24 '12 at 13:34