Here is my code:
class State
var a: Array[U32] ref
new create() =>
a = Array[U32](33)
actor Main
new create(env: Env) =>
var s = State.create()
s.a(0) = 5
I get the following compile error:
Building builtin -> /home/willem/pony/ponyc/packages/builtin
Building . -> /home/willem/pony/ponyc/examples/experiments
Error:
/home/willem/pony/ponyc/examples/experiments/state.pony:16:26: function body is partial but the function is not
new create(env: Env) =>
^
Error:
/home/willem/pony/ponyc/examples/experiments/state.pony:18:17: an error can be raised here
s.regs(0) = 5
^
I'm sure it is a reference issue, but am confused why it's a partial.
Thanks in advance!!
Willem
The constructor is partial because an error could be raised if the index is out of bounds. The explanation is shown in the docs.
@willemneal - Pony requires you to explicitly handle any error that might be raised in your code. So, you either have to surround the relevant code with a try block to catch the error (and an optional else block to take an action in case of failure), or you have to mark the entire function as a partial function with a ?. Here, partial function just means a function that might raise an error out of it. See this section of the tutorial for more info on this concept of errors and partial functions.
Here, the compiler error message is telling you that the line s.regs(0) = 5 can raise an error. This line is syntax sugar for s.regs.update(0, 5). The docs for Array.update tell you that this function can raise an error when the index is out of bounds. The compiler doesn't know at compile time whether this _will_ raise an error because the size of the array is dynamic, so it requires you to "guard against" this case with a try block.
Thank you both for the replies. That was exactly my problem and I learned something!
@Theodus The docs reference is now a 404.
@jemc the docs for Array.update is now a 404.
I think they are the same URL so not actually two problems. :-) I think it might be worth editing to point at the right place for people finding this issue via Google.
Thanks for the heads up @russel, I've updated the links