I've defined a lot of functions (say, 100+), each of which do a specific work but with the same signature. That is something like:
module R001 (run) where run = <do-...>
module R002 (run) where run = <do-...>
What I wanna do is to provide the actual 'run' as user input, such that:
main = do
runWith $ read $ getLine
where
runWith :: Int -> IO ()
runWith n = R<n-padded-with-0>.run
Currently, I import all modules qualified, and put all the run
's into a list of [Maybe (IO())]
, so this works:
runWith n = case Rs !! (read $ getLine) of
Just run -> run
Nothing -> undefined
But as the n
grows, I have to continously maintain a big list.
Is there any way I can define the big list using TemplateHaskell, or just load the corresponding module as needed at runtime without having to seperate each module into different shared libraries.
Based on epsilonhalbe's answer, I did some research:
import R1 (run1)
import R2 (run2)
test = $(functionExtractor "^run")
main :: IO ()
main = do
putStrLn $ show $ length $ test
run1 -- remove on second attempt
run2 -- remove on second attempt
This block of code prints 2 following the results of run1
and run2
. If I remove the last two lines, it just prints 0. It seems that functions imported but not referenced won't be extracted ...