[Pas2js] for .. in loop

Michael Van Canneyt michael at freepascal.org
Fri May 18 17:22:00 CEST 2018



On Fri, 18 May 2018, Ryan Joseph wrote:

>
>
>> On May 18, 2018, at 10:06 PM, Michael Van Canneyt <michael at freepascal.org> wrote:
>> 
>> 
>> Me neither. I don't understand why this compiles at all :)
>
> I didn’t know that feature exist. 
> Those are called object literals I think and are like hash tables in JS as far as I know.
>
>> 
>>> 
>>> 1) why are we talking about TJSObject? That’s an additional use for..in which is good to know but it’s not an array.
>>> 
>>> 2) Why doesn’t this work with JSArray like you would expect? I mean if a function returns a JSArray of integers for example can’t I use for..in and have the iterator be of type integer? That’s really the question I’m asking.
>> 
>> No. The compiler has no way of knowing what the TJSArray contains.
>> 
>> So in the best case you should be able to do
>> 
>> var
>>  o: TJSArray;
>>  JS: JSVAlue;
>> begin
>>  for JS in o do writeln(JS);
>> end.
>
> That’s what I wanted to do. It makes sense to use JSValue as the iterator and cast OR you should be able to cast the iterator like:

Mattias will need to shed some light on why one construct works, and the other not....

>
> var
> o: TJSArray;
> i: integer;
> begin
> for integer(i) in o do writeln(JS);

I don't think we'll allow this.

Michael.


More information about the Pas2js mailing list