Ecma262: BigInt implicit conversions

Created on 20 Jul 2020  路  7Comments  路  Source: tc39/ecma262

There's a paragraph about BigInt in 7.1 Type Conversion:

The BigInt type has no implicit conversions in the ECMAScript language; programmers must call BigInt explicitly to convert values from other types.

At the same time BigInt value can be conversed to Boolean value according Table 10: ToBoolean Conversions, likes the following:

1n ? 'true' : 'false' // 1n will be converted to true

So I suppose that paragraph about BigInt implicit conversions should be removed

question

Most helpful comment

If coercion doesn't exist that you'll get a TypeError, isn't it?
Observed "coerced" values and not observed "coerced" values are coerced values both ;)

Anyway our dialog creates questions around paragraph from 7.1 Type Conversion.
I think it must be edit to exclude potential questions. So spec must be improved:

  • Maybe specification needs to add its' meaning for coercion term.
  • Maybe specification needs to add some comments for sentence: The BigInt type has no implicit conversions ..

But now without any improvements spec writes that coercion for BigInt exists via Table 12. BigInt conversions

All 7 comments

I believe the paragraph's intention is to say that there is no way to implicitly convert a value into a BigInt, which is true.

Indeed; it鈥檚 saying you can鈥檛 implicitly coerce into BigInt. You can coerce everything into other things.

@devsnek @ljharb But you can implicitly coerce into BigInt :)

Please check this section 7.1.13 ToBigInt. It has the following conversion table:

ToBigInt

Therefore BitInt has conversion from Boolean and String types.

So developer can explicit convert to BigInt:

BigInt(false)   // will be converted to 0n
BigInt(true)    // will be converted to 1n
BigInt('1')     // will be converted to 1n
BigInt('2')     // will be converted to 2n

But coercions exist for String type, for example:

"1n" > 1n      // string "1n" will be converted to 1n BigInt, 
"2n" == 2n     // string "2n" will be converted to 2n BigInt, 

This coercions described in 7.2.14 Abstract Relational Comparison and 7.2.15 Abstract Equality Comparison

That's an explicit, not an implicit, coercion to BigInt.

I try to describe my position other words

Explicit conversion (type casting) when developer call BigInt:

BigInt(false)   // 0n
BigInt(true)    // 1n
BigInt('1')     // 1n
BigInt('2')     // 2n

Not let's analyze one simple block of code:

'2' > 1n

In this binary expression will be executed abstract relational comparison operator >

There's a quote of 7.2.14 Abstract Relational Comparison

小薪懈屑芯泻 褝泻褉邪薪邪 2020-07-21 胁 02 09 46

Further String value '2' will be implicitly converted to BigInt via abstract operator StringToBigInt

'2' > 1n expression after StringToBigInt operator eval will be converted to 2n > 1n

@ljharb Can you explain please why there's no implicit conversion (type coercion) in '2' > 1n expression?
Because I still sure that is implicit :)

_p.s. I've just try to understand why I not right in your opinion_

I consider < and > "coercions" the same as if (x) "coercions" - not actually coercing the values; that's just how the spec describes those comparisons. You can never observe the "coerced" values, so they don't actually exist.

If coercion doesn't exist that you'll get a TypeError, isn't it?
Observed "coerced" values and not observed "coerced" values are coerced values both ;)

Anyway our dialog creates questions around paragraph from 7.1 Type Conversion.
I think it must be edit to exclude potential questions. So spec must be improved:

  • Maybe specification needs to add its' meaning for coercion term.
  • Maybe specification needs to add some comments for sentence: The BigInt type has no implicit conversions ..

But now without any improvements spec writes that coercion for BigInt exists via Table 12. BigInt conversions

Was this page helpful?
0 / 5 - 0 ratings