Re: Re: What do you think about a C# 6 like nameof() expression for

classic Classic list List threaded Threaded
52 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

Stas Berkov

Can we revisit this issue?


In C# there is `nameof`, in Swift you can do the same by calling

```

let keyPath = \Person.mother.firstName

NSPredicate(format: "%K == %@", keyPath, "Andrew")

```

Let's introduce `nameof` in ES, please. 


Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 ) 

This feature is eagarly being asked by TypeScript community.


I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.


Minification.

1. If your code to be minimized be prepared that variable names will also change.

2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.

 

What if user already has `nameof` function.

1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.

2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

Augusto Moura
Can you list the benefits of having this operators? Maybe with example use cases

If I understand it correctly, the operator fits better in compiled
(and typed) languages, most of the use cases don't apply to dynamic
Javascript
The only legit use case I can think of is helping refactor tools to
rename properties (but even mismatch errors between strings and
properties names can be caught in compile time using modern
Typescript)

Em sex, 14 de jun de 2019 às 10:05, Stas Berkov
<[hidden email]> escreveu:

>
> Can we revisit this issue?
>
>
> In C# there is `nameof`, in Swift you can do the same by calling
>
> ```
>
> let keyPath = \Person.mother.firstName
>
> NSPredicate(format: "%K == %@", keyPath, "Andrew")
>
> ```
>
> Let's introduce `nameof` in ES, please.
>
>
> Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 )
>
> This feature is eagarly being asked by TypeScript community.
>
>
> I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.
>
>
> Minification.
>
> 1. If your code to be minimized be prepared that variable names will also change.
>
> 2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.
>
>
>
> What if user already has `nameof` function.
>
> 1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.
>
> 2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Atenciosamente,

Augusto Borges de Moura
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

guest271314
In reply to this post by Stas Berkov
Have not tried TypeScript. What are the use cases for JavaScript where TypeScript is not used? Does ```nameof()``` check if an object has a specific property name defined (```"MutationObserver" in window```; ```window.MutationObserver```; ```"document" in globalThis```)?

On Fri, Jun 14, 2019 at 1:05 PM Stas Berkov <[hidden email]> wrote:

Can we revisit this issue?


In C# there is `nameof`, in Swift you can do the same by calling

```

let keyPath = \Person.mother.firstName

NSPredicate(format: "%K == %@", keyPath, "Andrew")

```

Let's introduce `nameof` in ES, please. 


Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 ) 

This feature is eagarly being asked by TypeScript community.


I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.


Minification.

1. If your code to be minimized be prepared that variable names will also change.

2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.

 

What if user already has `nameof` function.

1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.

2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

Ron Buckton
The 'nameof' operator provides the string name of a static symbol (in compiler/linker terms, not an ECMAScript 'Symbol'). This is often useful for diagnostics (logging and errors), and gives developers a way to reduce repetition. It is also extremely helpful in editors that support symbolic "rename" refactoring.

While ES functions have a 'name' property, one of the main use cases is to get a string representation of the name of a thing that isn't itself reified as an object, such as a variable, parameter, or module namespace:

```
import * as ns from "foo";

nameof ns; // "ns"

let fn = function g() {};
fn.name; // "g"
nameof fn; // "fn"
```

Here is an example of two common use cases:

```
class C {
  compare(other) {
    if (!(other instanceof C)) {
      throw new TypeError(`Invalid argument: ${nameof other} `);
    }

  set prop(value) {
    this._prop = value;
    this.emit("propertychanged", nameof prop);
  }
}
```

It's not that a language like TypeScript would need this, but rather that users of an editor like VS Code or WebStorm that have a JavaScript language service would benefit from it.

Ron

From: es-discuss <[hidden email]> on behalf of guest271314 <[hidden email]>
Sent: Friday, June 14, 2019 9:05:55 AM
To: Stas Berkov
Cc: [hidden email]
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for
 
Have not tried TypeScript. What are the use cases for JavaScript where TypeScript is not used? Does ```nameof()``` check if an object has a specific property name defined (```"MutationObserver" in window```; ```window.MutationObserver```; ```"document" in globalThis```)?

On Fri, Jun 14, 2019 at 1:05 PM Stas Berkov <[hidden email]> wrote:

Can we revisit this issue?


In C# there is `nameof`, in Swift you can do the same by calling

```

let keyPath = \Person.mother.firstName

NSPredicate(format: "%K == %@", keyPath, "Andrew")

```

Let's introduce `nameof` in ES, please. 


Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 ) 

This feature is eagarly being asked by TypeScript community.


I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.


Minification.

1. If your code to be minimized be prepared that variable names will also change.

2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.

 

What if user already has `nameof` function.

1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.

2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

guest271314
Not following the module example. Would not ```ns``` be an instance of ```Module``` where ```* as ns``` is used?

For the ```class``` example would ```nameof``` be equivalent to ```Object.getOwnPropertyDescriptors(other).name.value```?

It's not that a language like TypeScript would need this, but rather that users of an editor like VS Code or WebStorm that have a JavaScript language service would benefit from it.

Is the primary use case a non-simple text editor; e.g., _not_ gedit (GUI) or pico or nano (CLI), where the user is depending on the program

for diagnostics (logging and errors)

within a text editor _before_ actually running the code in a given environment - not to log errors during code execution? 

On Fri, Jun 14, 2019 at 4:29 PM Ron Buckton <[hidden email]> wrote:
The 'nameof' operator provides the string name of a static symbol (in compiler/linker terms, not an ECMAScript 'Symbol'). This is often useful for diagnostics (logging and errors), and gives developers a way to reduce repetition. It is also extremely helpful in editors that support symbolic "rename" refactoring.

While ES functions have a 'name' property, one of the main use cases is to get a string representation of the name of a thing that isn't itself reified as an object, such as a variable, parameter, or module namespace:

```
import * as ns from "foo";

nameof ns; // "ns"

let fn = function g() {};
fn.name; // "g"
nameof fn; // "fn"
```

Here is an example of two common use cases:

```
class C {
  compare(other) {
    if (!(other instanceof C)) {
      throw new TypeError(`Invalid argument: ${nameof other} `);
    }

  set prop(value) {
    this._prop = value;
    this.emit("propertychanged", nameof prop);
  }
}
```

It's not that a language like TypeScript would need this, but rather that users of an editor like VS Code or WebStorm that have a JavaScript language service would benefit from it.

Ron

From: es-discuss <[hidden email]> on behalf of guest271314 <[hidden email]>
Sent: Friday, June 14, 2019 9:05:55 AM
To: Stas Berkov
Cc: [hidden email]
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for
 
Have not tried TypeScript. What are the use cases for JavaScript where TypeScript is not used? Does ```nameof()``` check if an object has a specific property name defined (```"MutationObserver" in window```; ```window.MutationObserver```; ```"document" in globalThis```)?

On Fri, Jun 14, 2019 at 1:05 PM Stas Berkov <[hidden email]> wrote:

Can we revisit this issue?


In C# there is `nameof`, in Swift you can do the same by calling

```

let keyPath = \Person.mother.firstName

NSPredicate(format: "%K == %@", keyPath, "Andrew")

```

Let's introduce `nameof` in ES, please. 


Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 ) 

This feature is eagarly being asked by TypeScript community.


I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.


Minification.

1. If your code to be minimized be prepared that variable names will also change.

2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.

 

What if user already has `nameof` function.

1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.

2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

Stas Berkov
In reply to this post by Augusto Moura
ES can befit from `nameof` feature the same way as TS. There is no TS specific in it.
It was ask to introduce in TS as a workaround since TS is considered as extention of ES.

Case 1. Function guard.
```
function func1(param1, param2, param3, userName, param4, param5) {
   if (userName == undefined) {
       throw new ArgumentNullError(nameof userName); // `ArgumentNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".
   }
}
```

Case 2. Access extended information an object property.
Assume a function
```
function protoPropertyIsSet(message, property) {
    return message != null && message.hasOwnProperty(property);
}
```
Then in code you use it as `if (protoPropertyIsSet(msg, "expiration_utc_time")) {... }`.
Having `nameof` would allow you to do that `if (protoPropertyIsSet(msg, nameof msg.expiration_utc_time)) {... }`.
You get more robust code.

On Fri, Jun 14, 2019 at 5:46 PM Augusto Moura <[hidden email]> wrote:
Can you list the benefits of having this operators? Maybe with example use cases

If I understand it correctly, the operator fits better in compiled
(and typed) languages, most of the use cases don't apply to dynamic
Javascript
The only legit use case I can think of is helping refactor tools to
rename properties (but even mismatch errors between strings and
properties names can be caught in compile time using modern
Typescript)

Em sex, 14 de jun de 2019 às 10:05, Stas Berkov
<[hidden email]> escreveu:
>
> Can we revisit this issue?
>
>
> In C# there is `nameof`, in Swift you can do the same by calling
>
> ```
>
> let keyPath = \Person.mother.firstName
>
> NSPredicate(format: "%K == %@", keyPath, "Andrew")
>
> ```
>
> Let's introduce `nameof` in ES, please.
>
>
> Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 )
>
> This feature is eagarly being asked by TypeScript community.
>
>
> I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.
>
>
> Minification.
>
> 1. If your code to be minimized be prepared that variable names will also change.
>
> 2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.
>
>
>
> What if user already has `nameof` function.
>
> 1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.
>
> 2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Atenciosamente,

Augusto Borges de Moura

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

guest271314
Is Case 1 equivalent to a briefer version of

```
   if (userName == undefined) {
       throw new Error(`Argument cannot be null: ${Object.keys({userName})[0]}`);
   }
```


If not, how is ```nameof``` different?

What is the difference between the use of ```message.hasOwnProperty(property)``` and ```nameof msg.expiration_utc_time```?

You get more robust code.

How is "robust" objectively determined?




On Fri, Jun 14, 2019 at 5:21 PM Stas Berkov <[hidden email]> wrote:
ES can befit from `nameof` feature the same way as TS. There is no TS specific in it.
It was ask to introduce in TS as a workaround since TS is considered as extention of ES.

Case 1. Function guard.
```
function func1(param1, param2, param3, userName, param4, param5) {
   if (userName == undefined) {
       throw new ArgumentNullError(nameof userName); // `ArgumentNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".
   }
}
```

Case 2. Access extended information an object property.
Assume a function
```
function protoPropertyIsSet(message, property) {
    return message != null && message.hasOwnProperty(property);
}
```
Then in code you use it as `if (protoPropertyIsSet(msg, "expiration_utc_time")) {... }`.
Having `nameof` would allow you to do that `if (protoPropertyIsSet(msg, nameof msg.expiration_utc_time)) {... }`.
You get more robust code.

On Fri, Jun 14, 2019 at 5:46 PM Augusto Moura <[hidden email]> wrote:
Can you list the benefits of having this operators? Maybe with example use cases

If I understand it correctly, the operator fits better in compiled
(and typed) languages, most of the use cases don't apply to dynamic
Javascript
The only legit use case I can think of is helping refactor tools to
rename properties (but even mismatch errors between strings and
properties names can be caught in compile time using modern
Typescript)

Em sex, 14 de jun de 2019 às 10:05, Stas Berkov
<[hidden email]> escreveu:
>
> Can we revisit this issue?
>
>
> In C# there is `nameof`, in Swift you can do the same by calling
>
> ```
>
> let keyPath = \Person.mother.firstName
>
> NSPredicate(format: "%K == %@", keyPath, "Andrew")
>
> ```
>
> Let's introduce `nameof` in ES, please.
>
>
> Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 )
>
> This feature is eagarly being asked by TypeScript community.
>
>
> I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.
>
>
> Minification.
>
> 1. If your code to be minimized be prepared that variable names will also change.
>
> 2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.
>
>
>
> What if user already has `nameof` function.
>
> 1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.
>
> 2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Atenciosamente,

Augusto Borges de Moura
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

Stas Berkov
Less fragile. Less mess. You can rename field/property without fear you break something (using IDE refactoring tools).
With high probablity you will break something when you refactor and have fields hardcoded as strings.
Someone can object that you can rename strings as well.
Issue here that you can ocassionally change non-related strings that should not be changed even they match or have matching substring.

On Fri, Jun 14, 2019 at 9:38 PM guest271314 <[hidden email]> wrote:
Is Case 1 equivalent to a briefer version of

```
   if (userName == undefined) {
       throw new Error(`Argument cannot be null: ${Object.keys({userName})[0]}`);
   }
