Guards

David Herman dherman at mozilla.com
Fri May 27 17:03:36 PDT 2011


To a first, approximation, it would look something like this:

    http://doc.racket-lang.org/reference/contracts.html

;-)

Seriously, the idea is to create contracts that can check structural properties by wrapping values with proxies that lazily do the checking. The core idea, known as higher-order contracts, was first published by Robby Findler:

    http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.11.4081

Cormac's student, Tim Disney, will be joining us for the summer starting in June, and we'll work on building such a library for JS using proxies. We'll also look into building the syntax into Narcissus.

Dave

On May 27, 2011, at 4:21 PM, Brendan Eich wrote:

> On May 27, 2011, at 10:59 AM, Peter Michaux wrote:
> 
>> I'm impressed with the following if this is what would actually be possible...
>> 
>> function(guard = identityGuard :: guardGuard)
>> 
>> What we have to write now in ES3 can be so long to achieve this kind
>> of default value and parameter checking functionality.
> 
> That is the idea.
> 
> Notice there's no type system or contract system spec here. This is just syntax plus an extension mechanism for coercing (which can throw).
> 
> 
>>> Call brand.coerce(s). This will either return a value (either s or
>>> possibly a coerced version of s) or throw an exception.
>> 
>> So the return value is actually what the parameter is set to before
>> the function body executes in the case of a guarded parameter?
> 
> Yes.
> 
> 
>>>> Suppose I
>>>> wanted MyType to be only positive even integers. Perhaps MyType is a
>>>> can only be a function that takes a single argument that is a string
>>>> and returns an integer. Perhaps MyType has to be an object with two
>>>> function properties named alpha and beta.
>>> 
>>> Exactly! Some in TC39 want to research contract systems for JS.
>> 
>> This is an alternate proposal to the guards, correct? I don't see
>> anything in the wiki index about contracts.
> 
> No, the guards proposal is not contracts only, nor is it runtime nominal typing only (see http://wiki.ecmascript.org/doku.php?id=strawman:trademarks which was not promoted), nor is it runtime structural types. It's syntax + a plugin system for "coercion".
> 
> 
>>> We have a
>>> research intern at Mozilla building such a prototype this summer. The plan
>>> was to build a library without syntax, but Waldemar's proposal would give
>>> nice syntax (we think -- details to hash out of course) for this work.
>> 
>> I'd be interested in such a library. Is there any more information
>> what it might look like to use it?
> 
> Not yet -- more when the intern starts, I imagine. Dave may know more.
> 
> /be
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list