Changes between Version 5 and Version 6 of Records/TypePunningDeclaredOverloadedRecordFields


Ignore:
Timestamp:
Mar 12, 2012 3:55:57 AM (3 years ago)
Author:
AntC
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Records/TypePunningDeclaredOverloadedRecordFields

    v5 v6  
    3434         customer_id :: Customer_id                                     -- field name puns on the type
    3535       , firstName   :: String                                          -- not a critical/shared field
    36        , lastName    :: String
     36       , lastName    :: String                                          -- so using H98 style
    3737       , ...   
    3838       }       sharing (Customer_id, ...)  deriving (...)               -- new sharing syntax
     
    5757    type instance SetResult Customer t    = Customer                    -- record type is not parametric, so doesn't change
    5858
     59    customer_id r = get r (undefined :: Customer_id)                    -- we can auto-decl this, see below
     60    customer_id :: r{ customer_id :: Customer_id } =>                   -- r{ ... } => is sugar for the Has constraint
     61                   r -> Customer_id -> Customer_id                      -- type inferred
     62                                                                       
     63
    5964    newtype FirstName = FirstName String                                -- newtype generated for non-sharing field
     65    firstName :: Customer -> FirstName -> String                        -- generated selector is monomorphic
     66    firstName r = get r (undefined :: FirstName)
     67
    6068    instance Has Customer FirstName                         where       -- Has instance generated
    6169        get Customer{ firstName = (FirstName x) } = x                   -- DisambiguateRecordFields pattern
     
    6573    -- type instance SetResult Customer FirstName  = Customer           -- not needed/already declared above
    6674
    67    customer_id r = get r :: Customer_id                                 -- we can auto-decl this, see below
    68    customer_id :: r{ customer_id :: Customer_id } => r -> Customer_id   -- type inferred for the function
    6975
    7076    myCust :: Customer                                                  -- usual record decl
     
    8187    It is an error to be `sharing` a record field without there being a same-named type in scope. The desugar for the data decl would create the instance to use the Type, but then the instance would fail.
    8288
    83 To generate the correct field selector function, there is to be a new deriving class:
     89To generate the correct field selector function, there is to be a new deriving class; and for record decls a shorthand:
    8490{{{
    8591    newtype Customer_id = Customer_id Int                               -- newtype or data decl, same name type and constr
    8692                                           deriving (Has, ...)          -- generates customer_id function, per above
     93}}}
    8794
     95Polymorphic record updates:
     96{{{
    8897    set (Customer_id 27) (                                              -- record update desugarred from above example
    8998               set (FirstName "Fred") myCust  )                         -- note nested