[Pas2js] Question regarding the future type helper feature
silvioprog
silvioprog at gmail.com
Fri Jan 4 18:55:17 CET 2019
Hello,
Since this feature is planned to be implemented, I have a question about
the possibility to declare multiple helpers for the same type. I suspect
you will implement it extending the object prototype, but it seems multiple
type helpers are not allowed in Pascal code. For example, consider the
following code:
uses SysUtils;
begin
ShowMessage(Integer(123).ToString);
end.
it compiles and runs fine, but, if you declare another helper:
uses SysUtils;
type
TMyIntegerHelper = record helper for Integer
function ToString2: string;
end;
function TMyIntegerHelper.ToString2: string;
begin
Result := Concat('my', IntToStr(Self));
end;
begin
ShowMessage(Concat(Integer(123).ToString, ';', Integer(123).ToString2));
end.
it can't compile:
Unit1.pas: E2003 Undeclared identifier: 'ToString'
however, in the Pas2JS it should be supported, and the result would be:
123;my123
A small example in JS extending the Date prototype, very close to our type
helper, but allowing multiple declarations:
Date.prototype.toMyString = (function() {
return "My " + this.toString();
});
var event = new Date("August 19, 1975 23:15:30");
console.log(event.toString(), ";", event.toMyString());
it prints:
Tue Aug 19 1975 23:15:30 GMT-0300 ; My Tue Aug 19 1975 23:15:30 GMT-0300
Many JS plugins works extending prototype, and we can use multiple plugins
simultaneously, so it should be supported in Pas2JS mode. :-)
Best,
--
Silvio Clécio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20190104/398a0a06/attachment.html>
More information about the Pas2js
mailing list