Array comprehension syntax

View: New views
3 Messages — Rating Filter:   Alert me  

Array comprehension syntax

by Olav Kjær :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

While experimenting with code-completion for ECMAScript I noticed a
limitation with array comprehension syntax.
The syntax as proposed (and implemented in Mozilla) has the output
expression before the loop and filter:

    [i.toString() for each (i in [1,2,3]) if (i != 2)]

The problem is that the variable i in the output expression is defined
by code to the right of it. Since people usually writes code left to
right, an editor with autocompletion is not able to provide type hints
like member proposals for i. This also limits what hints an editor can
give when writing an array literal. E.g. it cannot immediately flag an
undefined variable in the first expression in an array literal as a
possible typo, because we may be in the process of writing a
comprehension in which case the variable may be defined in the loop
which have not yet been typed. (Generally expressions in ECMAScript
never rely on definitions to the right of the expression -
comprehensions are the exception as far as I can tell.)

Traditional synax like:

   var x = [];
   for each (i in [1,2,3]) if (i != 2) x.push(i.toString());

or

   [1,2,3].filter(function(i) { return i != 2}).map(function(i){
return i.toString(); })

does not have the problem, since they read from left to right.

I wonder if it would make sense to turn array comprehension syntax
around so that they are written:

   [for each (i in [1,2,3]) if (i!=2) i.toString()]

This also makes it look more familiar since it is closer to the
traditional for-statement syntax. I realize this goes against the
tradition from most other languages which support list comprehensions.
e.g. Python.
I admit l like how it reads with the output expression first, but I
also think good tooling-support is pretty important for the success of
a language.

Regards
Olav Junker Kjær
(http://www.mascaraengine.com/)
_______________________________________________
es-discuss mailing list
es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss

Re: Array comprehension syntax

by Brendan Eich-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This sounds like a binary trade: follow Python and other precedent, or help autocompletion tools. I don't buy it, but it is hard to argue on these terms. Putting the comprehension expression on the right could help, but JS is dynamic: do you really know the type of i in more interesting cases than [i.toString() for each (i in [1,2,3]))]? That is a contrived example.

Real comprehensions are not so easy to analyze for likely autocompletions.

Real comprehensions are short enough the saved typing is not huge, in my experience.

/be



_______________________________________________
es-discuss mailing list
es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss

Re: Array comprehension syntax

by Jeff Walden-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 13.9.09 11:56 , Olav Kjær wrote:
> While experimenting with code-completion for ECMAScript I noticed a
> limitation with array comprehension syntax.
> The syntax as proposed (and implemented in Mozilla) has the output
> expression before the loop and filter:
>
>      [i.toString() for each (i in [1,2,3]) if (i != 2)]
>
> The problem is that the variable i in the output expression is defined
> by code to the right of it.

How do you address the following function?

> function foo()
> {
>   i = 3;
>   var i;
>   return i;
> }

Or this one?

> function foo()
> {
>   try
>   {
>     var i = 2;
>     throw i;
>   }
>   catch (e)
>   {
>     return i;
>   }
> }

Name before declaration is a problem already posed by the existing language syntax, isn't it?

Jeff
_______________________________________________
es-discuss mailing list
es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss