[英]How to handle exceptions in Frege?
Trying to handle a exception I found a related question talking about this: 试图处理异常我发现了一个相关的问题:
what is the Frege equivalent to Haskell's "interact" function? 什么是弗雷格相当于Haskell的“交互”功能?
But it wasn't clear to me how to use the try/catch/finally
expressions. 但是我不清楚如何使用
try/catch/finally
表达式。
The problem : 问题 :
I wanted to read a file and return all its lines. 我想读取一个文件并返回其所有行。 In case it didn't exist I may wanted to return an empty list.
如果它不存在,我可能想要返回一个空列表。 Something like:
就像是:
getContent :: String -> IO [String]
getContent filePath = openReader filePath >>= \reader -> reader.getLines
`catch` (\(e::FileNotFoundException) -> return [])
`finally` (println "something went wrong")
The previous code compiles but when executed it only shows the following: 之前的代码编译但在执行时它只显示以下内容:
frege> getContent "asdf"
java.io.FileNotFoundException: asdf (No such file or directory)
Questions: 问题:
docs/wiki/frege goodness
would help a lot. docs/wiki/frege goodness
中的更多例子会有很多帮助。 Thanks 谢谢
The code looks good so far, but there is a problem with the lambda. 到目前为止,代码看起来很好,但是lambda存在问题。 Like in Haskell, a lambda extends as far right as syntactically possible.
就像在Haskell中一样,lambda在语法上尽可能地扩展。 Hence, despite
catch
having lower precedence as >>=
it still belongs to the lambda. 因此,尽管
catch
优先级较低,因为>>=
它仍然属于lambda。
By the way, there is a short-hand form for such lambda expressions: 顺便说一下,这种lambda表达式有一个简写形式:
_.foo
is a term that desugars to 是一个荒谬的术语
\it -> it.foo
and extra arguments can also be applied: 还可以应用额外的参数:
_.foo bar baz
gets desugared to 得到了贬低
\it -> it.foo bar baz
This is exactly made for situations like above. 这完全适用于上述情况。
In the REPL you can get documentation on catch, finally and >>= with the :help command. 在REPL中,您可以获得有关catch的文档,最后使用:help命令获取>> =。
You are right that this would be a nice issue for Frege Goodness. 你是对的,这对弗雷格善良来说是一个很好的问题。 However, there are also working examples in the github repo.
但是,github repo中也有一些工作示例。 For this case, look at examples/SimpleIO.fr
对于这种情况,请查看examples / SimpleIO.fr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.