-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object with operator overloading + literals? #51
Comments
Great analysis! Fair enumeration of pros and cons. The key issue for me is that decimal will be only one of many upcoming numeric or numeric-like types, by which I mean, types that overload the arithmetic operators with arithmetic-like meaning, i.e., that satisfy many of the algebraic identities that we expect of these operators.
So I am against doing something for decimal that is not equivalent to what future JS programmers will be able to do for themselves with these others. That doesn't mean we don't eventually make some kind of special case for decimal, such as building it into the language. But it means we do it after we add general operator overloading and implement decimal as a library using it. It also does not necessarily mean that these are all objects, though it probably does. All of those above would have liked to be value types rather than objects if we could figure out a good general value types proposal. In E, all of these would have been "Data" objects rather than normal objects. E "Data" has the characteristics:
But, like Dan says, I doubt we'll be able to figure out general value types for JavaScript. Dan's operator proposal shows we can get most of the benefits for object types. I think we should. |
Regarding money, Agoric would never use decimal for it anyway, even if it were in the language. To us, the only good representation for money is BigInt, in terms of the smallest possible tradable atomic unit of a given currency. The only problem with a BigInt representation is its printed form, on both input and output. For this, if we had operator overloading, we'd build an object around a BigInt that had the additional desired formatting knowledge. |
@erights Are you saying, we should take all of the tradeoffs above ( Could we discuss use cases in #52 rather than this issue? ("smallest possible tradeable atomic unit" is not always so well-defined for currencies, but I'd like to dig into this elsewhere.) |
yes
I am arguing from consistency. If all the others will need to suffer these problems, why not decimal too? |
Hi Mark, sorry to barge in late. We have been talking about value types on and off (more off than on in my case, apologies) for years, so have some context not everyone else has. Dan and I are talking about writing it down, but until then, I'd like to leave my two cents on not deciding yet for Decimal that Object is the only way to go. Consistency has many dimensions with trade-offs among them, and if we can get our value types proposal together, perhaps Decimal (after BigInt, after number/string/boolean/symbol) can be a value type and people expecting consistency along the "numeric" dimension win. |
I'm fine with continuing to try to get general extensible value types working. If we do, do you agree that the above bulleted examples of things that would overload operators while maintaining algebraic identities should then be written as value types? If we decide to go this route, I would still like to wait until decimal has actually been implemented this way before considering more direct support for it. |
In the February 2020 TC39 meeting, @erights and @kmiller68 suggested that decimal may have the semantics of an object with operator overloading and extended literal syntax, following the proposals for each. This is partly inspired by @kmiller68's assessment of the complexity of adding new primitives (from both a design and implementation perspective, with a possible "slippery slope" of many new primitives) and @erights' assessment that it may be difficult to add user-defined value types to JS (due to conflicting requirements around a cross-realm value type registry).
When I discussed this idea with @BrendanEich, he raised several concerns about object semantics:
===
may not be overloaded. Instead, records and tuples and potential future value types proposals make forms of compound values which are the same, and compare as equal. Part of this would be to not expand beyond-0
andNaN
the set of values which are not the same byObject.is
but are the same by===
.===
. Developers would have to use==
to compare decimals instead, needing to unlearn the advice they've gotten about not using==
.Object.is
and don't permit other hash functions. The collection normalization proposal would let collections be keyed by other values; for decimals, keying a map by the.toString()
value would probably work well.ange this, though Brendan has proposed that value types be able to define more falsy values.
if (0m)
would take the true branch, for decimals, which would not match expectations people may have that decimals behave like Numbers in this respect.Are these impacts worth it? It seems to me like decimals as objects rather than primitives would be suboptimal for JS developers, but probably significantly better than not adding the feature.
The text was updated successfully, but these errors were encountered: