Rik
Rik

Reputation: 101

Eta reduce and DuplicateRecordFields language extension

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?

Upvotes: 7

Views: 146

Answers (1)

Reid Barton
Reid Barton

Reputation: 15019

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

Upvotes: 6

Related Questions