PureScript中Eq类型类实例实现报错,请求排查解决
Fixing Your PureScript TypeClasses Module Compilation Error
Let's walk through the issues in your code and get it compiling smoothly:
1. The Root Cause of the Compilation Error
The critical mistake here is a typo in your Eq instance's eq function:
- You wrote
p1.y1 == p2.y2, but yourPointdata type only has fieldsxandy—there's noy1ory2defined. This is exactly what's triggering the line 17 compilation error.
2. Bonus: Clean Up Redundant Code
Beyond the typo, you can simplify the eq function: PureScript lets you return boolean expressions directly, so the if ... then true else false wrapper is unnecessary and adds extra noise.
Corrected Full Code
module TypeClasses where import Prelude import Data.Array import Data.Number.Format(toString) data Point = Point { x :: Number, y :: Number } instance showPoint :: Show Point where show (Point { x, y }) = toString x <> ", " <> toString y instance eqPoint :: Eq Point where eq p1 p2 = p1.x == p2.x && p1.y == p2.y
Key Changes Explained
- Fixed the typo: Changed
p1.y1 == p2.y2top1.y == p2.yto match thePointtype's actual field names. - Removed redundant logic: The boolean comparison result can be returned directly, making the code cleaner and more aligned with idiomatic PureScript practices.
内容的提问来源于stack exchange,提问作者Knows Not Much
相关产品推荐
相关产品推荐

