不安全的IO或:Haskeline和目录(Unsafe IO Or: Haskeline and Directories)

编程入门 行业动态 更新时间:2024-10-27 18:25:48
不安全的IO或:Haskeline和目录(Unsafe IO Or: Haskeline and Directories)

免责声明:我对Haskell有点新鲜。

大家好,

我正在写一个翻译,或者在这种情况下,写一个REPL。 为此我使用haskeline,这对REPL很好。 它具有将命令行历史存储在文件中的能力,这也很好。

但是,我在使用它时遇到的一个问题是它似乎没有将“〜”扩展到主目录,这意味着我必须手动检索主目录。

我可以这样做(目前也这样做):

-- | returns a fresh settings variable addSettings :: Env -> Settings IO addSettings env = Settings { historyFile = Just getDir , complete = completeWord Nothing " \t" $ return . completionSearch env , autoAddHistory = True } where getDir :: FilePath getDir = unsafePerformIO getHomeDirectory ++ "/.zepto_history"

但是那使用了unsafePerformIO ,这让我感到畏缩。 您是否知道一个良好而干净的解决方法,不涉及重写整个功能? 这可能是我不知道的haskeline功能或者我没看到的东西。

告诉我,没有办法重写和重新思考一切都很好。

编辑:

我知道unsafePerformIO很糟糕,这就是为什么它让我感到畏缩。 如果你是Haskell的新手并且现在正在阅读这个问题:假装它不在那里。

DISCLAIMER: I am somewhat new to Haskell.

Hey guys,

I am writing an interpreter, or, in this context, a REPL. For that purpose I am using haskeline, which is nice for REPLs. It has the capability of storing the command line history within a file, which is also nice.

One problem I came across while working with it, though, is that it does not seem to expand "~" to the home directory, which means that I have to retrieve the home directory manually.

I could do it like this (and currently do):

-- | returns a fresh settings variable addSettings :: Env -> Settings IO addSettings env = Settings { historyFile = Just getDir , complete = completeWord Nothing " \t" $ return . completionSearch env , autoAddHistory = True } where getDir :: FilePath getDir = unsafePerformIO getHomeDirectory ++ "/.zepto_history"

But that uses unsafePerformIO, which makes me cringe. Do you know of a good and clean workaround that does not involve rewriting the whole function? This can be a haskeline feature I do not know of or something I just did not see.

Telling me there is no way around rewriting and rethinking it all is fine, too.

EDIT:

I know unsafePerformIO is bad, that's why it makes me cringe. If you are new to Haskell and reading this question right now: Just pretend it is not there.

最满意答案

更好的方法是在IO生成Settings对象,而不是相反,可以这么说:

addSettings :: Env -> IO (Settings IO) addSettings = do getDir <- fmap (++ "/.zepto_history") getHomeDirectory return $ Settings { historyFile = Just getDir , complete = completeWord Nothing " \t" $ return . completionSearch env , autoAddHistory = True }

毫无疑问,这需要对您当前的软件进行一些更改,但这将被视为解决此问题的“正确”方法。

A better approach would be to generate the Settings object inside IO, instead of the other way around, so to speak:

addSettings :: Env -> IO (Settings IO) addSettings = do getDir <- fmap (++ "/.zepto_history") getHomeDirectory return $ Settings { historyFile = Just getDir , complete = completeWord Nothing " \t" $ return . completionSearch env , autoAddHistory = True }

This will no doubt require some changes in your current software, but this would be considered the "right" way to go about this.

更多推荐

本文发布于:2023-07-23 11:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不安全   目录   IO   Haskeline   Unsafe

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!