tc39 / proposal-number-fromstring

{BigInt,Number}.fromString(string, radix)
https://mathiasbynens.github.io/proposal-number-fromstring/
65 stars 7 forks source link

To `ToString(string)` or not to `ToString(string)`? #17

Closed mathiasbynens closed 6 years ago

mathiasbynens commented 6 years ago

Step 1 of both algorithms is:

1. Let _S_ be ? ToString(_string_).

As @jakobkummerow points out, we could choose to not do this, and instead just throw for non-string arguments.

jakobkummerow commented 6 years ago

Obviously, a large number of existing functions in ECMAScript perform various implicit conversions on their arguments. .fromString could choose to match that pattern.

That said, there is also a large number of existing cases of TypeErrors being thrown for invalid arguments to various functions.

From a "minimizing surprise" point of view, given that the thing is called "fromString", being required to pass a string to it seems less surprising than being able to pass arbitrary objects. (Number.parseInt(true, 32) === 978894 seems quite surprising -- Number.fromString should preserve more sanity!)

FWIW, implementation effort and execution speed (when passing strings) should be pretty much the same.