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 <guest271...@gmail.com>
Sent: Friday, June 14, 2019 10:07 AM
To: Ron Buckton <ron.buck...@microsoft.com>
Cc: es-discuss@mozilla.org
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 
<ron.buck...@microsoft.com<mailto:ron.buck...@microsoft.com>> 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<https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Ffn.name&data=02%7C01%7CRon.Buckton%40microsoft.com%7C104ffaa78f6847e3a31708d6f0eab6e3%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636961288212685352&sdata=sBP1RXbppYfrzbSFUaC%2F80IBZk1tUDqdxeVmbuTveSE%3D&reserved=0>;
 // "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 
<es-discuss-boun...@mozilla.org<mailto:es-discuss-boun...@mozilla.org>> on 
behalf of guest271314 <guest271...@gmail.com<mailto:guest271...@gmail.com>>
Sent: Friday, June 14, 2019 9:05:55 AM
To: Stas Berkov
Cc: es-discuss@mozilla.org<mailto:es-discuss@mozilla.org>
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 
<stas.ber...@gmail.com<mailto:stas.ber...@gmail.com>> 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
es-discuss@mozilla.org<mailto: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

Reply via email to