Benjamin Gruenbaum benjamingr at
Mon Oct 5 16:00:27 UTC 2015

Well, I've personally thought about building a small pattern matching
library using the syntax, but that's hardly a general use case:

(x = 1) => doFoo(...)
(x = {y : 3}) => doBar(...)

However, there are several use cases people mention. Here are the first
twofrom questions asking for this on StackOverflow in other languages:

 - Exposing functions in a module over a JSON API. If the caller omits
certain function arguments, return a specific error that names the specific
function argument that was omitted. If a client omits an argument, but
there's a default provided in the function signature, I want to use that
 - Building an ORM that uses the default values in the database and returns
correct error messages when an insert of a default value failed.

Looking for the functions that get this value in other languages at GitHub,
we can find quite a lot of usages for this too (I searched for
.DefaultValue in C# code) :

 - ORMs and data mappers.
 - SOAP calls definition.
 - A C# Lua interpreter uses it to convert calls.
 - RPC call definition.

So mostly - things that map the language to another language (ORMs and data
mappers) and things that serialize the language (RPC tools) seem to be the
primary users. Otherwise some code that converts one language to another
language also uses it and other similar tools.

Generally - I think a method to get a function's parameter names would be
desirable as well, especially since there already is a (pretty widely used)
way to do it that involves parsing the `.toString` of the function in a
nasty way.

This is not the *strongest *use case, but given that the commonly used
alternative is pretty terrible and doesn't work for a lot of cases (parsing
.toString) it would be desirable to at least be able to do this somehow.

On Mon, Oct 5, 2015 at 5:23 PM, Claude Pache <claude.pache at> wrote:

> Question: What are the use cases?
> An issue with that sort of reflection API, is that it exposes how the
> function is defined rather than how the function behaves. That makes
> refactoring more brittle.
> —Claude
> > Le 5 oct. 2015 à 16:04, Benjamin Gruenbaum <benjamingr at> a
> écrit :
> >
> > Hey, other languages with default parameter values like Python, C#, Ruby
> and PHP have a means to retrieve the default parameter values of a function.
> >
> > From what I understand (correct me if I'm wrong) - there is no way to
> get the default values of a parameter of a function in JavaScript. For
> example:
> >
> > ```js
> > function foo(x = 5){
> >
> > }
> > Reflect.getDefaultParameterValues(foo); // {x : 5}
> > ```
> >
> > This would be very nice to have in the language in my opinion.
> >
> > Now, doing this right sounds a bit challenging. since default parameters
> get their value on every invocation of the function, it would have to
> return getters (or functions) for values rather than values themselves.
> There is also the issue of how `this` binding is handled by the said
> getters.
> >
> > Is there interest in such an API? Is there any previous work or
> discussions about it?
> > _______________________________________________
> > es-discuss mailing list
> > es-discuss at
> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list