```


If not, how is ```nameof``` different?

What is the difference between the use of ```message.hasOwnProperty(property)``` and ```nameof msg.expiration_utc_time```?

You get more robust code.

How is "robust" objectively determined?




On Fri, Jun 14, 2019 at 5:21 PM Stas Berkov <[hidden email]> wrote:
ES can befit from `nameof` feature the same way as TS. There is no TS specific in it.
It was ask to introduce in TS as a workaround since TS is considered as extention of ES.

Case 1. Function guard.
```
function func1(param1, param2, param3, userName, param4, param5) {
   if (userName == undefined) {
       throw new ArgumentNullError(nameof userName); // `ArgumentNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".
   }
}
```

Case 2. Access extended information an object property.
Assume a function
```
function protoPropertyIsSet(message, property) {
    return message != null && message.hasOwnProperty(property);
}
```
Then in code you use it as `if (protoPropertyIsSet(msg, "expiration_utc_time")) {... }`.
Having `nameof` would allow you to do that `if (protoPropertyIsSet(msg, nameof msg.expiration_utc_time)) {... }`.
You get more robust code.

On Fri, Jun 14, 2019 at 5:46 PM Augusto Moura <[hidden email]> wrote:
Can you list the benefits of having this operators? Maybe with example use cases

If I understand it correctly, the operator fits better in compiled
(and typed) languages, most of the use cases don't apply to dynamic
Javascript
The only legit use case I can think of is helping refactor tools to
rename properties (but even mismatch errors between strings and
properties names can be caught in compile time using modern
Typescript)

Em sex, 14 de jun de 2019 às 10:05, Stas Berkov
<[hidden email]> escreveu:
>
> Can we revisit this issue?
>
>
> In C# there is `nameof`, in Swift you can do the same by calling
>
> ```
>
> let keyPath = \Person.mother.firstName
>
> NSPredicate(format: "%K == %@", keyPath, "Andrew")
>
> ```
>
> Let's introduce `nameof` in ES, please.
>
>
> Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 )
>
> This feature is eagarly being asked by TypeScript community.
>
>
> I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.
>
>
> Minification.
>
> 1. If your code to be minimized be prepared that variable names will also change.
>
> 2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.
>
>
>
> What if user already has `nameof` function.
>
> 1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.
>
> 2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Atenciosamente,

Augusto Borges de Moura
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

Stas Berkov
> Is Case 1 equivalent to a briefer version of
> ```
   if (userName == undefined) {
       throw new Error(`Argument cannot be null: ${Object.keys({userName})[0]}`);
   }
```
Less readable but in this simple case might work.
What if we do the following:
Case 1. Function guard.
```
function func1(options) {
...
   if (options.userName == undefined) {
       throw new ParamNullError(nameof options.userName); // `ParamNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".
   }
}
```

Case 2. Accessing property extended info
e.g.
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
```
vs
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, nameof object1.property1);  
```
2nd variant (proposed) has more chances not to be broken during refactoring (robustness).

On Fri, Jun 14, 2019 at 9:48 PM Stas Berkov <[hidden email]> wrote:
Less fragile. Less mess. You can rename field/property without fear you break something (using IDE refactoring tools).
With high probablity you will break something when you refactor and have fields hardcoded as strings.
Someone can object that you can rename strings as well.
Issue here that you can ocassionally change non-related strings that should not be changed even they match or have matching substring.

On Fri, Jun 14, 2019 at 9:38 PM guest271314 <[hidden email]> wrote:
Is Case 1 equivalent to a briefer version of

```
   if (userName == undefined) {
       throw new Error(`Argument cannot be null: ${Object.keys({userName})[0]}`);
   }
```


If not, how is ```nameof``` different?

What is the difference between the use of ```message.hasOwnProperty(property)``` and ```nameof msg.expiration_utc_time```?

You get more robust code.

How is "robust" objectively determined?




On Fri, Jun 14, 2019 at 5:21 PM Stas Berkov <[hidden email]> wrote:
ES can befit from `nameof` feature the same way as TS. There is no TS specific in it.
It was ask to introduce in TS as a workaround since TS is considered as extention of ES.

Case 1. Function guard.
```
function func1(param1, param2, param3, userName, param4, param5) {
   if (userName == undefined) {
       throw new ArgumentNullError(nameof userName); // `ArgumentNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".
   }
}
```

Case 2. Access extended information an object property.
Assume a function
```
function protoPropertyIsSet(message, property) {
    return message != null && message.hasOwnProperty(property);
}
```
Then in code you use it as `if (protoPropertyIsSet(msg, "expiration_utc_time")) {... }`.
Having `nameof` would allow you to do that `if (protoPropertyIsSet(msg, nameof msg.expiration_utc_time)) {... }`.
You get more robust code.

On Fri, Jun 14, 2019 at 5:46 PM Augusto Moura <[hidden email]> wrote:
Can you list the benefits of having this operators? Maybe with example use cases

If I understand it correctly, the operator fits better in compiled
(and typed) languages, most of the use cases don't apply to dynamic
Javascript
The only legit use case I can think of is helping refactor tools to
rename properties (but even mismatch errors between strings and
properties names can be caught in compile time using modern
Typescript)

Em sex, 14 de jun de 2019 às 10:05, Stas Berkov
<[hidden email]> escreveu:
>
> Can we revisit this issue?
>
>
> In C# there is `nameof`, in Swift you can do the same by calling
>
> ```
>
> let keyPath = \Person.mother.firstName
>
> NSPredicate(format: "%K == %@", keyPath, "Andrew")
>
> ```
>
> Let's introduce `nameof` in ES, please.
>
>
> Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 )
>
> This feature is eagarly being asked by TypeScript community.
>
>
> I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.
>
>
> Minification.
>
> 1. If your code to be minimized be prepared that variable names will also change.
>
> 2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.
>
>
>
> What if user already has `nameof` function.
>
> 1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.
>
> 2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Atenciosamente,

Augusto Borges de Moura
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

guest271314
In reply to this post by Stas Berkov
Terms such as "more robust", "Less fragile." , "Less mess." are subjective. "Fragile" how? What is meant by "mess"? 

If the proposal is that ```nameof``` is briefer than using shorthand property names or computed property names to get an identifier as a string, then ```nameof``` would be less code.

> You can rename field/property without fear you break something (using IDE refactoring tools).

The use case appears to be within the context of an IDE?

For users that roll their own code by hand at a text editor and do not use an IDE for composing code (or relying on an IDE for logging errors), am not able to discern how ```nameof``` is different than using ```Object.getOwnPropertyDescriptors(other).name.value``` or ```Object.keys({userName})[0]```. The actual code will need to be tested in different browsers anyway before deployment to get the actual results.

If a user has decided they _have_ to use some form of a dynamic "IDE" to compose code and log errors - while they are writing the code, they can open DevTools at Chromium or Chrome then select ```Sources``` => ```Snippets``` => ```New snippet```, where

```
const n = 123;
const n = 456;
```

will highlight the line where the error occurs - during writing the code - the as a red circle with a transparent "x" in the middle

```Uncaught SyntaxError: Identifier 'n' has already been declared````

To run the code press ```ctrl+enter```.




On Fri, Jun 14, 2019 at 5:48 PM Stas Berkov <[hidden email]> wrote:
Less fragile. Less mess. You can rename field/property without fear you break something (using IDE refactoring tools).
With high probablity you will break something when you refactor and have fields hardcoded as strings.
Someone can object that you can rename strings as well.
Issue here that you can ocassionally change non-related strings that should not be changed even they match or have matching substring.

On Fri, Jun 14, 2019 at 9:38 PM guest271314 <[hidden email]> wrote:
Is Case 1 equivalent to a briefer version of

```
   if (userName == undefined) {
       throw new Error(`Argument cannot be null: ${Object.keys({userName})[0]}`);
   }
```


If not, how is ```nameof``` different?

What is the difference between the use of ```message.hasOwnProperty(property)``` and ```nameof msg.expiration_utc_time```?

You get more robust code.

How is "robust" objectively determined?




On Fri, Jun 14, 2019 at 5:21 PM Stas Berkov <[hidden email]> wrote:
ES can befit from `nameof` feature the same way as TS. There is no TS specific in it.
It was ask to introduce in TS as a workaround since TS is considered as extention of ES.

Case 1. Function guard.
```
function func1(param1, param2, param3, userName, param4, param5) {
   if (userName == undefined) {
       throw new ArgumentNullError(nameof userName); // `ArgumentNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".
   }
}
```

Case 2. Access extended information an object property.
Assume a function
```
function protoPropertyIsSet(message, property) {
    return message != null && message.hasOwnProperty(property);
}
```
Then in code you use it as `if (protoPropertyIsSet(msg, "expiration_utc_time")) {... }`.
Having `nameof` would allow you to do that `if (protoPropertyIsSet(msg, nameof msg.expiration_utc_time)) {... }`.
You get more robust code.

On Fri, Jun 14, 2019 at 5:46 PM Augusto Moura <[hidden email]> wrote:
Can you list the benefits of having this operators? Maybe with example use cases

If I understand it correctly, the operator fits better in compiled
(and typed) languages, most of the use cases don't apply to dynamic
Javascript
The only legit use case I can think of is helping refactor tools to
rename properties (but even mismatch errors between strings and
properties names can be caught in compile time using modern
Typescript)

Em sex, 14 de jun de 2019 às 10:05, Stas Berkov
<[hidden email]> escreveu:
>
> Can we revisit this issue?
>
>
> In C# there is `nameof`, in Swift you can do the same by calling
>
> ```
>
> let keyPath = \Person.mother.firstName
>
> NSPredicate(format: "%K == %@", keyPath, "Andrew")
>
> ```
>
> Let's introduce `nameof` in ES, please.
>
>
> Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 )
>
> This feature is eagarly being asked by TypeScript community.
>
>
> I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.
>
>
> Minification.
>
> 1. If your code to be minimized be prepared that variable names will also change.
>
> 2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.
>
>
>
> What if user already has `nameof` function.
>
> 1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.
>
> 2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Atenciosamente,

Augusto Borges de Moura
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Re: What do you think about a C# 6 like nameof() expression for

Ron Buckton
In reply to this post by guest271314

A module namespace isn’t an instance of `Module`, it’s a module namespace spec object. If it has a `name` member, that is because the module you imported has an exported binding called `name`. But what kind of thing it is isn’t what matters.

 

`nameof` just returns a string representation of the static symbolic name of its operand. `nameof` would be used at runtime (i.e. during code execution), but provides the ability for advanced editors to include it in a “rename” refactoring.

 

The operand merely needs to be an identifier that exists within the current lexical scope. `nameof` would not observe TDZ and would not evaluate any expression. The ECMAScript static semantics would merely: 1) check that the identifier provided to `nameof` is the name of something in the current lexical scope, and 2) replace the expression with a string literal representing the name.

 

If `nameof foo.bar` were allowed `nameof` would not actually evaluate `foo.bar`, but merely result in `”bar”`. `nameof` does not evaluate anything, it is merely a syntactic transformation by the runtime that becomes a string.

 

Ron

 

From: guest271314 <[hidden email]>
Sent: Friday, June 14, 2019 10:07 AM
To: Ron Buckton <[hidden email]>
Cc: [hidden email]
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for

 

Not following the module example. Would not ```ns``` be an instance of ```Module``` where ```* as ns``` is used?

 

For the ```class``` example would ```nameof``` be equivalent to ```Object.getOwnPropertyDescriptors(other).name.value```?

 

It's not that a language like TypeScript would need this, but rather that users of an editor like VS Code or WebStorm that have a JavaScript language service would benefit from it.

 

Is the primary use case a non-simple text editor; e.g., _not_ gedit (GUI) or pico or nano (CLI), where the user is depending on the program

 

for diagnostics (logging and errors)

 

within a text editor _before_ actually running the code in a given environment - not to log errors during code execution? 

 

On Fri, Jun 14, 2019 at 4:29 PM Ron Buckton <[hidden email]> wrote:

The 'nameof' operator provides the string name of a static symbol (in compiler/linker terms, not an ECMAScript 'Symbol'). This is often useful for diagnostics (logging and errors), and gives developers a way to reduce repetition. It is also extremely helpful in editors that support symbolic "rename" refactoring.

While ES functions have a 'name' property, one of the main use cases is to get a string representation of the name of a thing that isn't itself reified as an object, such as a variable, parameter, or module namespace:

```

import * as ns from "foo";

nameof ns; // "ns"

let fn = function g() {};

fn.name; // "g"

nameof fn; // "fn"

```

Here is an example of two common use cases:

```

class C {

