[Pas2js] Possible feature? keep the original JS overload behavior
Michael Van Canneyt
michael at freepascal.org
Fri Dec 21 17:54:35 CET 2018
On Fri, 21 Dec 2018, silvioprog wrote:
> 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';
Why not simply declare
function myfoo: string; varargs;external name 'foo';
Because if you declare all arguments as jsvalue, it makes no sense to create all
overloads, there is no type checking.
What we could do is extend varargs to accept a count:
function myfoo: string; varargs 2;external name 'foo';
So you cannot pass more arguments than the original function supports, the
compiler could check the number of var args passed.
Michael.
More information about the Pas2js
mailing list