Reflect.getDefaultParameterValues

# Benjamin Gruenbaum (9 years ago)

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:

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?

# Claude Pache (9 years ago)

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.

# Thomas (9 years ago)

My initial thoughts:

Perhaps exposing parameter names isn't a good idea - perhaps just return an array? You could easily pass an array to .bind, .call, etc.

I'm not sure the use cases for such a feature make it worth adding - isn't this a problem better solved by documentation? But if it did return an array, it might be useful. What are the typical use cases for this in other languages?

Also, should this just be a Reflect method or should it also be available on Function.prototype?

Thomas

# Isiah Meadows (9 years ago)

I'm thinking of the same questions. I have yet to think of any use cases for this.

# Benjamin Gruenbaum (9 years ago)

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

match(
(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 default.
  • 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.

# Tab Atkins Jr. (9 years ago)

On Mon, Oct 5, 2015 at 9:00 AM, Benjamin Gruenbaum <benjamingr at gmail.com> wrote:

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

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

That's just syntax punning/abuse, not a real use-case. ^_^

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 default.

I'm confused. If the argument defaulted or not? If it's defaulted, omitting it isn't an error. If omitting it is an error, then there's no default, by definition.

  • Building an ORM that uses the default values in the database and returns correct error messages when an insert of a default value failed.

What about that requires the knowledge of the defaulted value, rather than just the argument value? I might see a use-case here for knowing whether or not the argument was defaulted, so you can output a more helpful error message (saying that the default is wrong, rather than saying the passed value is wrong), but knowing the default value doesn't help there (unless it's a unique sentinel value, which defeats the point of default values in the first place).