Re: Reflect.getDefaultParameterValues

2015-10-06 Thread Tab Atkins Jr.
On Mon, Oct 5, 2015 at 9:00 AM, Benjamin Gruenbaum  wrote:
> Well, I've personally thought about building a small pattern matching
> library using the syntax, but that's hardly a general use case:
>
> ```js
> 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).

~TJ
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Reflect.getDefaultParameterValues

2015-10-05 Thread Isiah Meadows
I'm thinking of the same questions. I have yet to think of any use cases
for this.

On Mon, Oct 5, 2015, 10:32 Thomas <thomasjamesfos...@bigpond.com> wrote:

> 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
>
> > On 6 Oct 2015, at 1:04 AM, Benjamin Gruenbaum <benjami...@gmail.com>
> wrote:
> >
> > 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@mozilla.org
> > https://mail.mozilla.org/listinfo/es-discuss
> ___
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Reflect.getDefaultParameterValues

2015-10-05 Thread Claude Pache
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 <benjami...@gmail.com> 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@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Reflect.getDefaultParameterValues

2015-10-05 Thread Benjamin Gruenbaum
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@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Reflect.getDefaultParameterValues

2015-10-05 Thread Thomas
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

> On 6 Oct 2015, at 1:04 AM, Benjamin Gruenbaum <benjami...@gmail.com> wrote:
> 
> 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@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Reflect.getDefaultParameterValues

2015-10-05 Thread Benjamin Gruenbaum
Well, I've personally thought about building a small pattern matching
library using the syntax, but that's hardly a general use case:

```js
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.


On Mon, Oct 5, 2015 at 5:23 PM, Claude Pache <claude.pa...@gmail.com> 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 <benjami...@gmail.com> 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@mozilla.org
> > https://mail.mozilla.org/listinfo/es-discuss
>
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss