在异常处理中使用 GADT
这是我的问题
,我使用 Control.Exception.catch 进行异常处理,其类型如下: (从 Hoogle 中挑选)
catchSource
:: Exception e
=> IO a
The computation to run
-> (e -> IO a)
Handler to invoke if an exception is raised
-> IO a
这是我将传递给我的处理程序函数的构造函数
> data JobException = PreProcessFail
> | JobFail
> | ChartFail
> deriving (Show, Typeable)
> instance Control.Exception JobException
这是现在的处理程序函数:
> exceptionHandler :: JobException -> IO ()
> exceptionHandler exception = do
> writeFile "testException.txt" ("caught exception " ++ show exception )
我将使用它来执行一些日志记录。我需要记录的信息将在记录中 因此
> type JobState = MVar ProcessConfig
> data ProcessConfig = PConfig { model :: ServerModel
> , ipAddress :: String
> , cookie :: Cookie
> } deriving Show
,由于我需要一个强制具有上面提到的类型的处理程序,并且我需要一个 JobState,所以我认为答案是重写 JobException 以在其中隐藏 JobState。这似乎是 GADT 的工作!我不确定,这是新领域。
我说得对吗?我可以用 GADT 解决这个问题吗? 有人可以提供如何开始构建一个的线索吗?我一直在阅读的教程假设您正在尝试解决比我所遇到的更复杂的问题。
如果我错了,有人可以指出我正确的方向吗?
更新:我从 1 中了解了动态类型并发现此后不久,Data.Dynamic。 变暖了吗?
Here's my problem
I'm using Control.Exception.catch for my exception handling, which has the following type:
(culled from Hoogle)
catchSource
:: Exception e
=> IO a
The computation to run
-> (e -> IO a)
Handler to invoke if an exception is raised
-> IO a
Here is the constructor I will be passing to my handler function
> data JobException = PreProcessFail
> | JobFail
> | ChartFail
> deriving (Show, Typeable)
> instance Control.Exception JobException
Here is the Handler function as it is right now :
> exceptionHandler :: JobException -> IO ()
> exceptionHandler exception = do
> writeFile "testException.txt" ("caught exception " ++ show exception )
I'm going to use this to do some logging. The information I need to log will be in a record
of type JobState
> type JobState = MVar ProcessConfig
> data ProcessConfig = PConfig { model :: ServerModel
> , ipAddress :: String
> , cookie :: Cookie
> } deriving Show
So since I need a handler that is forced to have the type I mentioned above, and I need a JobState, I thought the answer would be to rewrite JobException to hide a JobState inside it. That seems to be a job for GADT! I'm not sure, this is new territory.
Am I right? Can I solve this with a GADT?
Could someone provide a clue in how to begin to construct one? The tutorials I have been reading assume you're trying to solve a more complicated problem than what I have got.
If I am wrong, could someone point me in the right direction?
update: I learned about dynamic types from 1 and found shortly thereafter Data.Dynamic.
Getting warmer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Show 异常实例主要在您自己不处理异常并且异常在程序中的其他位置(例如,在顶层)打印出来时使用。由于您使用自定义处理程序捕获异常,因此您应该能够将
JobState
放入异常中,并为其提供一个简单的Show
实例。例如:
The Show instance of exceptions is mainly used when you don't handle an exception yourself and it gets printed out somewhere else in your program (e.g., at the top level). Since you're catching the exceptions with a custom handler, you should be able to just put
JobState
in the exception and give it a trivialShow
instance .For example: