zksecurity / noname

Noname: a programming language to write zkapps
https://zksecurity.github.io/noname/
150 stars 34 forks source link

be clearer on mutability #134

Open mimoo opened 2 weeks ago

mimoo commented 2 weeks ago

we're not super clear on what can be mutated and what cannot be.

For example, can this function call mutate thing?

let mut thing = 5;
stuff(thing);

imo we should make mutability when we're passing mutable values around. In rust we would have to write stuff(&mut thing). In noname everything is passed by value so I'm not sure what would be a good syntax (there is no such thing as stuff(&thing) for example. Maybe stuff(mut thing)?

How about this example:

fn stuff(arg: Field) { /* ... */ }

can the body of stuff mutate arg? I think it could only if we have fn stuff(mut arg: Field), which I don't think is something we currently have in functions/methods.

The best thing to do would be to create tests to showcase these examples

katat commented 2 weeks ago

fn stuff(mut arg: Field) makes sense to mutate an external variable inside the function.

atm, all the variables passing between the functions are done via copying their values. The reference variable is only usable at local scope.

mimoo commented 2 weeks ago

arg, so now I'm wondering what this does exactly if stuff attempts to mutate a, does it fail to compile because stuff doesn't see a as mutable?

let mut a = 5;
stuff(a);

if this is the case then functions are pure, which might not be the worst thing for now...

katat commented 2 weeks ago

The function stuff(a) can be called with that mutable variable. Inside that function, that variable a is seen as immutable. As long as it doesn't directly mutate the argument a which is not mutable as the syntax mut is not supported for the function argument, it won't throw error.