Changes between Version 11 and Version 12 of Records/DeclaredOverloadedRecordFields


Ignore:
Timestamp:
Feb 21, 2012 11:33:47 PM (3 years ago)
Author:
guest
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Records/DeclaredOverloadedRecordFields

    v11 v12  
    5454
    5555We need a way to declare that a name is available as an overloadable field name (roughly speaking, a class/method definition), proposed syntax:
     56
     57'''Option One: new `fieldLabel` style of declaration:'''
    5658{{{
    5759    fieldLabel customer_id :: r -> Int
    5860}}}
    59 (The `r{ ... }` is added by the desugarer.)
     61    (The `r{ ... }` is added by the desugarer.)
     62
     63'''Option Two: explicit record constraint on the function:'''
     64{{{
     65    customer_id :: r{ customer_id :: Int} => r -> Int          -- field name same as the declared function
     66}}}
     67    (See discussion at [wiki:Records/DeclaredOverloadedRecordFields/COmpareSORF#TheStringtypeparametertoHasandScopecontrol Wild afterthought].)
    6068
    6169The `-> Int` means the field's domain (type) is `Int` -- it's just a type.
    6270We might also want to constrain the record -- for example to be sure it is savable to persistent storage:
    6371{{{
    64     fieldLabel unitPrice :: (Save r, Num t) => r -> t
    65 }}}
    66 (Again the `r{ ... }` gets added as a further constraint.)
     72    fieldLabel unitPrice :: (Save r, Num t) => r -> t    -- again the `r{ ... }` gets added as a further constraint
     73-- or
     74    unitPrice :: (r{ unitPrice :: t}, Save r, Num t) => r -> t     -- again repeated field name
     75}}}
     76
    6777
    6878Now we can use the field in a record, and that in effect declares an instance for the field/record. All these definitions are in the same module:
     
    7686    data Customer_Order = Cust_Order { customer_id :: Int, ... }
    7787}}}
    78 Then a field selection expression like:
     88
     89=== Field Selection ===
     90
     91With those records declared, a field selection expression like:
    7992    `... (customer_id r) ...`          -- H98 style field application
    8093uses familiar type instance resolution to figure out from record type `r` how to extract the `customer_id`.
     
    90103The type inferred would be:
    91104{{{
    92     fullName :: r{ firstName, lastName :: String} => r -> String
     105    fullName :: r{ firstName, lastName :: String} => r -> String               -- could declare this for yourself
     106                                                 -- note this is __not__ like a field label decl (Option Two)
     107                                                 -- because the function's name is different to the field(s)
    93108}}}
    94109which is eliding:
     
    97112                 => r -> String
    98113}}}
    99 And if you think that's very close to the type of a field selector function, you'd be right. Here's some more examples of pseudo- or 'virtual' fields, using dot notation:
     114And if you think that's very close to the type of a field selector function, you'd be right. Here's some more examples of field selection using '''pseudo-''' or''' 'virtual' '''fields, with dot notation:
    100115{{{
    101116    customer.fullName