Guards

Brendan Eich brendan at mozilla.com
Fri May 27 01:44:20 PDT 2011


On May 26, 2011, at 9:36 PM, Peter Michaux wrote:

> I'm looking at the strawman for guards.
> 
> http://wiki.ecmascript.org/doku.php?id=strawman:guards
> 
> I don't quite see how they are created.
> 
> I understand this part about how they would be used
> 
>  function f(p :: String, q :: MyType) :: Boolean { ... }
> 
> but there is no specific example of how MyType is defined. Is that
> related to classes at all? I'm hoping it is not related to classes and
> that it is more akin to some kind of interface definition.

From http://wiki.ecmascript.org/doku.php?id=strawman:guards#creating_guards,

"Optionally we may have an Object.setBrand(guard, brand) method that creates a [[Brand]] internal property on an extensible object guard that doesn’t already have one. It is an error to use Object.setBrand on a guard object that already has a [[Brand]] internal property."


> 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. 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.


> What about maybe types? Could MyType be defined so that null is not an
> allowed value? All the manually written not-null parameter checks in
> Java are an unfortunate result of its type checking system.

A static type system could perhaps be built on top of Harmony modules. Sam Tobin-Hochstadt's Typed Racket work seems helpful here, although it uses contracts too. This is all good research work.

The point is it is research, not ready to be standardized.


> One use case I could see would be using guards in development and then
> having the minifier strip the guards for production. Then the guard
> definitions could be stripped as well.

As runtime checkers, you can't erase guards. If you knew a particular use of guards as proposed was a static system that permitted erasure (I have no idea whether this is realistic), then sure. We're a long way from anything like that.

/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110527/8fd143fd/attachment.html>


More information about the es-discuss mailing list