我一直在使用 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
即使我给它一个不同的值,它也会保留旧的值。我删除了我的临时目录和所有内容。
这是插件包的固有限制,还是我做错了什么?