tc39 / proposal-extended-numeric-literals

Extensible numeric literals for JavaScript
https://tc39.github.io/proposal-extended-numeric-literals/
72 stars 18 forks source link

What is the benefit of allowing extensions on special numeric literal forms? #13

Open rwaldron opened 6 years ago

rwaldron commented 6 years ago

...More specifically, when would a program's source benefit from allowing the author to write things like:

It would be nice to see real cases where a quantity of pixels are represented in hexadecimal, or quantity of centimeters in binary, or a quantity of dollars in octal.

littledan commented 6 years ago

What did you find unconvincing about https://github.com/tc39/proposal-extended-numeric-literals/blob/master/README.md#motivation ?

Cc @TabAtkins

rwaldron commented 6 years ago

What did you find unconvincing

Nothing in that section is relevant to my question. I'm asking something very specific and I'd appreciate it if you, as champion of this proposal, would take the time to consider my question instead of pointing me at some explainer prose that doesn't address my question at all.

tabatkins commented 6 years ago

@rwaldron is specifically asking about using this on special numeric forms (bin, oct, hex), not the general motivation.

(Presumably because, if we disallow it on hex, we can throw away the _ and just use units directly.)

rwaldron commented 6 years ago

@tabatkins yep, you're exactly right on both accounts.

littledan commented 6 years ago

Sorry for the delay in my response, I missed that your question was about different bases. Bases come up in both one of the examples:

It'd seem to be a pretty odd omission to just not support other bases for something that's number-like. For this reason, BigInts support all the bases that Numbers do (even though I didn't have an especially strong use case for, say, octal in mind).

tabatkins commented 6 years ago

CSS's only hex usage shows up only in the context of #abcdef, and in the Typed OM we'll probably just have a constructor for it that takes the four channels as separate arguments; it's not super relevant here.

rwaldron commented 6 years ago

@littledan I agree with allowing the built-in BigIntLiteralSuffix for all existing bases, but there is still no compelling motivation to design this such that user extensions may be applied to all NumericLiteral forms.

User-level BigInt-like thing

In the absence of operator overloads, this isn't a compelling case. Consider the following:

// How would user-level _n be defined to do anything useful here?
0x100000000000000000000000000000_n * 1; 

// What does this do?
0x100000_n * 0x100000_n;

A built-in BigInt extension (ie. BigIntLiteralSuffix) is the right solution for that, where these semantics, including operator overloading behavior, can be explicitly defined.

littledan commented 6 years ago

In the BigInt proposal, we said we were walking down the trail that we'd later try to generalize with operator overloading, extensible literals and value types. Even if this proposal doesn't have operator overloading, the idea is that it would play well with a future operator overloading proposal.

That's not to say that we have bullet-proof motivation! Maybe we should reconsider that package. But, I wrote up this proposal to try to begin walking down that road, where the idea was to have these multiple features playing well together.

dy commented 5 years ago

I had a couple of cases when having 0x0ffffffff_f64 syntax would be a really nice way to write hex or binary as floating point. There is a lot of packages where that may come handy, as well as stdlib-io requests some updates for standards, where this feature may be relevant.