Lazarus

Announcements => Free Pascal => Topic started by: MvC on January 16, 2018, 05:15:09 pm

Title: Pas2JS missing language features ?
Post by: MvC on January 16, 2018, 05:15:09 pm
Hello,

Pas2js is still missing some of the more "modern" language features.
Currently we're at Delphi 7 compatibility (more or less), minus interfaces.

We'd like to know what language features we should work on first, according to the users.
You can vote 3 times, so we can have an idea of 'ranking' of the features.

Please vote, tell us what you think!

Michael.



Title: Re: Pas2JS missing language features ?
Post by: Blaazen on January 16, 2018, 05:24:31 pm
There's no place for democracy in software develpment  ;D
Title: Re: Pas2JS missing language features ?
Post by: Sergionn on January 16, 2018, 05:43:36 pm
Please vote, tell us what you think!

For me anonymus functions would be preffer - then i will be ready to translate to pas2js my ui/autolayout/bindings&ect  framework!
Wait for implementation very mach!
And thanks to you for very important work, which SmartMobile team didn't do:( - they took the wrong way...
Title: Re: Pas2JS missing language features ?
Post by: mdbs99 on January 16, 2018, 05:45:22 pm
Well, to be more Delphi 7 compatible we need Interfaces for sure.
Title: Re: Pas2JS missing language features ?
Post by: Thaddy on January 16, 2018, 06:44:31 pm
Interfaces, type helpers, whatever, anything....
What lacks are two features, one of which I deem important:that is anonymous methods.
But that would be for FPC itself to have first.
After I saw an actual demonstration of what can be achieved with pas2js I have been playing with it all day.

Note that it may be of interest to explain why certain language features are not possible in pas2js.
Title: Re: Pas2JS missing language features ?
Post by: zburns on January 16, 2018, 07:18:34 pm
Thaddy - do you have a demonstration - I'm also trying to figure out where/what I can use pas2js for and always looking for ideas.

Zack
Title: Re: Pas2JS missing language features ?
Post by: dubst3pp4 on January 17, 2018, 08:45:49 am
I'm also interested in the use-case of PAS2JS. Why use Pascal instead of JavaScript (with its ecosystem)?

In the wiki I've read that it has support to generate nodejs modules. But does it allow to interact with existing modules? (the Haxe language for example does this with 'externs' - you describe the API to an external JavaScript library / module, so the compiler does know about the external structure)

For me the biggest point would be the possibility to write the classes of my application logic only once: native Free Pascal classes which can be used for the server part of a web-application and to-JavaScript-compiled classes that I can use client-side in my own JavaScript code.

Sorry if my questions are too naive, but I had no time to seriously try PAS2JS ;-) ...btw, I would prefer a better name for the project! :-)
Title: Re: Pas2JS missing language features ?
Post by: Thaddy on January 17, 2018, 09:23:59 am
I'm also interested in the use-case of PAS2JS. Why use Pascal instead of JavaScript (with its ecosystem)?
Mainly because Pascal is strongly typed as opposed to JavaScript: that means that the transpiler can generate more accurate code, less prone to errors, even if it outputs eventually JavaScript.
So you have the advantages of a strongly typed language while generating code for a weakly typed language. That includes e.g. debugging, type safety, etc. Pascal is often less verbose too.
Note that pas2js also supports inserting Javascript directly if you need it, through asm blocks, where the asm code is actual javascript.

Simply install the packages in Lazarus and you are good to go.

In reference to other questions: I will add some node.js examples to the wiki and a simple database application with client-side forms. Maybe Michael and Mattias can add even more: they are the experts.
I need to clean up the code so I can't do it today.
Title: Re: Pas2JS missing language features ?
Post by: dubst3pp4 on January 17, 2018, 09:31:18 am
Thanks Thaddy, more examples would be really great!

A little offtopic, but:
I was playing with Haxe lately - and although it also has strict type checks during compilation, the generated JavaScript code was not safe! I was a little bit disappointed by that fact, you could, for example, declare an object property as string, but the generated JS code did not declared that property with the help of Object.defineProperty and a related setter, which checks the type at runtime. Thus you can assign every type to that property of an instance of that type! So you can write type-safe-code, as long as you write the whole application in Haxe (because all the code gets compiled with type checking), but as soon as you write modules, which you want to use in other JavaScript code, the compiled code is even worse than hand-written code. Adding JavaScript type checking via setters for properties would be a real benefit of using PAS2JS!
Title: Re: Pas2JS missing language features ?
Post by: PascalDragon on January 17, 2018, 09:32:50 am
What lacks are two features, one of which I deem important:that is anonymous methods.
But that would be for FPC itself to have first.

There isn't any reason why Pas2JS couldn't have anonymous functions before FPC.
Title: Re: Pas2JS missing language features ?
Post by: Thaddy on January 17, 2018, 09:59:08 am
There isn't any reason why Pas2JS couldn't have anonymous functions before FPC.
Well, no maybe, but it would confuse me (just like writing Delphi code using it confuses me when I switch back to FPC.... :o %))

Maybe, because pas2js goal is afaik to be a large subset (meaning all applicable features) of FPC, not a superset  :D
Anyway..
Title: Re: Pas2JS missing language features ?
Post by: SymbolicFrank on January 17, 2018, 01:52:38 pm
What would an anonymous function (method?) allow that cannot be done with a procedural type?
Title: Re: Pas2JS missing language features ?
Post by: mattias on January 17, 2018, 02:23:36 pm
Note that pas2js supports closures, so for example instead of using an anymous function you can use a sub function:

