在异常处理中使用 GADT

发布于 2024-12-04 18:35:23 字数 1501 浏览 1 评论 0原文

这是我的问题

,我使用 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?

Fun With Phantom Types

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2024-12-11 18:35:23

Show 异常实例主要在您自己不处理异常并且异常在程序中的其他位置(例如,在顶层)打印出来时使用。由于您使用自定义处理程序捕获异常,因此您应该能够将 JobState 放入异常中,并为其提供一个简单的 Show 实例。

例如:

data JobState = JobSuccess | JobFail deriving Show 
newtype JobException = JobException (MVar JobState) deriving Typeable
instance Show JobException where
    show _ = "JobException"
instance Exception JobException

exceptionHandler :: JobException -> IO ()
exceptionHandler (JobException m) = takeMVar m >>= print

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 trivial Show instance .

For example:

data JobState = JobSuccess | JobFail deriving Show 
newtype JobException = JobException (MVar JobState) deriving Typeable
instance Show JobException where
    show _ = "JobException"
instance Exception JobException

exceptionHandler :: JobException -> IO ()
exceptionHandler (JobException m) = takeMVar m >>= print
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文