Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Vector.toArray for casting #32

Open
DanielRX opened this issue Aug 8, 2019 · 2 comments
Open

Vector.toArray for casting #32

DanielRX opened this issue Aug 8, 2019 · 2 comments

Comments

@DanielRX
Copy link
Contributor

DanielRX commented Aug 8, 2019

Looking at collections.Vector there is no way to return the values in the vector to the outside world. In order to return the whole vector from the contract, I have something like

function toArray<T>(x: collections.Vector<T>): Array<T> {
  const len = x.length;
  const out = new Array<T>();
  for(let i = 0; i < len; i++) { out.push(x[i]); }
  return out;
}

However outside of the nearlib, I can't use generic types, and instead of patching the vector class myself, wanted to see if it could be added to the lib.

Another note: The type of the class can have [key: number]: T; added to it, and then
x[0] is typed correctly (right now there is a typescript warning).

@evgenykuzyakov
Copy link
Contributor

Instead of casting entire vector to array, we should instead expose a getRange (or getSubarray) function, that takes (index, length) and returns an array of [index,index+length) elements. By default it can take index=0 and length=-1, which would return full vector.

Because a persistent vector may grow beyond smart contract ram, toArray may not fit into allocated memory.

@DanielRX
Copy link
Contributor Author

DanielRX commented Aug 8, 2019

for a name .slice would be common in JS already, so I think .slice would work?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants