Option.unwrap error

Getting this warning as below when i build

Hex.mo:48.17-48.30: warning [M0154], field unwrap is deprecated:
Option.unwrap is unsafe and fails if the argument is null; it will be removed soon; use a switch or do? expression instead

Hex.mo:51.27-51.39: warning [M0154], field append is deprecated:
Array.append has critical performance flaws; use a Buffer, and Buffer.append, instead.

Is there any sample for the how we can use do? instead of Option.unwrap and Buffer.append instead of Array.append

Thank you

Hey @gladguy,

For the first question on handling options, please have a look at the reference here: Untitled :: Internet Computer
There’s an explanation of handling options witch switch/case and below is an explanation of option blocks do ? { }.

Concerning Array.append and Buffer.append, an example to work with Buffers generated from Arrays would be the following. However, it would be good if someone with more Motoko experience could comment

let array1 = [1,2];
let array2 = [3,4];

let buffer1 = Buffer.Buffer<Nat>(array1.size());
for (entry in array1.vals()) {
  buffer1.add(entry)
};

let buffer2 = Buffer.Buffer<Nat>(array2.size());
for (entry in array2.vals()) {
  buffer2.add(entry)
};

buffer1.append(buffer2);
3 Likes

Thank you very much domwoe

There’s also some example usage for Buffer here:
https://github.com/dfinity/motoko-base/blob/master/test/bufTest.mo

And some examples of do? with a little explanation:
https://smartcontracts.org/docs/language-guide/control-flow.html#option-blocks
and (as linked above):
https://smartcontracts.org/docs/language-guide/overview.html#_option_blocks

5 Likes