Haskell插件在重新编译文件时会提供旧值

时间:2014-03-03 22:06:32

标签: haskell dynamic compilation ghc hotswap

我一直在使用Haskell插件包将字符串编译成函数/值,这些函数/值可以在运行时在Haskell中使用。但是,我遇到了一个问题:当我尝试使用相同的名称加载不同的值时,它只给出第一个值。

这是我的测试文件:

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

我的ghci跑了:

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

即使我给它一个不同的值,它也会保留旧值。我删除了我的临时目录和所有内容。

这是插件包的固有限制,还是我做错了什么?

1 个答案:

答案 0 :(得分:2)

您只需在第二次加载之前调用unloadAll或类似内容。