ask icon indicating copy to clipboard operation
ask copied to clipboard

Build error when using `Mapping<K,V,H>` where `V` has no default constructor.

Open ashutoshvarma opened this issue 3 years ago • 1 comments

Problem

Error when build contracts when using Mapping<K,V,H> type where type V take more than one required constructor arguments.

Example contract

Using Uint8Array for mapping value, it takes length as mandatory constructor argument.

import {
    AccountId, HashKeccak256, Mapping
} from "ask-lang";

@spreadLayout()
class Data {
  map: Mapping<AccountId, Uint8Array, HashKeccak256> = new Mapping();
}

@contract()
export class Contract {
  data: Data;

  constructor() {
    this.data = new Data();
  }

  @constructor()
  default(): void {}

  @message()
  get(account: AccountId): Uint8Array {
    // return new Uint8Array(0);
    return this.data.map.get(account); // works if this line is commemted out.
  }
}

Error

$ ASK_CONFIG=./askconfig.json asc --config asconfig.json serialize.ts
ERROR TS2554: Expected 1 arguments, but got 0.

             return instantiate<T>();
                    ~~~~~~~~~~~~~~~~
 in ~lib/as-serde-scale/misc.ts(21,20)

FAILURE 1 compile error(s)
error Command failed with exit code 1.

ashutoshvarma avatar Jan 30 '23 06:01 ashutoshvarma

Yeah, Because we need to construct this type first and then decode storage data into it. You should wrap this type when it's used to be a storage type. Now it's actually not good design. But it's not easy to design a better way in AssemblyScript.

Creates a class instance must need to call constructor(instantiate) of this class. But there is no good way to support different constructor

yjhmelody avatar Jan 30 '23 06:01 yjhmelody