Extending Eta and DuplicateRecordFields

The problem is that there are two data types: Transaction and FormatModel , which have a formatId field. To prevent the addition of type signatures to get formatId from Transaction or FormatModel , I created a class of type 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 someone explain why the eta reduced implementation instance works and the other doesn't?

+6
source share
1 answer

Inconsistency in the fields of duplicate entries is necessarily the best way, because it must happen before type checking (you usually cannot enter expression checking before you know what identifiers name names in it, the value matters).

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 = xs 
+5
source

Source: https://habr.com/ru/post/1015373/


All Articles