Function Overloading or Pattern Matching of functions in Ecma

Isiah Meadows isiahmeadows at gmail.com
Wed Oct 17 08:08:17 UTC 2018


In general, you can do this via `arguments.length`, and if/once the pattern
matching proposal [1] hits stage 4, you could do this to emulate it very
closely:

```js
function foo(...args) {
    case (args) {
        when [] -> { /* zero args*/ },
        when [x] -> { /* one arg */ },
        // and so on...
    }
}
```

In general, that proposal would most interest you.

[1]:
https://github.com/tc39/proposal-pattern-matching

On Wed, Oct 17, 2018 at 01:24 bishwendu kundu <bishwenduk029 at gmail.com>
wrote:

> Hello All,
>
> I have been a great fan of the evolution of JavaScript in the recent past.
> The language is steadily closing the gap to ideal functional paradigm. One
> of the things that I have liked the most about languages like
> Erlang/Elixir/Haskell is their ability to define functions with same name
> and different airty. The decision of invoking the correct function-argument
> pattern is dependent on the invocation of the function. Implicit pattern
> matching in aforementioned languages helps avoid costly cognitive loads
> introduced by explicit if/else based pattern of logic flow branching.
>
> The greatest power of the pattern matching which amazed me was, dropping
> the whole of looping construct, altogether. Elixir/Erlang have no LOOPS.
> Stack based recursive calls combined with JavaScript's closures suddenly
> strikes me as a very powerful programming model that not only increases
> readability of code but also promotes immutability of the data structures
> in use. The loops would continue to exist but more modern codes can be
> written leveraging the recursive model of JavaScript.
>
> With de-structuring of arrays & objects already in place in JavaScript,
> pattern-matching of functions can fit right in perfectly, thereby taking
> JavaScript another step closer to ideal functional programming model.
>
> Looking forward to your response.
>
> Thanks & Regards,
> Bishwendu.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181017/d1440b83/attachment-0001.html>


More information about the es-discuss mailing list