2014-03-03 59 views
3

我一直在使用Haskell插件包將字符串編譯爲可在運行時在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 

即使當我給它一個不同的值,它使舊的。我刪除了我的臨時目錄和所有內容。

這是插件包的固有限制,還是我做錯了什麼?

回答

2

您只需要在第二次加載之前調用unloadAll或類似軟件。

相關問題