Changes between Initial Version and Version 1 of Records/DeclaredOverloadedRecordFields/PolyRecordPattern


Ignore:
Timestamp:
Feb 19, 2012 9:49:06 PM (3 years ago)
Author:
guest
Comment:

added, in response to John's suggestion

Legend:

Unmodified
Added
Removed
Modified
  • Records/DeclaredOverloadedRecordFields/PolyRecordPattern

    v1 v1  
     1= Polymorphic record patterns ('''''speculative''''' future) = 
     2 
     3John Meacham has requested the ability to put polymorphic record syntax in a pattern. http://www.haskell.org/pipermail/glasgow-haskell-users/2012-February/021918.html 
     4 
     5Without trying to match his request exactly, here's a possible desugarring within the DORF framework. (I am __not__ suggesting this gets implemented until we've more experience with DORF.) 
     6 
     7Syntax like this in a pattern: 
     8{{{ 
     9case r of { 
     10    r{customer_id, firstName, lastName } -> ... customer_id ... 
     11    } 
     12}}} 
     13Note: 
     14 * there's a lower-case name prefix to the `{ ... }` body, so this is __not__ a data constructor 
     15 * this is an irrefutable name binding, not really a pattern 
     16 * (That is, if `r` doesn't happen to have all those fields, we'll just fail to compile, we won't try to go on to the next case branch.) 
     17 * This example is using '''-XNamedFieldPuns''', we could also use the `{ blah = foo }` syntax John gives. 
     18 
     19The obvious way to desugar: 
     20{{{ 
     21let { customer_id = customer_id r; ... 
     22  in { ... customer_id ...} 
     23}}} 
     24won't work because we get name capture inside the let declarations. (The dangers of punning!) So it'll have to be: 
     25{{{ 
     26let { customer_id_via = customer_id r; ... } 
     27  in { let { customer_id = customer_id_via; ... } 
     28         in { ... customer_id ...} 
     29}}} 
     30    [Implementor's note: can we improve this?] 
     31Within the body we can indeed use polymorphic record update syntax to reconstitute the record: 
     32{{{ 
     33... r{ lastName = map toUpper lastName } ... 
     34}}} 
     35Note that since there's no data constructor, this will continue `r`'s existing data constructor.