Question: Feasibility of polymorphic field selectors
Please let me know if there's a better place to ask feasibility questions.
I was just lamenting in the work chat how DuplicateRecordFields
isn't quite smart enough, so we had to invent all this OverloadedRecordDot
to work with it correctly. It occurred to me that top-level field selectors could just be given polymorphic types.
Supposing we had a module like this,
module X where
data Foo = Foo { name :: String, age :: Int }
Currently, we generate these top-level functions, ish
name :: Foo -> String
name (Foo n _) = n
age :: Foo -> Int
age (Foo _ a) = a
But, we could instead generate these:
name :: HasField "name" a b => a -> b
name = getField @"name"
age :: HasField "age" a b => a -> b
age = getField @"age"
This seems somewhat obvious to me, which makes me suspicious that it must have been considered and rejected for some reason previously. A brief search was unable to find anything on this. So I'm curious if there's any reason this wouldn't be easy to support.
It would be pretty easy to support this with TemplateHaskell
and NoFieldSelectors
, so I can definitely see why we'd want to just use NoFieldSelectors
and let anyone who wants to do anything other than the standard rely on that.