I'm experimenting with simple assembly scripts on near and cannot seem to find a way to print the length of an array. Here's the smallest repro:
let a = new Array<string>();
logging.log(a.length.toString());
Doesn't compile with
ERROR TS2339: Property 'toString' does not exist on type 'i32'.
logging.log(a.length.toString());
~~~~~~~~
in assembly/main.ts(171,23)
While toString()
clearly exists on i32
, e.g. the following snippet compiles and works:
let a: i32 = 5;
logging.log(a.toString());
The problem is that assemblyscript type resolver cannot resolve certain kind of expressions, but this issue has been fixed here https://github.com/AssemblyScript/assemblyscript/pull/726 in upstream assemblyscript. We will fix this for smart contracts when we update the compiler. For now, the workaround is to use a temporary variable:
let a = new Array<string>();
let l = a.length;
logging.log(l.toString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With