Delphi:
Code: Pascal  [Select]
  1. function MakeAdder(y: Integer): TFuncOfInt;
  2. begin
  3.   Result := function(x: Integer)
  4.     begin
  5.       Result := x + y;
  6.     end;
  7. end;
  8.  

Pas2js:
Code: Pascal  [Select]
  1. function MakeAdder(y: Integer): TFuncOfInt;
  2.   function Adder(x: Integer): integer;
  3.   begin
  4.     Result := x + y;
  5.   end;
  6. begin
  7.   Result:=@Adder;
  8. end;
  9.  
Title: Re: Pas2JS missing language features ?
Post by: mattias on January 17, 2018, 02:38:57 pm
In the wiki I've read that it has support to generate nodejs modules. But does it allow to interact with existing modules? (the Haxe language for example does this with 'externs' - you describe the API to an external JavaScript library / module, so the compiler does know about the external structure)

Same in pas2js. It supports external classes, functions and variables. See unit js for examples.
You can even descend a new Pascal class from an external JS class.

And you can use asm-blocks to insert JS directly. But the external classes are so powerful, that even the RTL hardly use asm blocks.
Title: Re: Pas2JS missing language features ?
Post by: dubst3pp4 on January 17, 2018, 02:52:40 pm
@mattias
Can I use this also in native Free Pascal code? I've tried your interesting closure example, but in my version the value of y is initialized to the same value as x at every call:
Code: Pascal  [Select]
  1. program example;
  2. {$mode objfpc}{$H+}
  3. {$modeswitch nestedprocvars}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. type
  9.   TFuncOfInt = function(x: integer): integer is nested;
  10.  
  11. function MakeAdder(y: integer): TFuncOfInt;
  12.   function Adder(x: integer): integer;
  13.   begin
  14.     WriteLn('adding ' + IntToStr(y) + ' to ' + IntToStr(x));
  15.     Result := x + y;
  16.   end;
  17. begin
  18.   Result := @Adder;
  19. end;
  20.  
  21. var
  22.   SomeResult: integer;
  23.  
  24. begin
  25.   SomeResult := MakeAdder(2)(5);
  26.   WriteLn(IntToStr(SomeResult));
  27. end.
  28.  

Result:

adding 5 to 5
10


Sorry if this is way too offtopic, but how do you archieve y to stay in the scope (like I would expect from JavaScript)? Or does it only returns the expected result when compiling with PAS2JS?
Title: Re: Pas2JS missing language features ?
Post by: dubst3pp4 on January 17, 2018, 02:56:49 pm
Same in pas2js. It supports external classes, functions and variables. See unit js for examples.
You can even descend a new Pascal class from an external JS class.

And you can use asm-blocks to insert JS directly. But the external classes are so powerful, that even the RTL hardly use asm blocks.
Wow, good to know! So it seems it is now time to have a deep look into PAS2JS :-) Thanks for the hint!

Edit: found the section in the wiki: http://wiki.freepascal.org/pas2js#Importing_Javascript_classes Sorry for not reading carefully enough!
Title: Re: Pas2JS missing language features ?
Post by: avra on January 17, 2018, 10:19:28 pm
I also miss "Type alias" very much. Too bad it's not in the list. I use it a lot.
Title: Re: Pas2JS missing language features ?
Post by: BeniBela on January 18, 2018, 12:20:27 am
Pointers would be nice :)
Title: Re: Pas2JS missing language features ?
Post by: PascalDragon on January 20, 2018, 02:00:42 pm
@mattias
Can I use this also in native Free Pascal code? I've tried your interesting closure example, but in my version the value of y is initialized to the same value as x at every call:

No, this doesn't work in Free Pascal (or at least only by chance), because the value of y is loaded from the stack or a register depending on the calling convention. For this to work closure support is needed (which is already a Work In Progress) as that stores the value of y at a temporary, hidden class instance that is destroyed once the procedure reference moves out of scope again.
Title: Re: Pas2JS missing language features ?
Post by: PascalDragon on January 20, 2018, 02:02:23 pm
Note that pas2js supports closures, so for example instead of using an anymous function you can use a sub function:

Delphi:
Code: Pascal  [Select]
  1. function MakeAdder(y: Integer): TFuncOfInt;
  2. begin
  3.   Result := function(x: Integer)
  4.     begin
  5.       Result := x + y;
  6.     end;
  7. end;
  8.  

Pas2js:
Code: Pascal  [Select]
  1. function MakeAdder(y: Integer): TFuncOfInt;
  2.   function Adder(x: Integer): integer;
  3.   begin
  4.     Result := x + y;
  5.   end;
  6. begin
  7.   Result:=@Adder;
  8. end;
  9.  

Please note that once anonymous functions/closures are supported in FPC I plan to extend it so that local functions/procedures can also be passed to "reference to procedure/function" variables as well. This way those that don't want to use anonymous functions due to their syntax can still enjoy their capabilities.
Title: Re: Pas2JS missing language features ?
Post by: dubst3pp4 on January 22, 2018, 11:37:15 am
Thanks @PascalDragon, as a developer who's handling closures in JavaScript every day, my expectations were misleaded here, but what you wrote makes sense to me! :-)
Title: Re: Pas2JS missing language features ?
Post by: warleyalex on January 30, 2018, 10:24:27 pm
I just can not just believe, anonymous functions is not at top of the list.
It's so boring writing sub functions :( to do simple things.
Title: Re: Pas2JS missing language features ?
Post by: Sergionn on February 10, 2018, 11:27:19 am
I just can not just believe, anonymous functions is not at top of the list.
It's so boring writing sub functions :( to do simple things.
Can't add something better to say!