Changes between Version 15 and Version 16 of Records/DeclaredOverloadedRecordFields


Ignore:
Timestamp:
Feb 29, 2012 1:07:28 AM (2 years ago)
Author:
AntC
Comment:

added example of importing and not, due to popular mis-conception

Legend:

Unmodified
Added
Removed
Modified
  • Records/DeclaredOverloadedRecordFields

    v15 v16  
    172172  The existence of field `y` is hidden altogether. 
    173173 
     174With: 
     175{{{ 
     176module CRM               where 
     177    import CUST hiding (firstName, lastName)          -- note import is __not__ qualified 
     178 
     179    fieldLabel firstName :: r -> String 
     180    fieldLabel lastName :: r -> String 
     181 
     182    data Customer_Contact = Cust_Cont { customer_id :: Int, firstName, lastName :: String } 
     183 
     184}}} 
     185 
     186We're sharing fieldLabel `customer_id`, but we've got local fieldLabels for the names. There is no name clash! If you want to use the imported name labels, you have to qualify as `CUST.lastName`. 
     187 
     188Then this works: 
     189{{{ 
     190    contact1 :: Customer_Contact 
     191    custAddr1 :: Customer_NameAddress 
     192    ... 
     193    ... contact1.customer_id ...                         -- shared fieldLabel 
     194    ... custAddr1.customer_id ...                        -- 
     195    ... 
     196    ... contact1.firstName ...                           -- local fieldLabel 
     197    ... custAddr1.CUST.firstName ...                     -- imported fieldLabel used qualified 
     198    ...  
     199    ... 
     200    localfullName r = r.firstName ++ " " r.lastName 
     201}}} 
     202This doesn't: 
     203{{{ 
     204    ... custAddr1.firstName ... -- ==> No instance for (Has Customer_Contact Proxy_firstName t0) 
     205                                --     tried to use the local fieldLabel against an imported record type 
     206    ... contact1.fullName ...   -- ==> No instances for (Has Customer_Contact CUST.Proxy_firstName t0, 
     207                                --                       Has Customer_Contact CUST.Proxy_lastName t10) 
     208                                --        arising from a use of `fullName' 
     209                                --     tried to use an imported virtual field (used unqualified) against a local record type 
     210}}} 
     211because `fullName` is overloaded against the fieldLabel in module `CUST`, not the local module. 
     212 
     213Absolutely nothing magical going on here: all standard module/namespace control. Move along please. 
     214 
     215[There's a working example of all this importing, as an attachment to the implementor's page.] 
    174216 
    175217