Guards

Brendan Eich brendan at mozilla.com
Fri May 27 16:21:52 PDT 2011


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



More information about the es-discuss mailing list