kcsongor / generic-lens

Generically derive traversals, lenses, and prisms.
437 stars 53 forks source link

Product-type downcast (record extension) #47

Open lortabac opened 6 years ago

lortabac commented 6 years ago

Since we have an isomorphism between HLists and records, if we expose the List type we can easily implement a downcast function to extend records with an HList. However, since the isomorphism is based on position, we can only append fields at the end. Ideally we would like to convert from HLists to records by only looking at the names of the field, but this would require a substantial modification of the GIsList instances.

Do you think it is possible and worth the additional complexity? Record extension seems a very useful addition to me.

kcsongor commented 6 years ago

I think this is a good idea, and it's definitely possible. Not sure what would be the best way to implement it though; perhaps we could reuse the logic behind upcast to find what already exists, and the fields that don't - then require the missing fields in an HList (or as a curried function) and finally put everything back together? It should probably be OK to fix the order in which the missing fields have to be provided, so that we can infer the type based on the diff. What do you think?

lortabac commented 6 years ago

I like your idea. As you say, if we require the missing fields to be provided in order, we may not even need to expose an HList type. Since the types are inferred, the user-facing function can take variadic arguments or a tuple, which is preferable IMHO.