简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM