Question

I've been using the Haskell plugins package to compile strings into functions/values which can be used in Haskell at runtime. However, I've got a problem: when I try to load different values with the same name, it only gives the first value.

Here's my test file:

import System.Plugins
import System.IO.Temp
import System.Directory


deleteIfExists :: String -> IO ()
deleteIfExists filePath = do
  exists <- doesFileExist filePath
  if exists 
    then
      (removeFile filePath)
    else
      (return ())

compileInt :: String -> IO Int
compileInt codeMinusModule = do

  withTempDirectory "./.tmp" "__compileTemp" $ \path -> do
    let filePath = (path ++ "/GetInt.hs" )
    let code = "module GetInt where\n" ++ codeMinusModule

    deleteIfExists filePath
    writeFile filePath code

    status <- makeAll filePath []
    objectPath <- 
      case status of
        MakeSuccess _ opath -> return opath
        MakeFailure elist -> error $ "Couldn't make object:\n" ++ (concat elist)


    strat <- loadInt objectPath path

    deleteIfExists path

    return strat
    where 
      loadInt objectPath folderPath = do
        loadStatus <- load objectPath [folderPath] [] "myInt"
        case loadStatus of
            LoadFailure msg -> error $ "Failed to compile " ++ ( concat msg )
            LoadSuccess _ strat -> return strat

And my ghci run:

*Main> compileInt "myInt = 4"
Loading package array-0.4.0.1 ... linking ... done.
...
4
*Main> compileInt "myInt = 3"
4

Even when I give it a different value, it keeps the old one. I delete my temp directories and everything.

Is this an inherent limitation of the plugins package, or am I doing something wrong?

Was it helpful?

Solution

You just need to call unloadAll or similar before loading a second time.

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