简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM