What do you think about a C# 6 like nameof() expression for JavaScript.
Ron Buckton
Ron.Buckton at microsoft.com
Sun Aug 9 03:12:28 UTC 2015
One of the main purposes of the `nameof` operator is to provide the string value of a symbol, so that if you perform a "Rename" refactoring of that symbol that the change is also reflected. This is primarily for cases where you perform precondition assertions tied to an argument:
```
...
static void Method(string x) {
if (x == null) throw new ArgumentNullException(nameof(x));
...
}
```
Now, if I later rename `x`, I don't need to also find any string literals of "x" and manually update them.
There are other uses of `nameof`, but they all boil down to roughly the same thing.
Ron
________________________________
From: Isiah Meadows<mailto:isiahmeadows at gmail.com>
Sent: 8/8/2015 7:23 PM
To: Behrang Saeedzadeh<mailto:behrangsa at gmail.com>; EcmaScript Discuss Mailing List<mailto:es-discuss at mozilla.org>
Subject: Re: What do you think about a C# 6 like nameof() expression for JavaScript.
Call me crazy, but I don't see anything that couldn't be done more concisely with a string literal. Is it supposed to be able to do this?
```js
function foo(x) {
return nameof(x);
}
foo(bar); // "bar";
```
In that case, the engine would have to keep track of usages as well, in a similar sense as `arguments.callee`, and if it were a function, it would make optimization quite difficult, as engines don't have the capacity to statically analyze that such a function is used.
If it is like `typeof`, we now have a breaking change - a keyword that was a valid Identifier before.
```js
// Error?
function nameof(value) {
return value.name<https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2fvalue.name&data=01%7c01%7cron.buckton%40microsoft.com%7ca2e2c4d35400435810d008d2a061897d%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=pwV45avF9RX6COETpoLIY4EF%2bmCVmk6kEEmLc2JXSCY%3d>;
}
var bar = {name: 2};
nameof(bar); // "bar" or 2?
```
I don't think this is going to work out in practice, not in ECMAScript proper. You might appreciate Sweet.js, though.
On Sat, Aug 8, 2015, 21:27 Behrang Saeedzadeh <behrangsa at gmail.com<mailto:behrangsa at gmail.com>> wrote:
Forgot to mention that nameof works with local variables too:
function foo() {
var aNum = 1;
console.log(nameof(aNmum), aNum);
}
On Sat, Aug 8, 2015 at 10:38 AM Behrang Saeedzadeh <behrangsa at gmail.com<mailto:behrangsa at gmail.com>> wrote:
So basically we could use it like this:
function aFunc(aParam) {
throw new Error(nameof(aParam));
}
and nameof(aParam) would return the string "aParam".
This is possible to do even right now using arguments.callee and some hacky code, but having it built-in to spec would be nicer IMHO.
--
Best regards,
Behrang Saeedzadeh
--
Best regards,
Behrang Saeedzadeh
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org<mailto:es-discuss at mozilla.org>
https://mail.mozilla.org/listinfo/es-discuss<https://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fmail.mozilla.org%2flistinfo%2fes-discuss&data=01%7c01%7cron.buckton%40microsoft.com%7ca2e2c4d35400435810d008d2a061897d%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=7DHMx5gTd2OexSlKscSrKlMIxABMUkOKRC%2fuCbc6pWk%3d>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150809/82b0e514/attachment.html>
More information about the es-discuss
mailing list