The Paradox of Partial Parametricity

Mark S. Miller erights at google.com
Fri May 10 06:21:21 PDT 2013


On Fri, May 10, 2013 at 6:05 AM, Mark S. Miller <erights at google.com> wrote:

> On Fri, May 10, 2013 at 5:55 AM, Mark S. Miller <erights at google.com>wrote:
> [...]
>
>>  This same abstraction in a Q-like promise system would be written
>>>
>>>     class AsyncTable<t,u> {
>>>         constructor() {
>>>             this.m = Map<t,u>(); // encapsulation doesn't matter for
>>> this example
>>>         }
>>>         set(keyP :Promise<t>, val :u) {
>>>             keyP.then(key => { this.m.set(key, val) });
>>>         }
>>>         get(keyP :Promise<t>) :Promise<u> {
>>>             return keyP.then(key => this.m.get(key));
>>>         }
>>>     }
>>>
>>
>
> Actually, it's even better:
>
>
>      class AsyncTable<t,u> {
>          constructor() {
>              this.m = Map<t,u>(); // encapsulation doesn't matter for this
> example
>          }
>          set(keyP :Promise<t>, val :Ref<u>) {
>              keyP.then(key => { this.m.set(key, val) });
>          }
>          get(keyP :Promise<t>) :Promise<u> {
>              return keyP.then(key => this.m.get(key));
>          }
>      }
>


The way one would actually write in Q is even better

      class AsyncTable<t,u> {
          constructor() {
              this.m = Map<t,u>(); // encapsulation doesn't matter for this
example
          }
          set(keyP :Ref<t>, val :Ref<u>) {
              Q(keyP).then(key => { this.m.set(key, val) });
          }
          get(keyP :Ref<t>) :Promise<u> {
              return Q(keyP).then(key => this.m.get(key));
          }
      }


The difference is instructive. The Q version of Postel's law is to either
require t or Ref<t>, and to either provide t or Promise<t>.


-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130510/61fa4fc3/attachment.html>


More information about the es-discuss mailing list