在类型类约束中使用`typeError'时避免``undefined''
我有一个类型类别的实例:
instance {-# OVERLAPPABLE #-} (TypeError ( 'Text "Some error")) => SomeClass x where
someMethod = undefined
此实例存在于其他(有效)实例的末尾。当用户写入不符合这些有效实例的类型时,想法是让编译器抛出类型错误。 typeerror
在实例约束中实现了这一点,但这也迫使我用undefined
填写该方法,感觉就像是黑客攻击。
有没有办法避免这种情况?还是更好?
I have a type-class instance like this:
instance {-# OVERLAPPABLE #-} (TypeError ( 'Text "Some error")) => SomeClass x where
someMethod = undefined
This instance exists at the end of other (valid) instances. The idea is to have the compiler throw a type error when the user writes a type that doesn't adhere to these valid instances. TypeError
in instance constraint achieves this, but this also forces me to fill in the method with undefined
, which feels like a hack.
Is there a way to avoid this? Or do it better?
Here's the real-world code with this pattern.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能实现的最好的方法是。从本质上讲,我定义了
typeerr
类型的家族代表标准typeerror
约束和附加不可能的
约束提供所需的见证。不可能的
总是无法作为约束解决,但是无论如何,类型错误还是首先触发。The best I could achieve is this. Essentially, I defined a
TypeErr
type family standing for both the standardTypeError
constraint and an additionalImpossible
constraint providing the needed witness.Impossible
would always fail to resolve as a constraint, but the type error is triggered first anyway.我们有不允许
Trevial-Constraint
软件包中的类。它提供 nope pseudo-method,它是undefined
的版本,只能在不可能的上下文中使用(并通过可能“使用” 来见证这一点未包装,您无法使用标准未定义的
)。We have the
Disallowed
class in thetrivial-constraint
package. It offers thenope
pseudo-method, which is a version ofundefined
that can only be used in impossible contexts (and witnesses this by being possible to “use” unboxed, which you can't do with standardundefined
).