haskell - 在haskell中给`_`一个类型签名
问题描述
我想与之关联_
,coerce
但我不能给它一个类型签名。
有什么技巧可以解决这个问题吗?
import Data.Coerce
ok :: ()
ok =
let a = _ "hi"
in let a :: String = __ "Hi"
in ()
where
_ = undefined
__ :: Coercible a b => a -> b
__ = coerce
ko =
let a = _ "hi"
in let a :: String = __ "Hi"
in ()
where
__ = undefined
_ :: Coercible a b => a -> b. -- Invalid type signature: _ :: ... Should be of form <variable> :: <type>parser
_ = coerce
解决方案
_
是不能重新定义的保留名称。它可以在模式中用作通配符,例如
let (_,x) = .... -- takes the second component
(_,_,_,x,_) = .... -- takes the fourth component
_ = .... -- does not bind any variable
in ....
与其他变量名称不同,它可以在一个模式中出现多次。
它也可以用作孔:例如,
let a = _ "hi"
触发特殊错误
• Found hole: _ :: [Char] -> t
Where: ‘t’ is a rigid type variable bound by
the inferred type of a :: t
本质上,当hole_ "hi"
被替换为.t
_
t
因此,您的ok
esample 并不是真的 OK,而是触发了上述特殊错误。
推荐阅读
- java - ARCore 手势旋转问题
- javascript - javascript通过按钮切换div样式显示
- python-3.x - 使用连接到 Ui_MainWindow 的主类之外的类
- asp.net-mvc - 某些 PoP 服务器的 Microsoft 标准 CDN URL 重定向不起作用
- python - Python Web 套接字仅发送 1 条消息
- php - 如何在数千个代码页中找到一个字符串?
- python - 在 Mac 上,尽管有教程,但我无法连接安装 Python GAMS API。给出版本错误
- angular - 在 Angular 中导入本地库时生成错误“找不到模块”
- deep-learning - 注意力机制真的是注意力还是只是再次回顾记忆?
- .htaccess - .htaccess - RewriteRule 在另一个 RewriteRule 之后附加到 URL 的末尾