问题
Basically when I :load name.hs
the variables and bindings are gone.
Is there some option to tell ghci keep it all?
回答1:
To load a new module, you can use
Prelude> :m + Mymodule
But reloading and keeping interactive bindings is not generally possible. Reloading is essentially forgetting all modules and loading them again. The bindings could depend on already loaded modules. The dependency logic dictates that when GHCI forgets a module, it also needs to forget everything that depends on it, including interactive bindings.
GHCI could e.g. store the text of commands that where used to create the bindings, and try to re-run those commands on reload, knowing that some of them might fail. But this can get hairy very quickly, so it's not being done.
回答2:
I am adding yet another answer as this question was first google hit for me, whereas the real solution was further away (and I just hadn't checked rest of the hits, and only because of helpful people on IRC I have found it):
http://chrisdone.com/posts/ghci-reload
Basically, it's library that allows you to keep your data under a stable pointer with some C code underneath that survives GHCi reloads.
The example given on that blogpost is fairly complex, for someone that just wants to have some data ready in GHCi session the README from the library itself is better:
https://github.com/chrisdone/foreign-store/blob/master/README.md
It saved me a lots of time but my scenario was trivial: I wanted to have some assets loaded and ready while I manipulated the rest of the code. The types and structure for those assets is not changing at all.
回答3:
I don't think this can be done with GHCi.
Googling, I found a readme which states that :reload maintained variables, but keeping bindings doesn't work for me:
*Main> let x = 1
*Main> :show bindings
x :: forall t. (Num t) => t = _
*Main> x
1
*Main> :reload
Ok, modules loaded: Main.
*Main> :show bindings
*Main> x
<interactive>:1:0: Not in scope: `x'
来源:https://stackoverflow.com/questions/7428153/how-can-i-persist-the-environment-between-ghci-reloads