So in the previous lesson, we looked at how we can create functions in Motoko and how we can actually
start modifying our variables using these functions.
Now, we saw that the final function that we added was able to subtract whatever user-entered amount from
the currentValue variable.
Now there's just one problem here, and you might have already spotted it.
If current value is subtracted to a point where it's less than zero, then we're actually going to get
an error in this...
when we call this function. And I can show you that right here by just typing 999, so it's going to
subtract 999 from 100.
And we end up with an error that says, "Natural subtraction underflow".
So what this is saying is that this is a natural number, the currentValue,
so it starts from 0 and it goes up and it could only be positive.
So if we subtract it to below zero, if this amount happens to be very large, then it's going to under
flow.
And this is not allowed with this current data type.
So what we can do to prevent this from happening is to quickly add an if-statement.
So I just wanted to show you that the conditionals and the control flow in Motoko, looks pretty similar
to what you already know in JavaScript.
If the condition is the currentValue - amount is greater than or equal to 0,
then we're going to go ahead and carry out this function where we set the currentValue to be the previous
currentValue subtracting the amount, and then we're going to print it out.
Now you'll see this yellow squiggly line as soon as you write this and it gives you a warning, not
an error because its yellow. It says, "The operator may trap or inferred type Nat." So what this error is trying
to say is that due to the operation, it's unsure what the final data type will be after this operation,
in this case subtraction.
And then it may not be able to be compared in this if-statement and it would lead to problems down the
line.
So to fix this, we basically just have to explicitly say what is the data type of the final value of
the calculation.
And we can either do that in line or we can split it out into a separate line and define the data type.
So we can create a new constant, because we won't need it for very long, and we'll just call it the tempValue
and we'll set that to equal the currentValue subtract the amount.
Now inside this constant, we can assign it a type. So we can use the semicolon, and then we can give
this tempValue a data type of int, which is an integer, which means it can be any whole number,
positive or negative.
And then we replace this line with our tempValue so that it will compare it against zero.
So now we have no ambiguities in terms of what data type is what, the input amount is data type
Nat, the currentValue is data type
Nat and this here is inferred, by the way,
so if you wanted to make it clear, you can actually do that.
You can just simply add this.
But by default, as long as you're giving it a positive number, automatically assign it a natural number
data type.
So let's just go ahead and finish off the statement.
If it does happen to be less than 0, we want to add an else statement to catch that.
Just pause the video and quickly add an else statement so that you instead Debug.print and tell at least
the developer that there's a problem here.
There's an issue.
Give that a go.
All right.
This is going to be pretty easy for you by now.
All we going to do is we're just going to add a Debug.print and then inside here, we're going to
just simply type...
Now, in this case, because we're not actually printing anything other than text,
we don't need the debug_show.
So we spoke about this an earlier lesson,
if you're not quite sure, be sure to review that.
So now let's just finally make sure that it actually works.
If we go ahead and withdraw again a large amount from our bank, you can see in this case it does not
fail; it actually goes through successfully.
And when we go back, you can see that we get our debug being triggered. Amount too large; currentValue
less than 0,
our current value is not changed,
so this part of the if-statement doesn't get triggered.
So now if we actually try to withdraw a more reasonable value like $10, you can see it succeeds and
it subtracts 10 from 100.
So there you have it.
So in this lesson, I just wanted to quickly demo that a lot of the language features in Motoko is the
same as other languages that you've come across.
There's only a couple of things that are a little bit different, like the type system and like how
you work with the ca nisters, which I'm going to point out as we go along.
So hopefully this wasn't too hard.
And in the next lesson, I'm going to show you why it takes so long for some of these methods to call.
And what is the difference between an update method and a query method when it comes to canisters and
the ICP. For all of that
and more, head over to the next lesson.