Changes between Version 13 and Version 14 of Records/DeclaredOverloadedRecordFields
- Timestamp:
- 02/21/12 15:44:37 (15 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Records/DeclaredOverloadedRecordFields
v13 v14 178 178 {{{ 179 179 case r of { 180 Cust_Price {unit_Price, ..} -> Cust_Price {unit_Price = unit_Price * 1.05, .. }180 Cust_Price{ unit_Price, .. } -> Cust_Price{ unit_Price = unit_Price * 1.05, .. } 181 181 } -- increases Price by 5% 182 182 }}} … … 189 189 Returns a record with same fields as `myPrice`, except a different `unit_Price`. Note that the update can change the type of a field (if the record declaration is polymorphic). 190 190 191 Note that upon first encountering that expression, we don't know the record types (because `unit_Price` is overloaded). So the types initially inferred are:191 Upon first encountering that expression, we don't know the record types (because `unit_Price` is overloaded). So the types initially inferred are: 192 192 {{{ 193 193 <expr> :: r { unit_Price :: Int } => r … … 196 196 That is, the update might be changing the record type as well as the field type -- in case that the record type is parametric over the field type. 197 197 198 Behind the scenes, the update syntax with an expression prefix to the `{ ... }`is syntactic sugar for a call to the polymorphic record update method `set`:198 Behind the scenes, the update syntax with an expression prefix `e{ ... }` (as opposed to a data constructor `MkT{ .. }`) is syntactic sugar for a call to the polymorphic record update method `set`: 199 199 {{{ 200 200 set (undefined :: Proxy_unit_Price) (72 :: Int) myPrice … … 206 206 You can update multiple fields at the same time: 207 207 {{{ 208 myCustNA { firstName = "Fred", lastName = "Dagg" }208 myCustNA{ firstName = "Fred", lastName = "Dagg" } 209 209 }}} 210 210 [There's a poor story to tell here in implementation terms: we split into two calls to `set`, one nested inside the other. It's wasteful to build the intermediate record. Worse, the two fields' types might be parametric in the record type or polymorphically related (perhaps one is a method to apply to the other), then we get a type failure on the intermediate record.]
