Mutable method receivers in closures #18755
-
In this program I have the following function fn (mut c BaseClient) attempt_process(cmd Cmder, attempt int) ! {
c.with_connection(fn [cmd] (mut cn pool.Connection) ! {
cn.with_writer(fn [cmd] (mut wr proto.Writer) ! {
write_cmd(mut wr, cmd)!
})!
cn.with_reader(cmd.read_reply)! // <--
})!
} The method call marked by the arrow, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
It's normal. Closures make copies of the variables. If you want the value modified outside the closure, you have to pass a reference. Of course, this still shouldn't work because the original is not marked One could argue that the copy should not be mutable either, since the original wasn't, and there's no |
Beta Was this translation helpful? Give feedback.
-
I have not found an answer this this question. I have rewritten the function from this form: fn (mut c BaseClient) attempt_process(mut cmd Cmder) ! {
c.with_connection(fn [mut cmd] (mut cn pool.Connection) ! {
cn.with_writer(fn [cmd] (mut wr proto.Writer) ! {
write_cmd(mut wr, cmd)!
})!
cn.with_reader(cmd.read_reply)!
})!
} To this form: fn (mut c BaseClient) attempt_process(mut cmd Cmder) ! {
c.with_connection(fn [mut cmd] (mut cn pool.Connection) ! {
cn.with_writer(fn [cmd] (mut wr proto.Writer) ! {
write_cmd(mut wr, cmd)!
})!
cn.with_reader(fn [mut cmd] (mut rd proto.Reader) ! {
cmd.read_reply(mut rd)!
})!
})!
} When the function is written this way, I believe the whole problem is caused by passing a method as a function: |
Beta Was this translation helpful? Give feedback.
-
This post is an attempt to provide more information and a way to reproduce what I have encountered. This attempt follows a conversations that I had with @spytheman Setupgit clone --branch bugrep_function-params-in-closures https://github.com/Coachonko/redis.git
cd redis
git checkout f99737dc1386c667932bbff9809df974692f3a86
v run src/cmd_test.v Following these steps, everything should work. Problem 1: passing mutable method receiver with function parametergit checkout 3a7d04e59a92416700a03191ad605f35daf6a65e
v run src/cmd_test.v Following these steps, the console will show the output of 2 Problem 2: unexpected runtime memory access error when
|
Beta Was this translation helpful? Give feedback.
I have not found an answer this this question. I have rewritten the function from this form:
To this form:
When the function is written this way,
cmd
(whi…