  compare(other) {

    if (!(other instanceof C)) {

      throw new TypeError(`Invalid argument: ${nameof other} `);

    }

  set prop(value) {

    this._prop = value;

    this.emit("propertychanged", nameof prop);

  }

}

```

It's not that a language like TypeScript would need this, but rather that users of an editor like VS Code or WebStorm that have a JavaScript language service would benefit from it.

Ron


From: es-discuss <[hidden email]> on behalf of guest271314 <[hidden email]>
Sent: Friday, June 14, 2019 9:05:55 AM
To: Stas Berkov
Cc: [hidden email]
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for

 

Have not tried TypeScript. What are the use cases for JavaScript where TypeScript is not used? Does ```nameof()``` check if an object has a specific property name defined (```"MutationObserver" in window```; ```window.MutationObserver```; ```"document" in globalThis```)?

 

On Fri, Jun 14, 2019 at 1:05 PM Stas Berkov <[hidden email]> wrote:

Can we revisit this issue?

 

In C# there is `nameof`, in Swift you can do the same by calling

```

let keyPath = \Person.mother.firstName

NSPredicate(format: "%K == %@", keyPath, "Andrew")

```

Let's introduce `nameof` in ES, please. 

 

Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 ) 

This feature is eagarly being asked by TypeScript community.

 

I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.

 

Minification.

1. If your code to be minimized be prepared that variable names will also change.

2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.

 

What if user already has `nameof` function.

1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.

2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Re: What do you think about a C# 6 like nameof() expression for

Ron Buckton
In reply to this post by guest271314

Since `nameof` does not actually evaluate anything, the following would be legal:

 

```

const x = nameof y; // "y"

const y = 1;

```

 

However, the shorthand property name workaround is not legal due to TDZ:

 

```

const x = Object.keys({y})[0]; // error due to TDZ

const y = 1;

```

 

With the shortand property name workaround you also run into the added runtime overhead of:

  • Constructing an object (`{y}`)
  • Looking up a property (`Object.keys`)
  • Calling a function (`Object.keys`)
  • Allocating an array
  • Indexing into the array

 

Whereas `nameof` has *no* runtime overhead, because it would be handled during static semantics.

 

 

From: es-discuss <[hidden email]> On Behalf Of guest271314
Sent: Friday, June 14, 2019 11:17 AM
To: Stas Berkov <[hidden email]>
Cc: [hidden email]
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for

 

Terms such as "more robust", "Less fragile." , "Less mess." are subjective. "Fragile" how? What is meant by "mess"? 

 

If the proposal is that ```nameof``` is briefer than using shorthand property names or computed property names to get an identifier as a string, then ```nameof``` would be less code.

 

> You can rename field/property without fear you break something (using IDE refactoring tools).

 

The use case appears to be within the context of an IDE?

 

For users that roll their own code by hand at a text editor and do not use an IDE for composing code (or relying on an IDE for logging errors), am not able to discern how ```nameof``` is different than using ```Object.getOwnPropertyDescriptors(other).name.value``` or ```Object.keys({userName})[0]```. The actual code will need to be tested in different browsers anyway before deployment to get the actual results.

If a user has decided they _have_ to use some form of a dynamic "IDE" to compose code and log errors - while they are writing the code, they can open DevTools at Chromium or Chrome then select ```Sources``` => ```Snippets``` => ```New snippet```, where

 

```

const n = 123;
const n = 456;

```

 

will highlight the line where the error occurs - during writing the code - the as a red circle with a transparent "x" in the middle

 

```Uncaught SyntaxError: Identifier 'n' has already been declared````

 

To run the code press ```ctrl+enter```.

 

 

 

 

On Fri, Jun 14, 2019 at 5:48 PM Stas Berkov <[hidden email]> wrote:

Less fragile. Less mess. You can rename field/property without fear you break something (using IDE refactoring tools).

With high probablity you will break something when you refactor and have fields hardcoded as strings.

Someone can object that you can rename strings as well.

Issue here that you can ocassionally change non-related strings that should not be changed even they match or have matching substring.

 

On Fri, Jun 14, 2019 at 9:38 PM guest271314 <[hidden email]> wrote:

Is Case 1 equivalent to a briefer version of

 

```
   if (userName == undefined) {
       throw new Error(`Argument cannot be null: ${Object.keys({userName})[0]}`);
   }

```

 

 

If not, how is ```nameof``` different?

 

What is the difference between the use of ```message.hasOwnProperty(property)``` and ```nameof msg.expiration_utc_time```?

 

You get more robust code.

 

How is "robust" objectively determined?

 

 

 

 

On Fri, Jun 14, 2019 at 5:21 PM Stas Berkov <[hidden email]> wrote:

ES can befit from `nameof` feature the same way as TS. There is no TS specific in it.

It was ask to introduce in TS as a workaround since TS is considered as extention of ES.

 

Case 1. Function guard.

```

function func1(param1, param2, param3, userName, param4, param5) {

   if (userName == undefined) {

       throw new ArgumentNullError(nameof userName); // `ArgumentNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".

   }

}

```

 

Case 2. Access extended information an object property.

Assume a function

```

function protoPropertyIsSet(message, property) {
    return message != null && message.hasOwnProperty(property);
}

```

Then in code you use it as `if (protoPropertyIsSet(msg, "expiration_utc_time")) {... }`.

Having `nameof` would allow you to do that `if (protoPropertyIsSet(msg, nameof msg.expiration_utc_time)) {... }`.

You get more robust code.

 

On Fri, Jun 14, 2019 at 5:46 PM Augusto Moura <[hidden email]> wrote:

Can you list the benefits of having this operators? Maybe with example use cases

If I understand it correctly, the operator fits better in compiled
(and typed) languages, most of the use cases don't apply to dynamic
Javascript
The only legit use case I can think of is helping refactor tools to
rename properties (but even mismatch errors between strings and
properties names can be caught in compile time using modern
Typescript)

Em sex, 14 de jun de 2019 às 10:05, Stas Berkov
<[hidden email]> escreveu:
>
> Can we revisit this issue?
>
>
> In C# there is `nameof`, in Swift you can do the same by calling
>
> ```
>
> let keyPath = \Person.mother.firstName
>
> NSPredicate(format: "%K == %@", keyPath, "Andrew")
>
> ```
>
> Let's introduce `nameof` in ES, please.
>
>
> Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 )
>
> This feature is eagarly being asked by TypeScript community.
>
>
> I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.
>
>
> Minification.
>
> 1. If your code to be minimized be prepared that variable names will also change.
>
> 2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.
>
>
>
> What if user already has `nameof` function.
>
> 1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.
>
> 2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Atenciosamente,

Augusto Borges de Moura

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

guest271314
In reply to this post by Stas Berkov
> A module namespace isn’t an instance of Module, it’s a module namespace spec object. If it has a name member, that is because the module you imported has an exported binding called name. But what kind of thing it is isn’t what matters.

If ```Object.getOwnPropertyDescriptors(ns)``` is used within ```<script type="module">``` ```Symbol(Symbol.toStringTag): {value: "Module", writable: false, enumerable: false, configurable: false}``` will also be part of the output. If ```Object.keys(ns)``` is used ```Symbol(Symbol.toStringTag)``` will not be part of output.

> Since ```nameof``` does not actually evaluate anything, the following would be legal:

> ```
> const x = nameof y; // "y"
> const y = 1;
> ```

Interesting. ```y``` would be able to be evaluated before ```y``` is defined?


On Fri, Jun 14, 2019 at 6:01 PM Stas Berkov <[hidden email]> wrote:
> Is Case 1 equivalent to a briefer version of
> ```
   if (userName == undefined) {
       throw new Error(`Argument cannot be null: ${Object.keys({userName})[0]}`);
   }
```
Less readable but in this simple case might work.
What if we do the following:
Case 1. Function guard.
```
function func1(options) {
...
   if (options.userName == undefined) {
       throw new ParamNullError(nameof options.userName); // `ParamNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".
   }
}
```

Case 2. Accessing property extended info
e.g.
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
```
vs
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, nameof object1.property1);  
```
2nd variant (proposed) has more chances not to be broken during refactoring (robustness).

On Fri, Jun 14, 2019 at 9:48 PM Stas Berkov <[hidden email]> wrote:
Less fragile. Less mess. You can rename field/property without fear you break something (using IDE refactoring tools).
With high probablity you will break something when you refactor and have fields hardcoded as strings.
Someone can object that you can rename strings as well.
Issue here that you can ocassionally change non-related strings that should not be changed even they match or have matching substring.

On Fri, Jun 14, 2019 at 9:38 PM guest271314 <[hidden email]> wrote:
Is Case 1 equivalent to a briefer version of

```
   if (userName == undefined) {
       throw new Error(`Argument cannot be null: ${Object.keys({userName})[0]}`);
   }
