reformed with

Lars T Hansen lth at acm.org
Sun Feb 4 06:42:46 PST 2007


On Feb 4, 2007, at 1:31 PM, Yuh-Ruey Chen wrote:

> I've been looking at the reformed with proposal. It seems that using a
> let statement would be much easier than writing a reformed with
> statement. Compare:
>
> with (obj.obj2: {a: int, b:Object, c:String, d:Array) {
>   a = 10;
>   var x = b;
>   c = 'hi';
>   d.push(5.5);
> }
>
> vs.
>
> let (o = obj.obj2) {
>   o.a = 10;
>   var x = o.b;
>   o.c = 'hi';
>   o.d.push(5.5);
> }
>
> Brendan makes the argument that "migrating code should not require
> hand-expanding with to get precise types", but I would argue it would
> take just as much time (if not more) to look within the |with|  
> block to
> see what properties need to be in the object literal and defining  
> their
> types there, than to change the whole thing into a let statement. An
> example (sample code found with google code search):
>
>     with ( this.DOMTable )
>     {
>         // Sets the toolbar direction. IE uses "styleFloat" and Gecko
> uses "cssFloat".
>         style.styleFloat = style.cssFloat = FCKLang.Dir == 'rtl' ?
> 'right' : 'left' ;
>
>         cellPadding = 0 ;
>         cellSpacing = 0 ;
>         border = 0 ;
>     }
>
> What's easier here? Changing the first line to |with(this.DOMTable:
> {style: Object, cellPadding: int, cellSpacing: int, border: int})|. Or
> changing the first line to |let(o = this.DOMTable)| and prefixing each
> appropriate property with |o.|. Note that in both cases, the author  
> has
> to search through the |with| block to see which names are  
> properties of
> DOMTable. It's bad either way. Moreso for reformed with IMO, since you
> also have to enter the types of each property.

In practice your object would probably be of some named type, so you  
would
really do something like this:

     with (this.DOMTable : domtable_t) {
         ...
     }

Whether you define domtable_t yourself or the environment defines it  
for you,
having named types makes reformed with (as well as most uses of the  
structural
type system) much more pleasant.

If you need a subset view on the object (with access to other  
variables outside
the with having the same name as properties of the object) then I  
agree with
you, reformed with is cumbersome and most people would not use it,  
unless
the subset view could be named and used several places.

> There's also the argument of discoverability. Script authors are  
> likely
> to learn the new let syntax since it can be used pretty much anywhere.
> The reformed with syntax, on the other hand, is a bit obscure. If a
> feature isn't that discoverable and there's an alternative feature  
> that
> is and is just as good, I'd say drop the less discoverable feature.

I agree.  At the last committee meeting we discussed putting some  
restrictions
on classical "with" in strict mode (though the minutes do not record any
conclusions).  If we were to do that, however, the reformed with  
might fare
better.

> Unless there's reformed with would result in noticeably better
> performance than the let equivalent,

Depends on the implementation and the exact context, but a loop inside a
classical "with" could be a performance drain because all name  
lookups would
normally be searches by name, not by lexical address.

> I'd say that |with| is a lost cause
> and reforming |with| is unnecessary.
>

I'd like to remove both kinds of "with" from the language.  But I  
lost that
battle :-)

--lars






More information about the Es4-discuss mailing list