有没有更好的方式来表达这种类型?

发布于 2024-12-09 06:52:25 字数 271 浏览 0 评论 0原文

我制作了这种数据类型,其中有很多重复。

data JobState = UnsanitizedData Handle
              | SanitizedData Handle
              | VerifiedData Handle
              | JobFail Handle

我不希望在没有 Handle 的情况下使用这些 JobStates 中的任何一个。但重复让我怀疑我没有正确表达这一点。有更好的办法吗?

I've made this data type, which has a lot of repetition in it.

data JobState = UnsanitizedData Handle
              | SanitizedData Handle
              | VerifiedData Handle
              | JobFail Handle

I don't want any of these JobStates to ever be used without Handle. But the repetition has me suspecting I'm not expressing this correctly. Is there a better way?

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

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

发布评论

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

评论(1

他不在意 2024-12-16 06:52:25

一种方法是将其分解为状态值和包含句柄的包装器:

data JobState = UnsanitizedData | SanitizedData | VerifiedData | JobFail
  deriving (Eq, Enum, Ord, Show, Read)
data Job = Job { jobState :: JobState, jobHandle :: Handle }
-- or: data Job = Job JobState Handle

One way would be to factor this out into a state value, and a wrapper that includes the handle:

data JobState = UnsanitizedData | SanitizedData | VerifiedData | JobFail
  deriving (Eq, Enum, Ord, Show, Read)
data Job = Job { jobState :: JobState, jobHandle :: Handle }
-- or: data Job = Job JobState Handle
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文