Haskell——有什么方法可以在“派生”实例的情况下关闭可重新绑定的语法?
有一个恼人的“功能”,即派生实例也会受到 RebindableSyntax 扩展的影响。我想写的示例:
{-# LANGUAGE RebindableSyntax #-}
import qualified Prelude
data Color = Red | Green | Blue | Periwinkle | Fuschia deriving (Prelude.Eq, Prelude.Ord)
这会出现错误“不在范围内:`ifThenElse'”。
There's an annoying "feature" that deriving instances are also affected by the RebindableSyntax
extension. Example of what I want to write:
{-# LANGUAGE RebindableSyntax #-}
import qualified Prelude
data Color = Red | Green | Blue | Periwinkle | Fuschia deriving (Prelude.Eq, Prelude.Ord)
This comes up with the error " Not in scope: `ifThenElse' ".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我来说似乎是一个错误——无论如何,
deriving
子句充满了内置的魔法,所以我怀疑使用反弹语法的派生实例在实践中是否有用。现在,如果您还可以重新绑定deriving
子句本身并使用TH拼接来代替......但我离题了。我怀疑最简单和最简单的解决方案是使用不同的模块。将数据类型定义放入其自己的模块中,在范围内使用 deriving 子句和 Prelude 函数,然后使用可重新绑定语法将类型导入模块中。如果您的模块需要进一步的模块化,请注意
StandaloneDeriving
也存在,它可以让您在一个模块中定义类型(使用RebindableSyntax
活动),并在另一个模块中派生实例模块(没有RebindableSyntax
),并从实际使用该类型的模块导入这两个模块。That seems like a misfeature to me--the
deriving
clause is full of built-in magic anyway, so I'm skeptical that derived instances using rebound syntax would be useful in practice. Now, if you could also rebind thederiving
clause itself and use a TH splice instead... but I digress.I suspect the simplest and easiest solution is to use different modules. Put the data type definition in its own module, use the
deriving
clause there with the Prelude functions in scope, then import the type in the module using rebindable syntax. If you need further modularity in your modules, note thatStandaloneDeriving
also exists, and would let you define the types in one module (withRebindableSyntax
active), derive the instances in another module (withoutRebindableSyntax
), and import both from modules that actually use the type.