Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2019-07

pas2js: Generics for TJSArray?

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Simon Ameis

Hello all,

I'm currently defining some external classes for my JSON data payload which contains some array elements.

So far I got two solutions:

The first one uses "pascal arrays". This gives a lot of type safety but there are no array helpers yet which make requires me to use the pascal way of manipulating the array.

Tmyarraytype = array of TJSObject;
tmyclass = class external name 'abc' (TJSObject)
property myarray: Tmyarraytype read fmyarray;
end;

However there is TJSArray class which contains a lot of usefull methods which are available on all JavaScript arrays. Thus the second solution is:

tmyclass = class external name 'abc' (TJSObject)
property myarray: TJSArray read fmyarray;
end;

Then I have all the methods but no type safety at all and I need to do typecast for each array access.

I think this would be an ideal case for using generics. Are there any plans to implement TJSArray as generic as soon as generics are supported by pas2js?

Regards
Simon Ameis

Reply by email

Michael Van Canneyt
to Simon Ameis
Simon Ameis wrote 10 lines
Hello all, I'm currently defining some external classes for my JSON data payload which contains some array elements. So far I got two solutions: The first one uses "pascal arrays". This gives a lot of type safety but there are no array helpers yet which make requires me to use the pascal way of manipulating the array.

As far as I know, array helpers should work ?

What is missing on the arrays ? I think Copy, insert, Concat should all work. What else do you need ?

Show quoted text 18 lines
Tmyarraytype = array of TJSObject; tmyclass = class external name 'abc' (TJSObject) property myarray: Tmyarraytype read fmyarray; end; However there is TJSArray class which contains a lot of usefull methods which are available on all JavaScript arrays. Thus the second solution is: tmyclass = class external name 'abc' (TJSObject) property myarray: TJSArray read fmyarray; end; Then I have all the methods but no type safety at all and I need to do typecast for each array access. I think this would be an ideal case for using generics. Are there any plans to implement TJSArray as generic as soon as generics are supported by pas2js?

Yes. The default TJSArray will then just be a specialization using JSValue. Do not forget that a javascript array is not typed: it can contain any element.

Same for promise and some other things which will be reworked using generics (low-level buffers)

Michael.

Reply by email

Mattias Gaertner
to Michael Van Canneyt

This is the fpc list. It would be better to post on the pas2js mailing list.

Michael Van Canneyt wrote
[...] As far as I know, array helpers should work ?

Yes.

What is missing on the arrays ? I think Copy, insert, Concat should all work.

Yes.

[...]

Mattias

Reply by email