Changing the values of elements in an array

Using the documentation provided here: Verse Language Quick Reference
There is this information in the paragraph regarding boards


When I try to follow these guidelines or even copy this piece of code I get the following error:
This invocation calls a function that has the ‘decides’ effect, which is not allowed by its context. The ‘decides’ effect indicates that the invocation calls a function that might fail, and so must occur in a failure context that will handle the failure. Some examples of failure contexts are the condition clause of an ‘if’, the left operand of ‘or’, or the clause of the ‘logic’ macro.
What does it do wrong if the documentation shows it is allowed? The only solution I found is to rewrite the array and change the data in a specific position, but it seems illogical that this is the only way to change the value in the array.

Like the error said you need to put it in “failure contexts”
verse use if and for. Here is what you need to do:

var ExampleArray : []int = array{20, 21, 22}
if (set ExampleArray[1] = 77):

So I didn’t know that you could actually specify an index for which to change in an array, I thought it was only for maps for some reason.

But to clarify what @TheRealGwH said, calling set ExampleArray[1] = 77 is failable because the index 1 could not exist yet inside this array, thus the operation would fail and your entire block would crash, (this is why the compiler won’t let you do it) but wrapping it inside a failable context allows you to do that without worrying.

You can use the method the guy provided, with an if statement, but I find this method more readable though :

# Any of these
option{set ExampleArray[1] = 77}
option. set ExampleArray[1] = 77
option:
    set ExampleArray[1] = 77

Using the if statement, you’d still be able to manage the case where the assignement failed, so I guess it’s still ok to use anyway :person_shrugging:

1 Like

You can do all of the same syntax with an if statement, it doesn’t need a then branch:

if {set ExampleArray[1] = 77}
if. set ExampleArray[1] = 77
if:
    set ExampleArray[1] = 77
1 Like

Yeah I know, I’m just saying that it’s not really readable, for me atleast, I’m not used to mutate variables inside if statement, in any language you know…

When using an if, it implicitly suggests to have a then part, from an english language perspective. It’s not because it exists that we have to use it. :person_shrugging:

Ah, I just think using option is a bit weird bc the return value is meant to be used later, not forgotten about

It might help to think of the if with no then/else as a “if this is allowed, do it” when it comes to failure

2 Likes

Thank you all for your reply. I understand this error and I know that it can be circumvented by putting it in the if statement, but I am wondering why the official documentation provided by epic includes the option not to use the if statement. I was wondering if I was making a logical error somewhere, but as you can see, there is simply an error in the documentation.

Hey @Devartis,

You are correct, this is an error in the documentation. We will get that fixed. Thanks for catching that!

Also good explanation by @Ep8Script of using if vs option as a failure context. If you wanted to store the result of a failure expression, option is a good…option! But, if you just want to do the thing in the failure expression and you don’t need to store the result, if might be a better choice.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.