dfinity / motoko

Simple high-level language for writing Internet Computer canisters

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't concatenate arrays

vporton opened this issue · comments

actor {
  public shared func f(): async () {
      var newEntries: [Int] = [];
      newEntries #= newEntries;
  };
}

The above code must be compilable, but:

$ moc x.mo
x.mo:4.7-4.31: type error [M0060], operator is not defined for operand types
  [Int]
and
  [Int]
$ moc --version
Motoko compiler 0.11.0+ (source yh3wb1vx-vj8j4j32-k8gq4m8y-4cxlqinf)

Checked with a newer version of moc, the same bug:

$ moc --version
Motoko compiler 0.11.1+ (source 7k01snp5-bm3lbckz-84fkkzp5-zqz3gvyv)

Because Array is not Text :-) and #= (as well as #) applies to Text.

Background: Text has a representation of ropes internally and provides O(1) concatenation. Arrays are contiguous, so concatenation is a linear complexity operation. You'll have to implement it in terms of Array.tabulate.

https://github.com/dfinity/motoko-base/blob/6f49e5f877742b79e97ef1b6f226a7f905ba795c/src/Array.mo#L176 provides Array.append, but we deprecate it because it easily leads to quadratic behaviour.