```


If not, how is ```nameof``` different?

What is the difference between the use of ```message.hasOwnProperty(property)``` and ```nameof msg.expiration_utc_time```?

You get more robust code.

How is "robust" objectively determined?




On Fri, Jun 14, 2019 at 5:21 PM Stas Berkov <[hidden email]> wrote:
ES can befit from `nameof` feature the same way as TS. There is no TS specific in it.
It was ask to introduce in TS as a workaround since TS is considered as extention of ES.

Case 1. Function guard.
```
function func1(param1, param2, param3, userName, param4, param5) {
   if (userName == undefined) {
       throw new ArgumentNullError(nameof userName); // `ArgumentNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".
   }
}
```

Case 2. Access extended information an object property.
Assume a function
```
function protoPropertyIsSet(message, property) {
    return message != null && message.hasOwnProperty(property);
}
```
Then in code you use it as `if (protoPropertyIsSet(msg, "expiration_utc_time")) {... }`.
Having `nameof` would allow you to do that `if (protoPropertyIsSet(msg, nameof msg.expiration_utc_time)) {... }`.
You get more robust code.

On Fri, Jun 14, 2019 at 5:46 PM Augusto Moura <[hidden email]> wrote:
Can you list the benefits of having this operators? Maybe with example use cases

If I understand it correctly, the operator fits better in compiled
(and typed) languages, most of the use cases don't apply to dynamic
Javascript
The only legit use case I can think of is helping refactor tools to
rename properties (but even mismatch errors between strings and
properties names can be caught in compile time using modern
Typescript)

Em sex, 14 de jun de 2019 às 10:05, Stas Berkov
<[hidden email]> escreveu:
>
> Can we revisit this issue?
>
>
> In C# there is `nameof`, in Swift you can do the same by calling
>
> ```
>
> let keyPath = \Person.mother.firstName
>
> NSPredicate(format: "%K == %@", keyPath, "Andrew")
>
> ```
>
> Let's introduce `nameof` in ES, please.
>
>
> Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 )
>
> This feature is eagarly being asked by TypeScript community.
>
>
> I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.
>
>
> Minification.
>
> 1. If your code to be minimized be prepared that variable names will also change.
>
> 2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.
>
>
>
> What if user already has `nameof` function.
>
> 1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.
>
> 2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Atenciosamente,

Augusto Borges de Moura
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

Stas Berkov
guest271314, what is you point against `nameof` feature?

If you don't like it - don't use it. Why prohibit this feature for
those who find it beneficial?

I see `nameof` beneficial in following cases

Case 1. Function guard.
```
function func1(options) {
...
   if (options.userName == undefined) {
       throw new ParamNullError(nameof options.userName); //
`ParamNullError` is a custom error, derived from `Error`, composes
error message like "Parameter cannot be null: userName".
 // `Object.keys({options.userName})[0]` will not work here
   }
}
```

Case 2. Accessing property extended info
Those ES functions that accept field name as string.
e.g.
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
```
vs
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, nameof
object1.property1);
 // `Object.keys({options1.property1})[0]` will not work here
```
2nd variant (proposed) has more chances not to be broken during
refactoring (robustness).

It would make devs who use IDE more productive and make their life
easier. Why not give them such possiblity and make them happy?
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

guest271314
Am neither for nor against the proposal. Do not entertain "like"s or "dislike"s in any field of endeavor. Am certainly not in a position to prohibit anything relevant JavaScript. Do what thou wilt shall be the whole of the Law.

Have yet to view a case where code will be "broken" by ```nameof``` not being a JavaScript feature. "robustness", as already mentioned, is a subjective adjective that is not capable of being objectively evaluated as to code itself. That description is based on preference or choice.

In lieu of the proposal being specificed, use the posted code example of ```Object.keys()``` that "works". 

```
function func1({userName = void 0} = {}) {
  console.assert(userName !== undefined, [{userName}, 'property needs to be defined'])
}
```

provides a direct indication that the property value is required to be defined. Note that the example code posted thus far does not first check if ```options``` is passed at all, for which ```nameof``` will not provide any asssitance.

Usually try to meet requirement by means already available in FOSS browsers. Have no interest in TypeScript or using an IDE.

FWIW, have no objection to the proposal.

On Fri, Jun 14, 2019 at 7:53 PM Stas Berkov <[hidden email]> wrote:
guest271314, what is you point against `nameof` feature?

If you don't like it - don't use it. Why prohibit this feature for
those who find it beneficial?

I see `nameof` beneficial in following cases

Case 1. Function guard.
```
function func1(options) {
...
   if (options.userName == undefined) {
       throw new ParamNullError(nameof options.userName); //
`ParamNullError` is a custom error, derived from `Error`, composes
error message like "Parameter cannot be null: userName".
 // `Object.keys({options.userName})[0]` will not work here
   }
}
```

Case 2. Accessing property extended info
Those ES functions that accept field name as string.
e.g.
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
```
vs
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, nameof
object1.property1);
 // `Object.keys({options1.property1})[0]` will not work here
```
2nd variant (proposed) has more chances not to be broken during
refactoring (robustness).

It would make devs who use IDE more productive and make their life
easier. Why not give them such possiblity and make them happy?

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Re: What do you think about a C# 6 like nameof() expression for

Ron Buckton
In reply to this post by guest271314

 

> Interesting. ```y``` would be able to be evaluated before ```y``` is defined?

I very explicitly stated that `nameof y` would *not* evaluate its operand. Evaluation of `nameof` would merely result in a string containing the name of the binding referenced by the operand.

 

From: es-discuss <[hidden email]> On Behalf Of guest271314
Sent: Friday, June 14, 2019 12:09 PM
To: Stas Berkov <[hidden email]>
Cc: [hidden email]
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for

 

> A module namespace isn’t an instance of Module, it’s a module namespace spec object. If it has a name member, that is because the module you imported has an exported binding called name. But what kind of thing it is isn’t what matters.

 

If ```Object.getOwnPropertyDescriptors(ns)``` is used within ```<script type="module">``` ```Symbol(Symbol.toStringTag): {value: "Module", writable: false, enumerable: false, configurable: false}``` will also be part of the output. If ```Object.keys(ns)``` is used ```Symbol(Symbol.toStringTag)``` will not be part of output.

 

> Since ```nameof``` does not actually evaluate anything, the following would be legal:

> ```
> const x = nameof y; // "y"
> const y = 1;
> ```

Interesting. ```y``` would be able to be evaluated before ```y``` is defined?

 

On Fri, Jun 14, 2019 at 6:01 PM Stas Berkov <[hidden email]> wrote:

> Is Case 1 equivalent to a briefer version of

> ```

   if (userName == undefined) {
       throw new Error(`Argument cannot be null: ${Object.keys({userName})[0]}`);
   }

```

Less readable but in this simple case might work.

What if we do the following:

Case 1. Function guard.

```

function func1(options) {

...

   if (options.userName == undefined) {

       throw new ParamNullError(nameof options.userName); // `ParamNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".

   }

}

```

 

Case 2. Accessing property extended info

e.g.

```

const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');

```

vs

```

const descriptor1 = Object.getOwnPropertyDescriptor(object1, nameof object1.property1);  

```

2nd variant (proposed) has more chances not to be broken during refactoring (robustness).

 

On Fri, Jun 14, 2019 at 9:48 PM Stas Berkov <[hidden email]> wrote:

Less fragile. Less mess. You can rename field/property without fear you break something (using IDE refactoring tools).

With high probablity you will break something when you refactor and have fields hardcoded as strings.

Someone can object that you can rename strings as well.

Issue here that you can ocassionally change non-related strings that should not be changed even they match or have matching substring.

 

On Fri, Jun 14, 2019 at 9:38 PM guest271314 <[hidden email]> wrote:

Is Case 1 equivalent to a briefer version of

 

```
   if (userName == undefined) {
       throw new Error(`Argument cannot be null: ${Object.keys({userName})[0]}`);
   }

```

 

 

If not, how is ```nameof``` different?

 

What is the difference between the use of ```message.hasOwnProperty(property)``` and ```nameof msg.expiration_utc_time```?

 

You get more robust code.

 

How is "robust" objectively determined?

 

 

 

 

On Fri, Jun 14, 2019 at 5:21 PM Stas Berkov <[hidden email]> wrote:

ES can befit from `nameof` feature the same way as TS. There is no TS specific in it.

It was ask to introduce in TS as a workaround since TS is considered as extention of ES.

 

Case 1. Function guard.

```

function func1(param1, param2, param3, userName, param4, param5) {

   if (userName == undefined) {

       throw new ArgumentNullError(nameof userName); // `ArgumentNullError` is a custom error, derived from `Error`, composes error message like "Argument cannot be null: userName".

   }

}

```

 

Case 2. Access extended information an object property.

Assume a function

```

function protoPropertyIsSet(message, property) {
    return message != null && message.hasOwnProperty(property);
}

```

Then in code you use it as `if (protoPropertyIsSet(msg, "expiration_utc_time")) {... }`.

Having `nameof` would allow you to do that `if (protoPropertyIsSet(msg, nameof msg.expiration_utc_time)) {... }`.

You get more robust code.

 

On Fri, Jun 14, 2019 at 5:46 PM Augusto Moura <[hidden email]> wrote:

Can you list the benefits of having this operators? Maybe with example use cases

If I understand it correctly, the operator fits better in compiled
(and typed) languages, most of the use cases don't apply to dynamic
Javascript
The only legit use case I can think of is helping refactor tools to
rename properties (but even mismatch errors between strings and
properties names can be caught in compile time using modern
Typescript)

Em sex, 14 de jun de 2019 às 10:05, Stas Berkov
<[hidden email]> escreveu:
>
> Can we revisit this issue?
>
>
> In C# there is `nameof`, in Swift you can do the same by calling
>
> ```
>
> let keyPath = \Person.mother.firstName
>
> NSPredicate(format: "%K == %@", keyPath, "Andrew")
>
> ```
>
> Let's introduce `nameof` in ES, please.
>
>
> Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579 )
>
> This feature is eagarly being asked by TypeScript community.
>
>
> I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.
>
>
> Minification.
>
> 1. If your code to be minimized be prepared that variable names will also change.
>
> 2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.
>
>
>
> What if user already has `nameof` function.
>
> 1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.
>
> 2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss



--
Atenciosamente,

Augusto Borges de Moura

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

Jordan Harband
In reply to this post by guest271314
`nameof whatever` → `Object.keys({ whatever })[0]`, but I'm a bit confused why it'd be better to type `nameof foo` in code, rather than `'foo'` - if you change `foo` to `bar`, you have to change both of them anyways.

On Fri, Jun 14, 2019 at 1:31 PM guest271314 <[hidden email]> wrote:
Am neither for nor against the proposal. Do not entertain "like"s or "dislike"s in any field of endeavor. Am certainly not in a position to prohibit anything relevant JavaScript. Do what thou wilt shall be the whole of the Law.

Have yet to view a case where code will be "broken" by ```nameof``` not being a JavaScript feature. "robustness", as already mentioned, is a subjective adjective that is not capable of being objectively evaluated as to code itself. That description is based on preference or choice.

In lieu of the proposal being specificed, use the posted code example of ```Object.keys()``` that "works". 

```
function func1({userName = void 0} = {}) {
  console.assert(userName !== undefined, [{userName}, 'property needs to be defined'])
}
```

provides a direct indication that the property value is required to be defined. Note that the example code posted thus far does not first check if ```options``` is passed at all, for which ```nameof``` will not provide any asssitance.

Usually try to meet requirement by means already available in FOSS browsers. Have no interest in TypeScript or using an IDE.

FWIW, have no objection to the proposal.

On Fri, Jun 14, 2019 at 7:53 PM Stas Berkov <[hidden email]> wrote:
guest271314, what is you point against `nameof` feature?

If you don't like it - don't use it. Why prohibit this feature for
those who find it beneficial?

I see `nameof` beneficial in following cases

Case 1. Function guard.
```
function func1(options) {
...
   if (options.userName == undefined) {
       throw new ParamNullError(nameof options.userName); //
`ParamNullError` is a custom error, derived from `Error`, composes
error message like "Parameter cannot be null: userName".
 // `Object.keys({options.userName})[0]` will not work here
   }
}
```

Case 2. Accessing property extended info
Those ES functions that accept field name as string.
e.g.
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
```
vs
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, nameof
object1.property1);
 // `Object.keys({options1.property1})[0]` will not work here
