我可以在 GHCi 中添加实例声明吗
我正在摆弄 HashMap
并尝试使用 Data.Bson.ObjectId
作为键。当然,我发现该结构没有 Hashable
实例。没关系,因为编写一个代码很简单。1
instance Hashable ObjectId where hash (Oid x y) = hash (x,y)
我在 GHCi 中输入了这一行,并被告知“在输入‘实例’上解析错误”。这实际上是有意义的,因为 GHCi 提示符的运行方式就好像这些行被键入到 IO monad 中的 do 块中,并且无法在此上下文中定义实例。
那么我的问题是,有没有办法在 GHCi 中定义新实例?
1 为什么库不提供这个实例是另一回事。我相信答案是限制依赖关系,除了 bson 包已经依赖于阳光下的一切。
I was messing around with HashMap
and tried to use a Data.Bson.ObjectId
as a key. I, of course, discovered that there is not a Hashable
instance for that structure. That's ok, because writing one is trivial.1
instance Hashable ObjectId where hash (Oid x y) = hash (x,y)
I typed that line into GHCi and was told "parse error on input `instance'". This actually makes sense as the GHCi prompt operates as if the lines were being typed into a do block in the IO monad and an instance can not be defined in this context.
My question then, is there a way to define a new instance within GHCi?
1 Why this instance is not provided by the library is another matter. I would believe the answer was to limit dependencies except that the bson package already depends on everything under the sun.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好消息:是的,有一种方法可以在 GHCi 中定义新实例。
坏消息:目前,这样做的第一步是“安装 GHC 的开发快照”。
很长一段时间以来,这一直是 GHCi 中明显缺失的功能。它的缺席并没有什么内在原因,但我认为实施起来有些困难,所以它被搁置了。
然而,似乎从版本 7.4.1 开始,现在可用:
您是否认为现在值得安装 GHC 的非发行版本,这取决于您。
The good news: Yes, there is a way to define a new instance within GHCi.
The bad news: At the moment, the first step in doing so is "install a development snapshot of GHC".
This has been an obvious bit of missing functionality in GHCi for quite a while. There was no inherent reason for it to be absent, but I assume it was somewhat difficult to implement and so it got set aside.
However, it seems that as of version 7.4.1, it's now available:
Whether you think having that right now worth the hassle of installing a non-release version of GHC is up to you.