2021年4月7日星期三

Deriving projection functions using `generics-sop`

How would I go about deriving the function

getField :: (Generic a, HasDatatypeInfo a) => Proxy (name :: Symbol) -> a -> b  

to project a field from an arbitrary record using a type-level string (Symbol), using the generics-sop library?

This is similar to Retrieving record function in generic SOP, but I have the following problems:

  • The OP does not explain how to go the last mile to get the signature I desire.
  • The OP defines complex special-purpose helper types, which I am keen to avoid
  • The given solution only errors out at runtime, but compile-time matching should be possible, since a type-level DataTypeInfo is provided through the DatatypeInfoOf type family (nice to have, but not necessary).

The lens-sop package also seems to do something similar, but I can't work out how to make it work for me.

I would also prefer a solution that uses the IsProductType typeclass.

https://stackoverflow.com/questions/66992276/deriving-projection-functions-using-generics-sop April 08, 2021 at 02:41AM

没有评论:

发表评论