```
2nd variant (proposed) has more chances not to be broken during
refactoring (robustness).

It would make devs who use IDE more productive and make their life
easier. Why not give them such possiblity and make them happy?
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Re: What do you think about a C# 6 like nameof() expression for

Ron Buckton

> `nameof whatever` → `Object.keys({ whatever })[0]`, but I'm a bit confused why it'd be better to type `nameof foo` in code, rather than `'foo'` - if you change `foo` to `bar`, you have to change both of them anyways.

 

If you are using an editor that supports rename refactoring, its generally easier to rename the symbol `foo` and have all references (including `nameof foo`) be updated. You cannot safely automatically rename `'foo'` to `'bar'` since an editor or language service cannot guarantee that by the string `'foo'` you meant “the text of the identifier `foo`”.

 

From: es-discuss <[hidden email]> On Behalf Of Jordan Harband
Sent: Friday, June 14, 2019 2:29 PM
To: guest271314 <[hidden email]>
Cc: es-discuss <[hidden email]>
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for

 

`nameof whatever` → `Object.keys({ whatever })[0]`, but I'm a bit confused why it'd be better to type `nameof foo` in code, rather than `'foo'` - if you change `foo` to `bar`, you have to change both of them anyways.

 

On Fri, Jun 14, 2019 at 1:31 PM guest271314 <[hidden email]> wrote:

Am neither for nor against the proposal. Do not entertain "like"s or "dislike"s in any field of endeavor. Am certainly not in a position to prohibit anything relevant JavaScript. Do what thou wilt shall be the whole of the Law.

Have yet to view a case where code will be "broken" by ```nameof``` not being a JavaScript feature. "robustness", as already mentioned, is a subjective adjective that is not capable of being objectively evaluated as to code itself. That description is based on preference or choice.

 

In lieu of the proposal being specificed, use the posted code example of ```Object.keys()``` that "works". 

 

```

function func1({userName = void 0} = {}) {
  console.assert(userName !== undefined, [{userName}, 'property needs to be defined'])
}

```

 

provides a direct indication that the property value is required to be defined. Note that the example code posted thus far does not first check if ```options``` is passed at all, for which ```nameof``` will not provide any asssitance.

 

Usually try to meet requirement by means already available in FOSS browsers. Have no interest in TypeScript or using an IDE.

 

FWIW, have no objection to the proposal.

 

On Fri, Jun 14, 2019 at 7:53 PM Stas Berkov <[hidden email]> wrote:

guest271314, what is you point against `nameof` feature?

If you don't like it - don't use it. Why prohibit this feature for
those who find it beneficial?

I see `nameof` beneficial in following cases

Case 1. Function guard.
```
function func1(options) {
...
   if (options.userName == undefined) {
       throw new ParamNullError(nameof options.userName); //
`ParamNullError` is a custom error, derived from `Error`, composes
error message like "Parameter cannot be null: userName".
 // `Object.keys({options.userName})[0]` will not work here
   }
}
```

Case 2. Accessing property extended info
Those ES functions that accept field name as string.
e.g.
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
```
vs
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, nameof
object1.property1);
 // `Object.keys({options1.property1})[0]` will not work here
```
2nd variant (proposed) has more chances not to be broken during
refactoring (robustness).

It would make devs who use IDE more productive and make their life
easier. Why not give them such possiblity and make them happy?

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

guest271314
How is that behaviour related to the use cases presented by OP? Would such behaviour not lead to false-positive relevant to the 2 use cases?

On Fri, Jun 14, 2019 at 9:36 PM Ron Buckton <[hidden email]> wrote:

> `nameof whatever` → `Object.keys({ whatever })[0]`, but I'm a bit confused why it'd be better to type `nameof foo` in code, rather than `'foo'` - if you change `foo` to `bar`, you have to change both of them anyways.

 

If you are using an editor that supports rename refactoring, its generally easier to rename the symbol `foo` and have all references (including `nameof foo`) be updated. You cannot safely automatically rename `'foo'` to `'bar'` since an editor or language service cannot guarantee that by the string `'foo'` you meant “the text of the identifier `foo`”.

 

From: es-discuss <[hidden email]> On Behalf Of Jordan Harband
Sent: Friday, June 14, 2019 2:29 PM
To: guest271314 <[hidden email]>
Cc: es-discuss <[hidden email]>
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for

 

`nameof whatever` → `Object.keys({ whatever })[0]`, but I'm a bit confused why it'd be better to type `nameof foo` in code, rather than `'foo'` - if you change `foo` to `bar`, you have to change both of them anyways.

 

On Fri, Jun 14, 2019 at 1:31 PM guest271314 <[hidden email]> wrote:

Am neither for nor against the proposal. Do not entertain "like"s or "dislike"s in any field of endeavor. Am certainly not in a position to prohibit anything relevant JavaScript. Do what thou wilt shall be the whole of the Law.

Have yet to view a case where code will be "broken" by ```nameof``` not being a JavaScript feature. "robustness", as already mentioned, is a subjective adjective that is not capable of being objectively evaluated as to code itself. That description is based on preference or choice.

 

In lieu of the proposal being specificed, use the posted code example of ```Object.keys()``` that "works". 

 

```

function func1({userName = void 0} = {}) {
  console.assert(userName !== undefined, [{userName}, 'property needs to be defined'])
}

```

 

provides a direct indication that the property value is required to be defined. Note that the example code posted thus far does not first check if ```options``` is passed at all, for which ```nameof``` will not provide any asssitance.

 

Usually try to meet requirement by means already available in FOSS browsers. Have no interest in TypeScript or using an IDE.

 

FWIW, have no objection to the proposal.

 

On Fri, Jun 14, 2019 at 7:53 PM Stas Berkov <[hidden email]> wrote:

guest271314, what is you point against `nameof` feature?

If you don't like it - don't use it. Why prohibit this feature for
those who find it beneficial?

I see `nameof` beneficial in following cases

Case 1. Function guard.
```
function func1(options) {
...
   if (options.userName == undefined) {
       throw new ParamNullError(nameof options.userName); //
`ParamNullError` is a custom error, derived from `Error`, composes
error message like "Parameter cannot be null: userName".
 // `Object.keys({options.userName})[0]` will not work here
   }
}
```

Case 2. Accessing property extended info
Those ES functions that accept field name as string.
e.g.
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
```
vs
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, nameof
object1.property1);
 // `Object.keys({options1.property1})[0]` will not work here
```
2nd variant (proposed) has more chances not to be broken during
refactoring (robustness).

It would make devs who use IDE more productive and make their life
easier. Why not give them such possiblity and make them happy?

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: What do you think about a C# 6 like nameof() expression for

Augusto Moura
In reply to this post by Jordan Harband
Fri, 14 jun 2019 - 18:29, Jordan Harband <[hidden email]> wrote:
>
> `nameof whatever` → `Object.keys({ whatever })[0]`, but I'm a bit confused why it'd be better to type `nameof foo` in code, rather than `'foo'` - if you change `foo` to `bar`, you have to change both of them anyways.
>

Exactly, if you already have the name of the property beforehand in
design time why not write it as a string literal

Again, the only justifiable use case is refactoring tools, but even
today that can be arranged with static code analysis

You can safe guard a string literal to be a property of a type in
Typescript with a bit of handwork
``` ts
interface Options {
  userName?: string;
}

// If you change the property in the interface without changing here, Typescript
// will raise a error informing that 'userName' is not a valid key of Options
const userNameKey: keyof Options = 'userName';

if (options.userName === undefined) {
  throw new ParamNullError(userNameKey);
}
```

--
Atenciosamente,

Augusto Borges de Moura
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
123