Changes between Version 35 and Version 36 of Records/OverloadedRecordFields/Plan


Ignore:
Timestamp:
Sep 3, 2013 10:58:40 AM (10 months ago)
Author:
adamgundry
Comment:

virtual record fields

Legend:

Unmodified
Added
Removed
Modified
  • Records/OverloadedRecordFields/Plan

    v35 v36  
    390390 
    391391 
    392 === User-defined `Has` instances === 
    393  
    394 The user can write explicit `Has` instances, and they are scoped normally. For example: 
    395  
    396 {{{ 
    397 instance ctx => Has r "x" t where 
    398   getField = blah :: proxy "x" -> r -> t 
    399 }}} 
    400  
    401 Even with an explicit `Has` instance as above, the name `x` will not be in scope unless a datatype has a field with name `x`. This is likely to make it less useful. However, it does allow virtual fields to be declared. 
     392=== Virtual record fields === 
     393 
     394We could imagine supporting virtual record fields by allowing the user to declare their own instances of `Has` and `GetResult` (and possibly `Upd` and `SetResult`). For example, the user could write the following: 
     395 
     396{{{ 
     397data Person = MkPerson { firstName :: String, lastName :: String } 
     398 
     399type instance GetResult Person "fullName" = String 
     400instance Has Person "fullName" String where 
     401  getField _ p = firstName p ++ " " ++ lastName p 
     402}}} 
     403 
     404This means that the `Person` type can be used where a type with a field `fullName` is expected. Since no `SetResult` and `Upd` instances are provided, the field cannot be updated. 
     405 
     406However, this does not bring `fullName` into scope as a field, [#Scopeissuesorwhywemissdot as previously observed]. Moreover, it is difficult to check the type family instances for consistency. For example, given the following declaration 
     407 
     408{{{ 
     409type instance GetResult a "foo" = Int 
     410}}} 
     411 
     412we would need to check that any datatype with a field `foo` in scope gave it the type `Int`. For these reasons, user-defined instances of the classes are not currently permitted, so virtual fields are not available. 
    402413 
    403414