- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些类型的实例。我们称它们为 A、B 和 C。它们都是类型类 X 的实例。现在我想创建一个单独的函数 create
给定一些输入(比如说一个字符串),它会创建一个 A、B 或 C 的实例。类型系统无法知道什么输入将给出什么类型。那是 Haskell 不喜欢的东西,我想我知道答案,但我想确定一下。我得到的当前错误是:
• Couldn't match expected type ‘c’ with actual type ‘GCCCommand’
‘c’ is a rigid type variable bound by
the type signature for:
compiler :: forall c. CompilerCommand c => String -> c
at src/System/Command/Typed/CC.hs:29:1-44
• In the expression: gcc path
In an equation for ‘compiler’:
compiler path
| exe == "g++" || exe == "gcc" || exe == "cc" || exe == "cpp"
= gcc path
where
exe = takeFileName path
• Relevant bindings include
compiler :: String -> c
(bound at src/System/Command/Typed/CC.hs:31:1)
data SuperX = SuperA A | SuperB B | SuperC C
create :: String -> SuperX
-- create can now be implemented
instance X SuperX where
-- a lot of boilerplate code ...
最佳答案
这取决于你需要用它做什么。
如果您以后的处理不关心它是否得到 A
, 一个 B
, 或 C
, 只是它得到了实现 X
的东西...
restOfProgram :: X a => a -> ThingIWantToCompute
parseABC :: (X a => a -> r) -> String -> Maybe r
parseABC f "A" = Just (f A)
parseABC f ('B':xs) = Just (f (B xs))
parseABC f ('C':xs) = Just (f (C (read xs)))
parseABC _ _ = Nothing
data SomeX where
SomeX :: X t => t -> SomeX
parseABC :: String -> Maybe SomeX
parseABC "A" = Just (SomeX A)
parseABC ('B':xs) = Just (SomeX (B xs))
parseABC ('C':xs) = Just (SomeX (C (read xs)))
parseABC _ _ = Nothing
restOfProgram' :: SomeX -> ThingIWantToCompute
restOfProgram' (SomeX t) = restOfProgram t
A
有不同的路径,
B
或
C
,您可能希望返回一个总和类型,如
SuperX
.
关于haskell - 如何在 Haskell 中工作的返回类型上获得 'unpredictable' 重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53837669/
这个问题在这里已经有了答案: Relationship between Threads and println() statements (2 个回答) 6年前关闭。 我正在浏览 volatile 的
对 Scala 2.8 的收集行为感到非常沮丧。问题是:我正在创建一个数独板。我正在标记从 A1 到 I9 的单元格(字母是行,数字是列)。我想获得棋盘上的单位列表,即第 9 行、夜间列和夜间象限。
问题的一点概述。 假设我有一个名为 TableA 的表,它具有固定属性 PropertyA、PropertyB、PropertyC。这已经足以满足您自己的网站需求,但是您突然有客户想要在您的网站上自定
我有一些类型的实例。我们称它们为 A、B 和 C。它们都是类型类 X 的实例。现在我想创建一个单独的函数 create给定一些输入(比如说一个字符串),它会创建一个 A、B 或 C 的实例。类型系统无
如有必要,我会发布一个 JSFIddle,因为我使用的是标记语言,这会花费一些精力,但请转到此处:http://mixtape.meteor.com 添加足够的元素,以便溢出并出现滚动条 现在尝试检查
请引用下面的 LinqPad 脚本。 在实现工作流时,我从集合 (IEnumerable) 中获取下一组 HasRun 任务。在迭代 Linq 查询的结果集时,我将任务更改为 HasRun = tru
我正在创建一个 PDF,其中包含几个简单的表格。每个表格包含 1-2 个单元格:一个图像单元格,大多数表格还包含一个文本单元格。 它适用于所有 10 个图像,除了其中一张图像:图像未渲染。 现在,我获
我正在帮助处理仪表板中的交互式表格。单击表中的行会更改显示在仪表板其余部分的数据。表行可以有一个 'selected' 类,其中选定的行有一个 4px 的粗体边框。这些行还有一个 :hover 选择器
当使用 myDelegate -= eventHandler ReSharper(版本 6)问题时: Delegate subtraction has unpredictable result 这背后
我在 xcode 中打开了一个新标志,并收到警告“弱接收器在 ARC 模式下可能不可预测地为空”。这让我很困惑,因为它当然可能为零。 最佳答案 我一周前问过这个问题,但没有收到任何答复,但格雷格·帕克
我编写了一个 Java 套接字服务器,它将保持连接处于 Activity 状态,直到客户端断开连接。我的客户端代码将继续将消息推送到该服务器应用程序。 但是当我运行这些程序一段时间时,我也发现了一种不
我是一名优秀的程序员,十分优秀!