[Pas2js] Possible feature? keep the original JS overload behavior

silvioprog silvioprog at gmail.com
Fri Dec 21 17:35:11 CET 2018


Hello dudes.

Consider the following JS function:

function foo(a, b) {
  if (a && b)
    return a + b;
  if (a)
    return a;
  return "N/D";
}

it allows the following "overloading":

console.log(foo());
console.log(foo("abc"));
console.log(foo("abc", 123));

that prints:

N/D
abc
abc123

I know it is already possible to map functions like that "foo()" using the
"overload" keyword. However, it is very common to see JS libraries
declaring functions with a lot of parameters, making it very difficult and
verbose to do a wrapper in the Pas2JS-ish style.

So, I have a question. Consider the following code:

function myfoo(const a: JSValue = nil; const b: JSValue = nil): string;
external name 'foo';

begin
  console.log(myfoo);
  console.log(myfoo('abc'));
  console.log(myfoo('abc', 123));
end.

it will be transpiled as:

console.log(foo(null,null));
console.log(foo("abc",null));
console.log(foo("abc",123));

but, what do you think about to generating the following?:

console.log(foo());
console.log(foo("abc"));
console.log(foo("abc",123));

maybe by adding a new compiler option, something like:
'OverrideStyle=pascal|javascript', respectively something like: -JOs=pas
(or js).

I'm mapping a JS library that forced me to declare about fifteen overloaded
functions, and its entire API declares about twenty-nine functions, in such
situations, it would be nice to overload them like in JS, i.e., declaring
only one function already overloaded by itself.

best,

--
Silvio Clécio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20181221/658035ef/attachment.html>


More information about the Pas2js mailing list