如何在 AssemblyScript / Near 中打印数组的长度?

How to print the length of an array in AssemblyScript / Near?

我正在 near 上试验简单的汇编脚本,但似乎找不到打印数组长度的方法。这是最小的复制品:

  let a = new Array<string>();
  logging.log(a.length.toString());

不编译

ERROR TS2339: Property 'toString' does not exist on type 'i32'.

   logging.log(a.length.toString());
                        ~~~~~~~~
 in assembly/main.ts(171,23)

虽然 toString() 显然存在于 i32,例如以下代码段编译并运行:

  let a: i32 = 5;
  logging.log(a.toString());

问题是 assemblyscript 类型解析器无法解析某些类型的表达式,但此问题已在上游 assemblyscript 中 https://github.com/AssemblyScript/assemblyscript/pull/726 处修复。我们将在更新编译器时为智能合约修复此问题。目前,解决方法是使用临时变量:

let a = new Array<string>();
let l = a.length;
logging.log(l.toString());