Changes between Version 11 and Version 12 of Records/DeclaredOverloadedRecordFields
- Timestamp:
- 02/21/12 15:33:47 (15 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Records/DeclaredOverloadedRecordFields
v11 v12 54 54 55 55 We 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:''' 56 58 {{{ 57 59 fieldLabel customer_id :: r -> Int 58 60 }}} 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].) 60 68 61 69 The `-> Int` means the field's domain (type) is `Int` -- it's just a type. 62 70 We might also want to constrain the record -- for example to be sure it is savable to persistent storage: 63 71 {{{ 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 67 77 68 78 Now 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: … … 76 86 data Customer_Order = Cust_Order { customer_id :: Int, ... } 77 87 }}} 78 Then a field selection expression like: 88 89 === Field Selection === 90 91 With those records declared, a field selection expression like: 79 92 `... (customer_id r) ...` -- H98 style field application 80 93 uses familiar type instance resolution to figure out from record type `r` how to extract the `customer_id`. … … 90 103 The type inferred would be: 91 104 {{{ 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) 93 108 }}} 94 109 which is eliding: … … 97 112 => r -> String 98 113 }}} 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, usingdot notation:114 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 field selection using '''pseudo-''' or''' 'virtual' '''fields, with dot notation: 100 115 {{{ 101 116 customer.fullName
