Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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());
like image 708
Ishamael Avatar asked Sep 11 '19 22:09

Ishamael


1 Answers

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());
like image 174
berryguy Avatar answered Jan 01 '23 20:01

berryguy