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 theDatatypeInfoOf
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.
没有评论:
发表评论