Should unary + be made goofy because of asm.js? #25
Comments
Well, it'd still be nice if shipping Integer didn't block on removing asm.js. Different browsers have different appetites for these deprecations, and some might want to keep it for longer. But anyway, I agree, where possible, better to not carry technical debt. I see a couple options to potentially decouple the two, under the assumption that asm.js will be removed and Integer will not be added to it, but to allow the two to temporarily coexist in some engines:
Any opinions on these? |
I don't think preserving the identities for asm.js makes sense really if asm.js is ultimately going to be removed by browsers so I'd prefer the former option simply because when asm.js is removed any code still using asm.js is going to be run as the former anyway. |
There's another argument, that non-asm code may rely on the invariant that |
That's a big utility; it's a clear, safe, syntactic way to coerce to a number. |
The non-asm.js argument for @ljharb Do you think the utility is |
To a numeric value; specifically so I can pass it to functions expecting a numeric value and call prototype methods on it. |
@ljharb The distinction here is that
|
Since I assume Separately, the value of having a syntactic, safe method of always producing a value of |
@syg I'd hypothesize that most code which uses unary |
There is definitely code using both unary and binary minus to cast to a number; I'm pretty sure some of my shims do that. |
The thing is though, wrong arguments passed to a function is wrong arguments anyway, even if a function uses Like say I have a function function foo(arg) {
const x = +arg
return [1,2,3,4][x]
} Does it really matter if it behaves unexpectedly in the case of, given that Integers aren't really a valid argument to begin with? foo(2n) // undefined as [1,2,3,4]['2n'] would be undefined Because as it stands no existing code will break from this behaviour, and if an Integer slips into the code that's simply a concern of either
|
@Jamesernator since few things throw when attempted to be coerced to a number, and Integers have a very intuitive implicit conversion to Number, why would we want it to throw? I would also expect that the spec itself would want to start returning Integers instead of Numbers from builtin methods - if there was an implicit coercion from Integer to Number, it might be web compatible to make |
@ljharb I'm not suggesting that +Integer throws but rather that it simply acts as identity for integers, e.g. The thing is I see only drawbacks to coercing it to a number e.g. this simple case: function add(a, b) {
return (+a) + (+b)
}
// Supposing a hypothetical syntax for big exponents
add(1e100n, 1e100n) Would be Infinity, which means existing functions which otherwise would be logically sound with no coercion couldn't be used at all with the Integer type. I'd much rather that throw than coerce to number and certainly much rather it simply equal Also returning integers from Of course I have no issues with future methods returning Integers or even future |
Regardless of what |
The spec specifies that calling Number(x) would be sufficient. What I want to see is |
@Jamesernator What do you mean by "can't be resolved for the type"? |
@littledan Basically I mean Effectively in spec text terms I'd like to see Unary + replaced with something along these general lines (there's probably better ways to define it):
With BigInt's defining an internal slot:
|
@Jamesernator I don't think any of that mechanism is necessary--we already have ToNumeric. I think we'd just make |
We discussed this question at the May 2017 TC39 meeting and decided to stick with throwing on +BigInt. |
It is my understanding that, at Mozilla at least, we'd like to sunset asm.js opts sooner rather than later. Integers, by all means, should outlive asm.js by a long, long time. When do we optimistically estimate that Integer will ship unflagged in the various engines? That might be distance enough in the future that we need not, and should not, make unary + goofy.
The text was updated successfully, but these errors were encountered: