What’s the correct way to convert an Array to Buffer in Motoko for both immutable and mutable?
Documentation display Buffer.fromArray and Buffer.fromVarArray but none seem to effectively exists (Buffer.fromArray - field fromArray does not exist in type, dfx v0.11.2)
Doing it more directly would require changing the Buffer class’s constructor, so that it takes an array, but that is not planned for any near-term revision to the API, as far as I know.
Dittoing Matthew’s response. You just have to call it as a static library function.
There will be a slight optimization to these functions in the new version, but they will be backwards compatible/under-the-hood, so you should be able to freely use these right now, with no problem.
As a note though, the current buffer to array conversions (the reverse direction) that exist as class methods will be deprecated in the new buffer, and users will be encouraged to use new static versions.
Thanks for the answer @matthewhammer and @kentosugama. I think I get it but I am to dumb to intepret it code wise. Do you have a concrete example somewhere?
Sorry for asking even it’s probably pretty clear for most people.
So, turns out I’m maybe not that dumb, just a bit.
In VS code, your snippet works fine in one project but not in another sample repo. I ask myself if I don’t have a conflict with Vessel or something (which I don’t use in first project).
I ran into this same issue and the way I got around it was to point my vessel configuration to one of the recent moc-xxx tags on the Motoko-Base repo. Probably not the wisest thing to do but it did fix my problem and I’ve been able to call Buffer.fromArray() ever since.