Issue is about having 2 data types, Transaction
and FormatModel
, both having formatId
field. To prevent adding type signatures to get formatId
from a transaction
or formatModel
, I have created type class HasFormat
:
class HasFormat a where
formatId_ :: a -> FormatId
instance HasFormat Transaction where
formatId_ x = formatId x -- gives error because ambiguous occurrence ‘formatId’
instance HasFormat FormatModel where
formatId_ = formatId -- this works
Can some explain why the instance which has eta reduced implementation is working and the other one not?
Disambiguation of duplicate record fields is necessarily a best-effort kind of thing because it needs to occur before type checking (you can't generally type check an expression before you know what identifiers the names in it refer to; which is what the disambiguation is doing).
Your non-working example is equivalent to this non-working example from the documentation:
data S = MkS { x :: Int }
data T = MkT { x :: Bool }
bad :: S -> Int
bad s = x s
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With