我们如何在Dart 2中将int转换为BigInt? [英] How do we cast an int as a BigInt in Dart 2?
问题描述
在Dart 2(Dart VM版本:2.0.0-dev.50.0)中使用 BigInt
类,我遇到了以下问题:
Playing around with the BigInt
class in Dart 2 (Dart VM version: 2.0.0-dev.50.0), I came across the following issues:
BigInt x = 5;
print(x % 3); // 2
print(x ~/ 3); // 1
到目前为止,很好,但是...
So far, so good, but...
当我尝试访问方法时,例如:
When I try to access the methods, for example:
print(x.pow(2));
我得到一个 NoSuchMethodError:类'int'没有实例方法'pow'异常.看来 x
被视为香草味的 int
...
I get a NoSuchMethodError: Class 'int' has no instance method 'pow' exception. It appears that x
is being treated as a vanilla flavored int
...
此外,如果我尝试更明确地实例化 x
:
Further, if I try to instantiate x
more explicitly:
var x = new BigInt.from(5);
我得到 NoSuchMethodError:类'int'没有实例获取方法'_used'.
我误会了文档?具体来说,我们如何将 int
转换为 5
或 ['a','b','c'].length
BigInt
对象?
Am I misunderstanding the documentation? Specifically, how do we cast an int
, like 5
or ['a', 'b', 'c'].length
, as a BigInt
object?
尽管这不能解释为什么 BigInt x = 5
以及 x%3
之类的表达式不会产生错误并按预期工作,但这似乎可以仅仅是由于 BigInt
和 int
不能很好地配合.例如,一旦 x
确实代表了一个 BigInt
(与使用 BigInt x = 5
实例化的情况不同,此后 x
似乎代表常规的 int
),则表达式 x%3
不起作用,因为%
操作期望使用右侧的BigInt
,而不是 3
.(表达式需要更改为笨重的 x%new BigInt.from(3)
,以摆脱 NoSuchMethodError:类'int'没有实例getter'_used'例外.)
Although this doesn't explain why BigInt x = 5
and then such expressions as x % 3
do not generate errors and work as expected, this appears to simply be due to BigInt
s and int
s not playing well together. For example, once x
really does represent a BigInt
(unlike if it's instantiated using BigInt x = 5
, after which x
appears to represent a regular int
) then the expression x % 3
doesn't work because the %
operation is expecting a BigInt
on the right hand side, which 3
is not. (The expression needs to change to the clunky x % new BigInt.from(3)
to get rid of the NoSuchMethodError: Class 'int' has no instance getter '_used' exception.)
正如我已经提到的,这并不能解释所有内容.允许这样的表达式,例如 BigInt x = 5
,然后像预期的那样运行诸如 x%3
的表达式,这表明 BigInt
s和 int
是 meant ,它们至少可以在某些设计师的论文上无缝地协同工作...
As I mentioned already, this does not explain everything. The fact that such expressions as BigInt x = 5
are allowed, and that expressions such as x % 3
then work as expected suggests that BigInt
s and int
s are meant to work seamlessly together, at least on some designer's papers...
推荐答案
您正在Dart 1生产模式下运行Dart,而没有使用分析器测试代码.这意味着您不会进行类型检查,并且您的程序确实不是类型正确的.文字 5
不是 BigInt
,而是 int
.您将其分配给类型为 BigInt
的变量,但是在Dart 1生产模式下,该变量类型将被忽略,从而成功". BigInt
s和 int
s不能无缝地一起工作,但是如果您实际上不检查类型,则不会知道使用的是错误的类型
You are running Dart in Dart 1 production mode and without testing your code with the analyzer. That means that you get no type checking, and your program is indeed not type-correct.
The literal 5
is not a BigInt
, it is an int
. You are assigning it to a variable typed as BigInt
, but in Dart 1 production mode, the variable type is ignored, so that "succeeds".
BigInt
s and int
s are not meant to work seamlessly together, but if you don't actually check the types, you won't know that you are using the wrong one.
您的修正 var x = new BigInt.from(5);
是正确的.Dart不会进行隐式转换,您必须显式创建一个 BigInt
来获取隐式转换.这也意味着您必须这样做:
Your fix, var x = new BigInt.from(5);
, is correct. Dart does not do implicit conversion, you have to explicitly create a BigInt
to get one.
That also means that you have to do:
var x = new BigInt.from(5);
print(x % new BigInt.from(3)); // 2
该代码应该起作用.因此,如果抛出,则表示出了问题.我在Dart 2.0.0-dev.49.0中尝试了该代码,并且可以正常工作.您正在使用哪个版本的Dart( dart --version
)以及如何运行它?
That code should work. so if it throws, something is wrong. I tried the code in Dart 2.0.0-dev.49.0, and it worked. Which version of Dart are you using (dart --version
) and how are you running it?
如果您将程序作为 dart --checked program.dart
运行,则当您尝试将 int
分配给 BigNum时,将出现类型错误.
变量.
If you run your program as dart --checked program.dart
then you will get the type error when you try to assign an int
to a BigNum
variable.
这篇关于我们如何在Dart 2中将int转换为BigInt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!