Es-discuss - several decimal discussions
Brendan Eich
brendan at mozilla.org
Wed Sep 3 23:28:56 PDT 2008
On Sep 3, 2008, at 11:15 PM, David-Sarah Hopwood wrote:
> In the absence of decimal, Object.eq is trivial to spec:
>
> Object.eq(NaN, NaN) = true
> Object.eq( 0, -0) = false
> Object.eq( -0, 0) = false
> Object.eq( x, y) = (x === y), otherwise.
>
> or to implement:
>
> Object.eq = function (x, y) {
> if (x === y) {
> return x !== 0 || 1/x === 1/y;
> } else {
> return x !== x && y !== y;
> }
> };
Even shorter, without else after return non-sequitur and with one
guaranteed same-type == instead of === to be perverse:
Object.eq = function (x, y) {
return (x === y) ? x !== 0 || 1/x == 1/y : x !== x && y !== y;
}
But what's the point?
My point was that Decimal doesn't make the case particularly strong,
since you just need to add Decimal.compareTotal:
Object.eq = function (x, y) {
if (x instanceof Decimal && y instanceof Decimal)
return Decimal.compareTotal(x, y) == 0;
return (x === y) ? x !== 0 || 1/x == 1/y : x !== x && y !== y;
}
How much less trivial is this? Two lines, fewer with more ?: chaining.
>> Just not particularly on account of Decimal, even
>> with equated cohort members.
>
> If there is a possibility that we are ever going to add decimal (or
> other
> types for which === might not be an identity test), then adding
> Object.eq
> now allows writing future-proof code for such things as memoizers --
> whatever the semantics of === for decimals (or between decimals and
> other numbers) turns out to be.
JS developers have to cope with downrev browsers. There's no
imperative "now" (ES3.1) vs. "next time", since they'll need
something like the above as fallback for "last time" (ES3-based)
browsers.
Again my point is to avoid mission creep in 3.1. I have no problem
with identical and hashcode. If I'm wrong and they can be slipped in
without any delay, filling inevitable latency in the current
schedule, great.
I should get busy on the spec.
Ob. Bikeshed: You seem committed to eq as the name. I say it's undue
Lisp nostalgia, if not arrant cybercrud. But I concede that identical
is overlong. Alternative ideas?
/be
More information about the Es-discuss
mailing list